Repository navigation
doc: note that FreeEnvironment() must not run inside uv_run() - #67660
magurotuna wants to merge 1 commit into
Conversation
|
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. |
1779848 to
ffbadc2
Compare
This comment was marked as outdated.
This comment was marked as outdated.
|
@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. |
|
Reopening now that #67508 has landed. |
This comment was marked as resolved.
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>
ffbadc2 to
7490888
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
FreeEnvironment()runs the event loop becauseCleanupHandles()callsuv_run(). libuv states thatuv_run()must not be called from a callback. The embedding docs do not mention this rule forFreeEnvironment(). This PR adds a warning todoc/api/embedding.mdandsrc/node.h. The new paragraph indoc/api/embedding.mdtells embedders to callFreeEnvironment()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, twofs.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 callsFreeEnvironment()on the second Environment.FreeEnvironment()waits for the secondfs.stat()request, but the loop already took that request. The loop can run the second callback only afterFreeEnvironment()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 withuv_stop()and frees the Environment in a platform task:SpinEventLoop()runs platform tasks (DrainTasks()) afteruv_run()returns, soFreeTaskruns outsideuv_run(). The full code isfreeLater()in the repro. Itsdeferredmode 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.