MCPcopy Create free account
hub / github.com/Snapchat/Valdi / teardown

Method teardown

valdi/src/valdi/runtime/JavaScript/JavaScriptRuntime.cpp:371–424  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

369 _globalContext = _contextManager->createContext(nullptr, nullptr, /* deferRender */ true);
370 // Keep a +1 disposable until we complete the teardown
371 _globalContext->retainDisposables();
372}
373
374void JavaScriptRuntime::postInit() {
375 if (!_isWorker && _anrDetector != nullptr) {
376 _anrDetector->appendTaskScheduler(this);
377 }
378
379 _contextHandler = makeShared<JavaScriptComponentContextHandler>(*this, this, *_logger);
380 _initLock.leaveIfNotCompleted();
381
382 if (auto listener = getListener()) {
383 auto runtimeTweaks = listener->getRuntimeTweaks();
384 if (runtimeTweaks != nullptr) {
385 _dispatchQueue->setDisableSyncCallsInCallingThread(runtimeTweaks->disableSyncCallsInCallingThread());
386 // Pull the termination mode here too: worker runtimes are created fresh (runtimeCreateWorker)
387 // and only inherit the host's listener, not its pushed tweaks, so Runtime::setRuntimeTweaks
388 // (which targets the host runtime) never reaches them. Reading it at postInit — after the
389 // worker's listener is set — lets a worker honor an aggressive-termination override.
390 setCooperativeTermination(runtimeTweaks->useCooperativeTermination());
391 setJoinJsThreadOnTeardown(runtimeTweaks->joinJsThreadOnTeardown());
392 setRefuseDispatchAfterJsQueueTeardown(runtimeTweaks->refuseDispatchAfterJsQueueTeardown());
393 }
394 }
395}
396
397JavaScriptRuntime::~JavaScriptRuntime() {
398 VALDI_DEBUG(*_logger, "Destroying JavaScriptRuntime (instance ptr {})", static_cast<void*>(this));
399 fullTeardown();
400
401 // fullTeardown() -> teardown() early-returns without draining when the runtime is already
402 // disposed, trusting a prior disposer to have quiesced the JS thread. That trust is unsafe:
403 // dispatched JS-thread tasks hold the runtime only by raw pointer, so an in-flight loadJsModule can
404 // still be mutating members (e.g. the lock-free _moduleResourceTracker) when the last ref drops on
405 // another thread. The only off-thread join would otherwise be the _dispatchQueue member destructor,
406 // which runs AFTER _moduleResourceTracker is freed (member order) -- too late. Force the (idempotent)
407 // drain+join here, before any member is destroyed. Off the JS thread this joins; on it, it is a no-op.
408 //
409 // Gated by VALDI_JOIN_JS_THREAD_ON_TEARDOWN (default on): the join replaces the prior member-order
410 // crash with a wait, so a frozen JS thread turns it into a hang instead. The flag lets that trade be
411 // flipped off without a build if a frozen-JS population shows up.
412 if (_joinJsThreadOnTeardown) {
413 _dispatchQueue->fullTeardown();
414 }
415}
416
417void JavaScriptRuntime::doInitialize() {
418 if (_isDisposed || _hostTornDown) {
419 return;
420 }
421
422 VALDI_TRACE("Valdi.initializeJsRuntime");
423
424 // Preload the JS context
425
426 auto initializeResult = initializeContext();
427 if (!initializeResult) {

Callers 1

Calls 11

weakRefFunction · 0.85
leaveIfNotCompletedMethod · 0.80
removeTaskSchedulerMethod · 0.80
releaseDisposablesMethod · 0.80
destroyContextMethod · 0.65
clearMethod · 0.65
getMethod · 0.65
syncMethod · 0.45
fullTeardownMethod · 0.45
setContextMethod · 0.45
expiredMethod · 0.45

Tested by

no test coverage detected