Skip to content

fix(worker): tear down a worker runtime whose Java bootstrap failed - #2069

Open
adrian-niculescu wants to merge 2 commits into
NativeScript:mainfrom
adrian-niculescu:fix/worker-bootstrap-failure-teardown
Open

adrian-niculescu wants to merge 2 commits into
NativeScript:mainfrom
adrian-niculescu:fix/worker-bootstrap-failure-teardown

Conversation

@adrian-niculescu

@adrian-niculescu adrian-niculescu commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

A worker whose bootstrap fails after its native runtime is up, for example because internal/ts_helpers.js throws, reports the error to its parent but leaves the native runtime and its isolate allocated for the rest of the process.

Runtime.initWorkerRuntime creates the native runtime in runtime.init() and then runs ts_helpers.js. When that throws, the Java side rolls back its own caches and rethrows, so the call never returns the runtime id, WorkerWrapper::runtime_ stays null, and BackgroundLooper skips the teardown that disposes the isolate. The native runtime is still the thread's current runtime (PrepareV8Runtime sets it last, and UnwindFailedInit clears it when native init itself fails), so shutdown now takes it from there when runtime_ is null and tears it down on the existing path, detaching it from Java by its own id. That id matters when the failure comes after the Java runtime finished initializing, for example a logger that throws on its last line: Java still holds that runtime and its GC subscription, and detaching with the -1 the call never replaced would leave both pointing at the deleted native runtime.

With ts_helpers.js patched to throw in workers, a failed worker on main leaves its runtime current after shutdown, and with this change the runtime is gone and the error still reaches the parent once. That needs a patched asset, so there is no spec for it. The full device suite passes.

Summary by CodeRabbit

  • Bug Fixes
    • Improved cleanup when app startup fails, helping prevent a native runtime from being left undisposed.

When initWorkerRuntime built the native runtime and then failed in Java, for example because internal/ts_helpers.js threw, it threw before returning the runtime id, so the worker never took ownership and the native runtime and its isolate were never disposed. Worker shutdown now takes that runtime from the thread's current one and tears it down like any other.
@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bd98fcc6-9982-4d4a-9f63-97d3821f5511
📥 Commits

Reviewing files that changed from the base of the PR and between 9b12329 and 84d8a99.

📒 Files selected for processing (1)
  • test-app/runtime/src/main/cpp/WorkerWrapper.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.


📝 Walkthrough

Walkthrough

WorkerWrapper::BackgroundLooper now recovers the current runtime and its ID during shutdown if Java bootstrap fails before assigning runtime_. The recovered runtime can then follow the existing detach and disposal path.

Changes

Runtime shutdown recovery

Layer / File(s) Summary
Recover runtime during shutdown
test-app/runtime/src/main/cpp/WorkerWrapper.cpp
When Java bootstrap fails before assigning runtime_, shutdown obtains the current thread’s runtime and ID when available. The existing cleanup path then handles the recovered runtime.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: edusperoni

Merge Risk: ⚪ Minimal · up to 84d8a

A Java bootstrap failure after native initialization now allows shutdown to clean up the worker’s runtime. The inspected path supports the change, with no actionable merge-blocking risk established.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: tearing down a worker runtime when Java bootstrap fails.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the runtime’s trail,
And finds its ID when bootstraps fail.
The thread can leave its runtime clear,
Then detach and dispose with care.
One quiet hop, and shutdown’s done.

Comment @coderabbitai help to get the list of available commands.

The bootstrap can also fail after the Java runtime finished initializing, when the logger throws on its last line, and the Java side then still holds the runtime and its GC subscription. Detaching with the id the call never returned left both behind, so a later GC notification could reach the deleted native runtime.
@adrian-niculescu
adrian-niculescu marked this pull request as ready for review October 6, 2026 21:53
@adrian-niculescu

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant