| 460 | return; |
| 461 | } |
| 462 | |
| 463 | // Just in case postInit() was never called |
| 464 | _initLock.leaveIfNotCompleted(); |
| 465 | |
| 466 | _dispatchQueue->async([self = strongSmallRef(this)]() { self->teardownOnJsThread(true); }); |
| 467 | } |
| 468 | |
| 469 | void JavaScriptRuntime::requestExecutionTermination() { |
| 470 | // requestExecutionTermination() on the context only sets a sticky atomic flag (Hermes also triggers a |
| 471 | // non-blocking async break), so it is safe to call while holding _mutex. Doing so keeps it mutually |
| 472 | // exclusive with teardownOnJsThread(), which clears _javaScriptContext under the same lock: we never |
| 473 | // hold a strong context reference past the lock, avoiding both the teardown SC_ASSERT race and a |
| 474 | // wrong-thread context destruction if the dispatch queue is already torn down. |
| 475 | std::lock_guard<Mutex> lock(_mutex); |
| 476 | if (_isDisposed || _javaScriptContext == nullptr) { |
| 477 | return; |
| 478 | } |
| 479 | _javaScriptContext->requestExecutionTermination(); |
| 480 | } |
| 481 | |
| 482 | void JavaScriptRuntime::teardown(bool destroyContext) { |
| 483 | auto wasDisposed = _isDisposed.exchange(true); |
| 484 | if (wasDisposed) { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | // Just in case postInit() was never called |
| 489 | _initLock.leaveIfNotCompleted(); |
| 490 | |
| 491 | markWorkersHostTornDown(); |
| 492 | |
| 493 | // Workers share this runtime's listener (see createWorker); detach them so a worker |
| 494 | // outliving this runtime does not keep a dangling reference. A worker task holding a |
| 495 | // RetainedListener keeps the listener's owner alive until the call completes. |
| 496 | for (const auto& jsWorker : getAllWorkers()) { |
| 497 | jsWorker->setListener(nullptr, {}); |
| 498 | } |
| 499 | |
| 500 | if (destroyContext) { |
| 501 | _dispatchQueue->safeSync([&]() { teardownOnJsThread(true); }); |
| 502 | } else { |
| 503 | teardownOnJsThread(false); |
nothing calls this directly
no test coverage detected