Skip to content

src: fix Stop() terminating the next Environment on the isolate - #65819

Open
codebytere wants to merge 1 commit into
nodejs:mainfrom
codebytere:fix/embedder-stop-termination-leak
Open

src: fix Stop() terminating the next Environment on the isolate#65819
codebytere wants to merge 1 commit into
nodejs:mainfrom
codebytere:fix/embedder-stop-termination-leak

Conversation

@codebytere

@codebytere codebytere commented Sep 5, 2026

Copy link
Copy Markdown
Member

After Stop(env), freeing that Environment and creating a new one on the same isolate fails with the new Environment's first script terminated (LoadEnvironment() aborts) whenever no JavaScript ran in between, which is the normal case when Stop() is called from the process exit handler for an uncaught exception or on an idle Environment: Stop() requests TerminateExecution() and V8 only clears that request the next time JavaScript runs, so it outlived the Environment it was aimed at.

FreeEnvironment() now cancels a pending termination, which can only have been meant for the Environment being freed; Worker::Run() did this by hand with a TODO asking why it was needed, and that call moves into FreeEnvironment().

Tests: EnvironmentTest.StopFromExitHandlerDoesNotLeakIntoNextEnvironment (aborted before); the worker termination tests pass.

Refs: #33347


Disclosure: the code, test and this description were written by Claude Code, directed and reviewed by @codebytere.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. worker Issues and PRs related to the worker_threads module and Worker API. labels Sep 5, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@codebytere codebytere changed the title src: cancel pending termination in FreeEnvironment() src: fix Stop() terminating the next Environment on the isolate Sep 5, 2026
After `Stop(env)`, freeing the Environment and creating another one on
the same isolate failed whenever no JavaScript ran in between: the new
Environment's first script was terminated before it started. That is
the normal case when `Stop()` is called from the process exit handler
for an uncaught exception, or by an embedder while the loop is idle.
`Stop()` calls `isolate->TerminateExecution()` unless
`kDoNotTerminateIsolate` is set, and V8 only clears that request the
next time JavaScript runs, so it outlived the Environment it was meant
for.

Cancel a pending termination when the Environment it was meant for is
freed. `Worker::Run()` already did this by hand before freeing its
Environment, with a TODO asking why V8 hit a DCHECK without it; this is
why, and that call now happens in `FreeEnvironment()`.

Refs: nodejs#33347
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
@codecov

codecov Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.18%. Comparing base (b0d56c7) to head (ec575f4).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65819      +/-   ##
==========================================
- Coverage   90.19%   90.18%   -0.01%     
==========================================
  Files         770      770              
  Lines      264410   264410              
  Branches    50243    50238       -5     
==========================================
- Hits       238490   238464      -26     
- Misses      16924    16963      +39     
+ Partials     8996     8983      -13     
Files with missing lines Coverage Δ
src/api/environment.cc 78.22% <100.00%> (+0.34%) ⬆️
src/node_worker.cc 82.08% <ø> (-0.22%) ⬇️

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codebytere
codebytere force-pushed the fix/embedder-stop-termination-leak branch from 02b1736 to ec575f4 Compare September 5, 2026 17:56
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

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

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. worker Issues and PRs related to the worker_threads module and Worker API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants