fix: guard FuturePromise resolve against env teardown crash#2
Open
cam-from-ditto wants to merge 1 commit intoditto/closure-into-jsfunctionfrom
Open
fix: guard FuturePromise resolve against env teardown crash#2cam-from-ditto wants to merge 1 commit intoditto/closure-into-jsfunctionfrom
cam-from-ditto wants to merge 1 commit intoditto/closure-into-jsfunctionfrom
Conversation
In Node.js >= 22, calling napi_resolve_deferred / napi_reject_deferred inside the threadsafe-function callback used by execute_tokio_future crashes inside V8's GlobalHandles::Destroy when the environment is tearing down — the Persistent backing the Deferred has already been invalidated by the time the AsyncCb fires. Mirror the napi_delete_async_work fix: register a napi_add_env_cleanup_hook per FuturePromise to detect when the environment begins tearing down, and skip the deferred resolve/reject calls in that case. The hook is unregistered on normal completion. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
napi_delete_async_workenv-teardown fix) but for the TSFN-backedFuturePromiseused byEnv::execute_tokio_future.napi_add_env_cleanup_hookperFuturePromiseto detect when the N-API environment begins tearing down. When set, the TSFN'scall_js_cbskipsnapi_resolve_deferred/napi_reject_deferredto avoid crashing inside V8'sGlobalHandles::Destroy.Root cause
In Node.js >= 22, the
Persistent<Promise::Resolver>backing theDeferredcan be invalidated during environment teardown before the TSFN's queuedAsyncCbfires. When the AsyncCb then callsnapi_resolve_deferred, V8'sGlobalHandles::Destroysegfaults on the dangling handle.This is the trace that DEVX-852 was originally chasing:
Frame 2 is the function this PR guards.
Validation
CI run 25482278460 on getditto/ditto's
cam/DEVX-852/flaky-node-v24-sigsegvbranch (which has both this fix and the previously-mergednapi_delete_async_workfix) no longer hits the original signature. A different unrelated crash now surfaces (node::fs::FSReqPromise::~FSReqPromisefrom Node's own fs internals), confirming this PR closes its target trace.Test plan