Skip to content

fix(worker): let a heap-limit terminate re-enter the inspector lock from console.log - #2070

Open
adrian-niculescu wants to merge 1 commit into
NativeScript:mainfrom
adrian-niculescu:fix/worker-console-log-inspector-lock
Open

adrian-niculescu wants to merge 1 commit into
NativeScript:mainfrom
adrian-niculescu:fix/worker-console-log-inspector-lock

Conversation

@adrian-niculescu

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

Copy link
Copy Markdown
Contributor

In a debug build, a worker that runs out of heap inside console.log can leave its thread deadlocked for good: the parent still gets the heap-limit error, but the worker never finishes terminating.

WorkerWrapper::ConsoleLog holds inspectorMutex_ across WorkerInspectorClient::consoleLog, which allocates on the worker heap: it captures a stack trace, and building the message text converts the arguments, which can run getters. When an allocation there reaches the worker's limit, V8 runs OnNearHeapLimit synchronously on the same thread, which calls Terminate(), which takes inspectorMutex_ again to notify the inspector. The mutex is not recursive, so the thread blocks on itself. This holds while no DevTools session has enabled the Debugger domain; once one has, V8 calls its own near-heap-limit callback instead of the worker's. inspectorMutex_ is a std::recursive_mutex now: the same thread re-enters, and every other thread is excluded as before. Releasing the mutex across consoleLog instead would not be safe, because a worker callback run through __runOnMainThread logs from the main thread while the worker thread can delete the client.

With OnNearHeapLimit called at that point by hand, the worker thread on main stays blocked in Terminate() under ConsoleLog, and with this change it finishes terminating. The only difference a spec could observe is a thread that never finishes, so there is none. The full device suite passes.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of worker termination and inspector operations during debug logging and memory-limit callbacks.

…rom console.log

A worker's console.log holds inspectorMutex_ across the inspector's consoleLog, which captures a stack trace and so allocates. When that allocation hits the worker's heap cap, the near-heap-limit callback calls Terminate(), which takes the same non-recursive mutex on the same thread and deadlocks the worker. The mutex is recursive now, so that re-entry proceeds while other threads stay excluded as before.
@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: 2ee0474e-70dc-4da1-a0de-71f2cb6b8f52
📥 Commits

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

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

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


📝 Walkthrough

Walkthrough

The inspector mutex changes from std::mutex to std::recursive_mutex. Termination notification, inspector creation and destruction, and console logging use the recursive mutex.

Changes

Inspector Lock

Layer / File(s) Summary
Recursive inspector lock and its use
test-app/runtime/src/main/cpp/WorkerWrapper.h, test-app/runtime/src/main/cpp/WorkerWrapper.cpp
inspectorMutex_ changes to std::recursive_mutex. Terminate, CreateInspector, DestroyInspector, and ConsoleLog use the lock. The declaration comment describes same-thread re-entry when stack capture triggers a near-heap-limit callback that calls Terminate().

Priority: ⬇️ Low

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

Change: Bug fix

Suggested reviewers: edusperoni

Merge Risk: ⚪ Minimal · up to ff1f6

The change allows worker termination to proceed when heap-limit handling re-enters the inspector lock, while continuing to serialize access from other threads. No material merge-blocking risk is established.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 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 identifies the main change: allowing heap-limit termination triggered during console.log to re-enter the inspector lock.
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 lock with care
A recursive mutex waits right there
Console logs and inspectors meet
Termination joins the thread
The rabbit hops along, contentedly.

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

@adrian-niculescu
adrian-niculescu marked this pull request as ready for review October 6, 2026 21:54
@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