deno
5fc4ac99 - fix(ext/napi): defer GC weak-callback finalizers to the event loop (#33260)

Commit
101 days ago
fix(ext/napi): defer GC weak-callback finalizers to the event loop (#33260) ## Summary - Defer napi Reference weak-callback finalizers from V8 GC to the next event loop tick via `V8CrossThreadTaskSpawner`, matching Node.js's `EnqueueFinalizer` pattern - Implement `node_api_post_finalizer` (was a no-op) - Add regression test verifying finalizers run after GC, not during ## Background Deep comparison of Node-API implementations between Node.js and Deno revealed that Deno was calling user-provided finalizer callbacks **directly during V8 GC pauses**. This is unsafe — finalizers can re-enter V8, allocate GC'd objects, or trigger cascading weak callbacks, all of which are undefined behavior during a GC pause. Node.js avoids this by deferring finalizers via `EnqueueFinalizer` to the event loop. This is the most likely root cause of the intermittent `napi_unwrap` failures reported in #33137 (Vite 8 / rolldown builds). The timing-dependent nature of the bug matches: it only manifests when GC fires at a particular moment during napi class instance lifecycle. Ref #33137 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
Parents
Loading