fix(napi): use Arc in Reference because it is Send - #2668
Merged
Brooooooklyn merged 1 commit intoMay 29, 2025
Conversation
Brooooooklyn
deleted the
05-28-fix_napi_use_arc_in_reference_because_it_is_send
branch
May 29, 2025 01:12
Brooooooklyn
added a commit
that referenced
this pull request
Jun 9, 2026
…fusion) (#3313) The `finalize_callbacks` allocation backing `Reference<T>` was created with `Rc::into_raw` (callback_info.rs `_construct`/`_factory`, class.rs) and reclaimed with `Rc::from_raw` in the GC finalizer (mod.rs), yet reconstructed with `Arc::from_raw` in `Reference::from_value_ptr` and `add_ref` (value_ref.rs). Since #2668 changed the `Reference` field to `Arc<Cell<..>>` and added `unsafe impl Sync`, the same heap allocation was being used as both `Rc` and `Arc` — undefined behavior: mismatched `RcBox`/`ArcInner` layout assumptions, mixed atomic/non-atomic refcount access, and a deallocation path that assumes `ArcInner` for an `RcBox` allocation. The reported impact is a process abort (DoS) under repeated `Reference<T>` conversions plus GC. Reverting to `Rc` is unsound because `Reference`'s `unsafe impl Sync` requires atomic refcounting. This completes the #2668 refactor by switching the three allocation sites and the finalizer to `Arc`. The `#[allow(clippy:: arc_with_non_send_sync)]` matches the existing precedent in threadsafe_function.rs: the inner `Cell` is only mutated on the JS thread via `Env`, as documented on `Reference`. Reported-by: Thanasis Trispiotis Co-authored-by: Claude Opus 4.8 (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.

No description provided.