Skip to content

doc: note that FreeEnvironment() must not run inside uv_run() - #67660

Open
magurotuna wants to merge 1 commit into
nodejs:mainfrom
magurotuna:doc-freeenvironment-uv-run
Open

magurotuna wants to merge 1 commit into
nodejs:mainfrom
magurotuna:doc-freeenvironment-uv-run

Conversation

@magurotuna

Copy link
Copy Markdown
Contributor

FreeEnvironment() runs the event loop because CleanupHandles() calls uv_run(). libuv states that uv_run() must not be called from a callback. The embedding docs do not mention this rule for FreeEnvironment(). This PR adds a warning to doc/api/embedding.md and src/node.h. The new paragraph in doc/api/embedding.md tells embedders to call FreeEnvironment() after the loop stops.

Calling FreeEnvironment() from a loop callback can hang. This repro shows the hang on Linux, macOS, and Windows with Node.js 24, 26, and the 2026-11-19 nightly (CI run). In the repro, two fs.stat() calls finish together: one in the main Environment and one in a second Environment. The libuv loop takes both finished requests at once and then runs their callbacks one by one. The first callback calls FreeEnvironment() on the second Environment. FreeEnvironment() waits for the second fs.stat() request, but the loop already took that request. The loop can run the second callback only after FreeEnvironment() returns, so the wait never ends.

Question: freeing an Environment from an addon

The updated docs tell embedders to call FreeEnvironment() after the loop stops. However, addon code usually runs inside a loop callback, for example when JavaScript calls it. How should an addon free an Environment?

My workaround in magurotuna/vitest-pool-fastforks#14 makes uv_run() return early with uv_stop() and frees the Environment in a platform task:

// Called from JavaScript, so inside uv_run().
void FreeLater(node::Environment* env) {
  pending.push_back(env);
  uv_idle_start(&idle, [](uv_idle_t*) {});  // Keep the poll phase from blocking.
  uv_check_start(&check, OnCheck);
}

// Runs in the check phase of the same loop iteration.
void OnCheck(uv_check_t* handle) {
  uv_idle_stop(&idle);
  uv_check_stop(handle);
  uv_stop(handle->loop);  // uv_run() returns after this iteration.
  runner->PostTask(std::make_unique<FreeTask>());  // Calls FreeEnvironment().
}

SpinEventLoop() runs platform tasks (DrainTasks()) after uv_run() returns, so FreeTask runs outside uv_run(). The full code is freeLater() in the repro. Its deferred mode does not hang on any CI job. Can embedders rely on this order?

Refs: #65691


AI disclosure: I used Claude Code to research and to help write the added doc and PR body. I reviewed all of them.

@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 Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

@magurotuna
magurotuna force-pushed the doc-freeenvironment-uv-run branch from 1779848 to ffbadc2 Compare November 20, 2026 14:52
@magurotuna
magurotuna marked this pull request as ready for review November 20, 2026 15:05
@MikeMcC399

This comment was marked as outdated.

@magurotuna

Copy link
Copy Markdown
Contributor Author

@MikeMcC399 Thank you for pointing that out, and sorry that I missed that guideline. I will focus on my first PR #67508 until it lands, and then come back to this PR. I'll close this PR for now so that it doesn't show up in the "Open PR" view.

@magurotuna magurotuna closed this Nov 21, 2026
@magurotuna magurotuna reopened this Nov 30, 2026
@magurotuna

Copy link
Copy Markdown
Contributor Author

Reopening now that #67508 has landed.

@MikeMcC399

This comment was marked as resolved.

FreeEnvironment() runs the event loop of the Environment. If an
embedder calls it from a callback of that loop, uv_run() runs nested,
and libuv does not allow that. It can hang: the outer uv_run() can hold
thread pool completions that the inner uv_run() never sees.

Signed-off-by: Yusuke "YT" Tanaka <ytanaka@cloudflare.com>
@magurotuna
magurotuna force-pushed the doc-freeenvironment-uv-run branch from ffbadc2 to 7490888 Compare November 30, 2026 15:04
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.58%. Comparing base (91f8170) to head (7490888).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67660      +/-   ##
==========================================
- Coverage   90.58%   90.58%   -0.01%     
==========================================
  Files         802      802              
  Lines      286659   286659              
  Branches    55370    55366       -4     
==========================================
- Hits       259676   259673       -3     
+ Misses      17083    17077       -6     
- Partials     9900     9909       +9     
Files with missing lines Coverage Δ
src/node.h 92.15% <ø> (ø)

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

3 participants