Skip to content

test: cover Environments sharing an embedder-owned isolate - #66239

Open
codebytere wants to merge 5 commits into
nodejs:mainfrom
codebytere:test-multi-env-shared-isolate
Open

codebytere wants to merge 5 commits into
nodejs:mainfrom
codebytere:test-multi-env-shared-isolate

Conversation

@codebytere

@codebytere codebytere commented Sep 23, 2026

Copy link
Copy Markdown
Member

Refs: #65977

Node.js supports running several Environments on one isolate and event loop, including an isolate the embedder created itself, but no test builds that setup. The crashes fixed in it over the past month were all found by embedders.

This adds a cctest that does: it creates the isolate, CppHeap and contexts the way an embedder would, starts a few Environments that keep timers running, and then frees, stops and inspects them one at a time while checking that the others keep working. It runs once with a shared IsolateData and once with one per Environment. Reverting any of the recent fixes makes it fail.

Writing it turned up three small issues, fixed in separate commits:

  • The src: fix FreeEnvironment() breaking JS in sibling Environments #65977 fix didn't cover Environments with their own IsolateData; its counter is now per thread.
  • An Environment without an inspector threw a plain string from node:inspector; it now throws ERR_INSPECTOR_NOT_AVAILABLE.
  • Freeing an IsolateData before its Environments now fails a CHECK instead of leaving a dangling pointer.

It also rewrites the multiple-Environments section of embedding.md to describe the supported setup.


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

@codebytere codebytere added c++ Issues and PRs that require attention from people who are familiar with C++. test Issues and PRs related to Node.js core tests and test infrastructure. embedding Issues and PRs related to embedding Node.js in another project. labels Sep 23, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/inspector

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Sep 23, 2026
@codebytere
codebytere marked this pull request as ready for review September 23, 2026 12:59
@codecov

codecov Bot commented Sep 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.31%. Comparing base (6dfe4eb) to head (df2ae81).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
src/env.cc 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66239      +/-   ##
==========================================
+ Coverage   90.28%   90.31%   +0.02%     
==========================================
  Files         789      789              
  Lines      272878   272880       +2     
  Branches    52109    52104       -5     
==========================================
+ Hits       246363   246442      +79     
+ Misses      16979    16903      -76     
+ Partials     9536     9535       -1     
Files with missing lines Coverage Δ
src/api/callback.cc 83.25% <100.00%> (+0.47%) ⬆️
src/env.h 97.33% <100.00%> (+0.07%) ⬆️
src/inspector_agent.cc 83.20% <100.00%> (+1.46%) ⬆️
src/node_internals.h 80.35% <ø> (ø)
src/env.cc 82.16% <85.71%> (+0.04%) ⬆️

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

The FreeEnvironment() fix for sibling Environments keeps the depth of
nested Environment::CleanupHandles() calls on the IsolateData, so that
InternalCallbackScope can re-allow JavaScript for sibling Environments
while one of them is being freed. Environments that each have their own
IsolateData on the same isolate and loop never see that counter and
still fail with "illegal access". Environments that share a loop share a
thread, so keep the depth in a thread_local instead.

Refs: nodejs#65977
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
…out one

An Environment created with kNoCreateInspector threw a bare string from
inspector.Session#connect(), inspector.open() and the other Agent entry
points, so callers could not tell the condition apart by error code. Use
the ERR_INSPECTOR_NOT_AVAILABLE code that connectToMainThread() already
throws for the same situation.

Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
FreeIsolateData() while an Environment created from it is still alive
left that Environment with a dangling pointer and failed later in
unrelated code. Count the Environments using an IsolateData and CHECK
in its destructor that none are left.

Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
@codebytere
codebytere force-pushed the test-multi-env-shared-isolate branch from f55fe32 to df2ae81 Compare September 23, 2026 15:41
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++. embedding Issues and PRs related to embedding Node.js in another project. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. test Issues and PRs related to Node.js core tests and test infrastructure.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants