src: fix Stop() terminating the next Environment on the isolate - #65819
Open
codebytere wants to merge 1 commit into
Open
src: fix Stop() terminating the next Environment on the isolate#65819codebytere wants to merge 1 commit into
codebytere wants to merge 1 commit into
Conversation
Collaborator
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 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
codebytere
force-pushed
the
fix/embedder-stop-termination-leak
branch
from
September 5, 2026 17:56
02b1736 to
ec575f4
Compare
Collaborator
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.
After
Stop(env), freeing that Environment and creating another one on the same isolate can fail: the new Environment's first script is terminated before it runs, andCreateEnvironment()/LoadEnvironment()abort. It happens whenever no JavaScript runs betweenStop()andFreeEnvironment(), which is the normal case whenStop()is called from the process exit handler that node.h recommends and the process is exiting through an uncaught exception, or when an embedder stops an idle Environment from C++.Stop()callsisolate->TerminateExecution(), V8 only clears that request the next time JavaScript runs, and here it outlives the Environment it was meant for.FreeEnvironment()now cancels a pending termination request, since it can only have been aimed at the Environment being freed.Worker::Run()did exactly this by hand before freeing its Environment, with a TODO asking why V8 hit a DCHECK without it; this is why, and that call is removed in favour of the one inFreeEnvironment().Tests:
EnvironmentTest.StopFromExitHandlerDoesNotLeakIntoNextEnvironmentstops an Environment from the exit handler after an uncaught exception, frees it and creates a second one on the same isolate; the cctest binary aborts there before the change.test-async-hooks-worker-asyncfn-terminate-*and the other worker termination tests pass.Refs: #33347
Disclosure: the code, test and this description were written by Claude Code, directed and reviewed by @codebytere.