Skip to content

src: fix FreeEnvironment() breaking JS in sibling Environments - #65977

Open
codebytere wants to merge 1 commit into
nodejs:mainfrom
codebytere:fix/embedder-free-env-shared-loop
Open

src: fix FreeEnvironment() breaking JS in sibling Environments#65977
codebytere wants to merge 1 commit into
nodejs:mainfrom
codebytere:fix/embedder-free-env-shared-loop

Conversation

@codebytere

Copy link
Copy Markdown
Member

When several Environments share one isolate and event loop (an embedder hosting them on one thread), calling FreeEnvironment() on one of them throws "illegal access" into the others' timers and I/O callbacks: FreeEnvironment() forbids JavaScript on the whole isolate with a DisallowJavascriptExecutionScope and then spins the shared loop in Environment::CleanupHandles() until its own handles close, and those loop turns also run the siblings' callbacks.

While an Environment is closing its handles, InternalCallbackScope now re-allows JavaScript for callbacks whose own Environment can still call into JS. The Environment being freed stays blocked: its callbacks fail the can_call_into_js() check before that point, and anything that bypasses InternalCallbackScope (V8 foreground tasks, raw addon callbacks) still runs under the disallow scope.

Tests: EnvironmentTest.FreeEnvironmentWhileSiblingHasActiveHandles frees one Environment while a sibling's interval is running and checks the sibling keeps ticking; it threw before.

Refs: #33874
Refs: #65819


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

`FreeEnvironment()` forbids JavaScript on the whole isolate with a
`DisallowJavascriptExecutionScope` and then spins the event loop in
`Environment::CleanupHandles()` until its handles are closed. When the
loop and isolate are shared with other Environments, as they are for an
embedder hosting several Environments on one thread, those loop turns
also run the other Environments' timers and I/O callbacks, and every
one of them failed with an "illegal access" exception thrown into
unrelated code.

While an Environment is closing its handles, let `InternalCallbackScope`
re-allow JavaScript for callbacks of Environments that can still call
into JS. The Environment being freed stays blocked: its callbacks fail
the `can_call_into_js()` check before that point, and anything that
bypasses `InternalCallbackScope` (V8 foreground tasks, raw addon
callbacks) still runs under the disallow scope.

Refs: nodejs#33874
Refs: nodejs#65819
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
@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. labels Sep 11, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@codecov

codecov Bot commented Sep 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.18%. Comparing base (0de4fcc) to head (902c011).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #65977   +/-   ##
=======================================
  Coverage   90.17%   90.18%           
=======================================
  Files         771      771           
  Lines      265470   265473    +3     
  Branches    50463    50465    +2     
=======================================
+ Hits       239383   239412   +29     
+ Misses      17055    17001   -54     
- Partials     9032     9060   +28     
Files with missing lines Coverage Δ
src/api/callback.cc 83.25% <100.00%> (+0.08%) ⬆️
src/env.cc 82.04% <100.00%> (-0.12%) ⬇️
src/env.h 97.22% <ø> (ø)
src/node_internals.h 80.35% <ø> (ø)

... and 36 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.

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants