Title: doc: note that FreeEnvironment() must not run inside uv_run() by magurotuna · Pull Request #67660 · nodejs/node · GitHub
Open Graph Title: doc: note that FreeEnvironment() must not run inside uv_run() by magurotuna · Pull Request #67660 · nodejs/node
X Title: doc: note that FreeEnvironment() must not run inside uv_run() by magurotuna · Pull Request #67660 · nodejs/node
Description: 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
Open Graph Description: 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 FreeEnv...
X Description: 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 FreeEnv...
Opengraph URL: https://github.com/nodejs/node/pull/67660
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:e9f5bd0c-3812-26e4-41ea-2f7da812115c |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 84C6:20D128:44BA2F:5B42D5:6B0381D8 |
| html-safe-nonce | 0e0d1d884055361ad0a35bb558631b22f35b9252de0d8c6282650ace5d5b14d6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NEM2OjIwRDEyODo0NEJBMkY6NUI0MkQ1OjZCMDM4MUQ4IiwidmlzaXRvcl9pZCI6IjU0NTM0MzI2Nzc0Njg0MzkwMDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0ecc6a1356cf8d67f05ae310f1e035d6d5da3d787eb4373a241418a4a3dc2c6d |
| hovercard-subject-tag | pull_request:5415249762 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/nodejs/node/pull/67660/files |
| twitter:image | https://avatars.githubusercontent.com/u/23649474?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/23649474?s=400&v=4 |
| og:image:alt | 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 FreeEnv... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width