Repository navigation
fix(ext/napi): cancel async sends after close - #36077
Merged
bartlomieju merged 1 commit intoJul 20, 2026
Merged
Conversation
nathanwhit
marked this pull request as ready for review
July 16, 2026 00:54
bartlomieju
approved these changes
Jul 17, 2026
bartlomieju
left a comment
Member
There was a problem hiding this comment.
LGTM. The ref-counted close-state approach genuinely closes the use-after-free window. The key insight is implemented correctly: pending sends check state.closing through their own Arc clone, not through the possibly-freed handle.
Traced the model:
- Threading:
async_work_senderis aV8CrossThreadTaskSpawner, so the send task (S) and deferred close task (C) execute serially on the runtime thread, FIFO. Only the enqueue fromuv_async_sendmay be cross-thread; execution never races, so there's no TOCTOU between thestate.closingcheck and the(*handle).async_cbderef in S. - UAF trace:
uv_closedoesclosing.swap(true, AcqRel)before spawning C (which runs the addon close cb that frees the handle). Since tasks run in program order on one thread, any task after C observesclosing == trueand bails before touching the handle. No ordering derefs a freed handle. - The
Arcoutlives all pending sends:Arc::into_rawat init, each S holds a clone, C consumes the original viafrom_raw+ drop only afterclose(handle). Soclosingis readable even after the handle is gone. - Ref-count discipline balanced across send / close / is_closing (correct
increment_strong_count+from_rawidiom, exactly onefrom_rawperinto_raw). CorrectlyArcnotRc. Double-close idempotent via theswapguard. - Layout:
statefield sits in the private region on both unix and windows variants with matching_paddingsubtraction, and thesizestest asserts againstlibuv_sys_lite::uv_async_tper-platform, so drift fails the test / underflow fails to compile. - The added
ref_op/unref_oparound C correctly keeps the loop alive until the deferred close cb fires.
Regression test aborts the process from the async_cb if a queued send runs after close — deterministic for the same-thread send-then-close case. Non-blocking: it doesn't exercise a genuine cross-thread uv_async_send racing uv_close (inherently hard to make deterministic), so that path rests on the reasoning above rather than a test.
bartlomieju
pushed a commit
that referenced
this pull request
Jul 23, 2026
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
uv_async_tclose state in its private ABI storageProblem
uv_async_send()queued a runtime callback that retained the addon-owned handle pointer. TheUV_ASYNCclose path then invoked the close callback synchronously, even though that callback is allowed to release the handle. A previously queued send could consequently run after close and access a handle that was no longer valid.This change gives each async handle private ref-counted close state. Pending sends retain that state and check it before touching the handle, while the close callback runs from the runtime queue after close has marked the handle.
Validation
./tools/format.jscargo test -p deno_napi sizes --libcargo check -p test_napicargo build -p test_napicargo build --bin denotests/napi/uv_test.js