Skip to content

fix(napi): use Arc in Reference because it is Send - #2668

Merged
Brooooooklyn merged 1 commit into
mainfrom
05-28-fix_napi_use_arc_in_reference_because_it_is_send
May 29, 2025
Merged

fix(napi): use Arc in Reference because it is Send#2668
Brooooooklyn merged 1 commit into
mainfrom
05-28-fix_napi_use_arc_in_reference_because_it_is_send

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

No description provided.

Copy link
Copy Markdown
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@Brooooooklyn
Brooooooklyn merged commit 29ae3ab into main May 29, 2025
@Brooooooklyn
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>
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.

1 participant