Open
Conversation
WorkQueue was a separate class with its own thread, dispatcher, and cancel source. AppRuntime captured 'this' in lambdas passed to WorkQueue, creating split-lifetime issues where the worker thread could access partially-destroyed AppRuntime members during shutdown. This change inlines WorkQueue's functionality directly into AppRuntime: - Thread, dispatcher, cancel source, and env are now AppRuntime members - Destruction order is deterministic (no cross-object lifetime issues) - WorkQueue.h and WorkQueue.cpp are removed - AppRuntime_JSI.cpp updated to use AppRuntime::Dispatch instead of WorkQueue::Append, and fixed task.release() to std::move(task) for shared_ptr construction Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the AppRuntime threading model by inlining the former WorkQueue implementation into AppRuntime, aiming to eliminate split-lifetime shutdown hazards caused by cross-object thread/dispatcher ownership.
Changes:
- Removed the
WorkQueueclass/files and moved its thread, dispatcher, cancellation source, env tracking, and suspension lock intoAppRuntime. - Updated
AppRuntimeto manage the worker thread lifecycle directly (including the deadlock-avoidance “cancel + enqueue no-op” wake-up). - Updated the JSI task runner adapter to post tasks via
AppRuntime::Dispatchand modernized shared ownership creation for tasks.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| Core/AppRuntime/Source/WorkQueue.h | Removed (WorkQueue API eliminated). |
| Core/AppRuntime/Source/WorkQueue.cpp | Removed (WorkQueue implementation merged into AppRuntime). |
| Core/AppRuntime/Source/AppRuntime.cpp | Moved WorkQueue run-loop/suspend/resume/shutdown logic into AppRuntime and starts the worker thread directly. |
| Core/AppRuntime/Source/AppRuntime_JSI.cpp | TaskRunnerAdapter now targets AppRuntime instead of WorkQueue; task ownership construction updated. |
| Core/AppRuntime/Include/Babylon/AppRuntime.h | Added dispatcher/cancellation/thread members and an internal Append helper. |
| Core/AppRuntime/CMakeLists.txt | Removed WorkQueue sources from the AppRuntime target. |
Comments suppressed due to low confidence (1)
Core/AppRuntime/Source/AppRuntime.cpp:17
- The worker thread is started in the member initializer list. If any code in the constructor body throws after
m_threadis created (e.g.,Dispatch(...)or allocations inside it), stack unwinding will destroym_threadwhile it is still joinable, which will callstd::terminatebecauseAppRuntime’s destructor is not run for a partially-constructed object. Consider starting the thread after all potentially-throwing constructor work (or using a scope guard/RAII thread wrapper that joins on unwind).
AppRuntime::AppRuntime(Options options)
: m_options{std::move(options)}
, m_thread{[this] { RunPlatformTier(); }}
{
Dispatch([this](Napi::Env env) {
JsRuntime::CreateForJavaScript(env, [this](auto func) { Dispatch(std::move(func)); });
});
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merge WorkQueue into AppRuntime to eliminate split-lifetime issues.
Motivation
WorkQueue was a separate class with its own thread, dispatcher, and cancel source. AppRuntime captured
thisin lambdas passed to WorkQueue, creating split-lifetime issues where the worker thread could access partially-destroyed AppRuntime members during shutdown.Changes
Behavioral note
JSI tasks now route through Dispatch, which wraps them in the Execute/exception-handler. Previously, uncaught exceptions from JSI tasks would crash the process. They are now handled by UnhandledExceptionHandler like all other dispatched work.
The deadlock fix from #147 is preserved.