Skip to content

fix(ext/napi): cancel async sends after close - #36077

Merged
bartlomieju merged 1 commit into
denoland:mainfrom
nathanwhit:fix/napi-async-close-lifetime
Jul 20, 2026
Merged

bartlomieju merged 1 commit into
denoland:mainfrom
nathanwhit:fix/napi-async-close-lifetime

Conversation

@nathanwhit

Copy link
Copy Markdown
Member

Summary

  • track uv_async_t close state in its private ABI storage
  • skip queued async callbacks once close has begun
  • defer the close callback until pending runtime tasks can observe cancellation

Problem

uv_async_send() queued a runtime callback that retained the addon-owned handle pointer. The UV_ASYNC close 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.js
  • cargo test -p deno_napi sizes --lib
  • cargo check -p test_napi
  • cargo build -p test_napi
  • cargo build --bin deno
  • focused async send/close regression
  • complete tests/napi/uv_test.js

@nathanwhit
nathanwhit marked this pull request as ready for review July 16, 2026 00:54
@bartlomieju bartlomieju changed the title fix(napi): cancel async sends after close fix(ext/napi): cancel async sends after close Jul 16, 2026

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_sender is a V8CrossThreadTaskSpawner, so the send task (S) and deferred close task (C) execute serially on the runtime thread, FIFO. Only the enqueue from uv_async_send may be cross-thread; execution never races, so there's no TOCTOU between the state.closing check and the (*handle).async_cb deref in S.
  • UAF trace: uv_close does closing.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 observes closing == true and bails before touching the handle. No ordering derefs a freed handle.
  • The Arc outlives all pending sends: Arc::into_raw at init, each S holds a clone, C consumes the original via from_raw + drop only after close(handle). So closing is readable even after the handle is gone.
  • Ref-count discipline balanced across send / close / is_closing (correct increment_strong_count + from_raw idiom, exactly one from_raw per into_raw). Correctly Arc not Rc. Double-close idempotent via the swap guard.
  • Layout: state field sits in the private region on both unix and windows variants with matching _padding subtraction, and the sizes test asserts against libuv_sys_lite::uv_async_t per-platform, so drift fails the test / underflow fails to compile.
  • The added ref_op/unref_op around 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
bartlomieju merged commit 2652840 into denoland:main Jul 20, 2026
173 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants