Skip to content

Detach a Rust JSG resource's Rcs before a field's Drop runs - #8466

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-14-rjsg-drop-detach-nested
Nov 17, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-14-rjsg-drop-detach-nested

Conversation

@littledivy

Copy link
Copy Markdown
Member

#8403 detaches a resource's Rcs before it is dropped only when the resource has its own Drop impl. A Drop impl on a field's type can move a jsg::Rc out just the same, and the moved Rc stays weak with the dead resource as its parent, so its next owner's visit fails KJ_ASSERT(&p == &visitor.parent) in Wrappable::visitRef.

In this patch Traced gets needs_drop_detach(), false by default. Wrappers and collections return their contents', a hand-written impl for a type with its own Drop returns true, and #[jsg_resource] detaches before drop when the resource implements Drop or a field's type needs it. The check is a compile-time constant per type, so resources without such a field pay nothing.

@littledivy
littledivy requested review from a team as code owners November 14, 2026 05:31

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 81ec0e34.

This review, of 2982e795

💬 Approved with comments — One WARNING: ReactionData<T> does not forward needs_drop_detach, so promise-reaction data can still hit the original assertion. There is also one documentation note about custom_trace resources.

Findings: 1 warning · 1 info (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8466.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Code Quality] `ReactionData<T>` does not forward `needs_drop_detach`, so `FunctionData<ReactionData<T>>` never detaches before drop and promise-reaction data can still hit the `visitRef` assertion.
- **File:** src/rust/jsg/v8.rs:2103

## INFO (1)

### [Documentation] README does not say that a `custom_trace` resource's own `Traced::needs_drop_detach` must cover its fields.
- **File:** src/rust/jsg/README.md:214

📋 Code Quality: ⚠️ 1 warning

ReactionData<T> (and Arguments<T>) do not forward needs_drop_detach, so the new FunctionData check is always false for promise reactions.

Recommendation: 📝 Minor Comments

  • [WARNING] ReactionData<T> does not forward needs_drop_detach, so FunctionData<ReactionData<T>> never detaches before drop and promise-reaction data can still hit the visitRef assertion. (src/rust/jsg/v8.rs:2103)
View full review
  • FunctionData<T>::detach_before_drop now returns T::needs_drop_detach(). But the promise reactions registered by then, catch and then_or_catch use FunctionData<ReactionData<T>>, and impl<T: Traced> Traced for ReactionData<T> (src/rust/jsg/promise.rs:180-184) keeps the default false. Suppose reaction data holds a type whose Drop moves a jsg::Rc out, and the promise never settles. When the function wrapper is collected, the data is dropped without being detached. The moved Rc stays weak with a dead parent, and its next owner's visit fails KJ_ASSERT(&p == &visitor.parent). Fix: forward T::needs_drop_detach() in ReactionData's impl. Arguments<T> (src/rust/jsg/arguments.rs:90-94) is a Vec wrapper that forwards trace but not this method, so it should forward it as well.
  • The new test covers Option, a plain struct and Vec<Rc>. It does not cover the FunctionData/Opaque forwarding, which is where this gap is.

📝 Documentation: 💬 1 info

The README does not say that custom_trace resources must report field drop-detach needs through their own Traced impl.

Recommendation: 💬 Optional Nits

  • [INFO] README does not say that a custom_trace resource's own Traced::needs_drop_detach must cover its fields. (src/rust/jsg/README.md:214)
View full review
  • src/rust/jsg/README.md:213-215 says a resource detaches when a field's type says it needs it. With #[jsg_resource(custom_trace)], though, the macro consults only the resource's own Traced::needs_drop_detach() (drop_detach_types in jsg-macros/resource.rs) and does not look at its fields. Mention that a custom_trace resource's Traced impl must return true when one of its fields needs it.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found in this change.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues.


⚡ Performance: ✅ LGTM

The detach check should resolve to a constant per type, and the new trait methods are trivial.


Reviewed commit: 2982e795

Comment thread src/rust/jsg/v8.rs Resolved
Comment thread src/rust/jsg/README.md Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-14-rjsg-drop-detach-nested branch 2 times, most recently from 590cb9c to 81ec0e3 Compare November 14, 2026 05:38

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 15e138ba.

This review, of 81ec0e34

💬 Approved with comments — One WARNING is still open: Arguments<T> keeps the default needs_drop_detach() == false, so a type that holds one can still hit the original visitRef assertion. The ReactionData and documentation points from the last review are fixed.

Findings: 1 warning

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8466.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Code Quality] `Arguments<T>` does not forward `Traced::needs_drop_detach`. A resource, `FunctionData` or `Opaque` that holds `Arguments<T>` is never detached before drop, even when `T`'s `Drop` moves a `jsg::Rc` out, so the original `visitRef` assertion can still fire. Forward `T::needs_drop_detach()` in that impl.
- **File:** src/rust/jsg/arguments.rs:90

📋 Code Quality: ⚠️ 1 warning

ReactionData<T> is fixed. Arguments<T> still does not forward needs_drop_detach.

Recommendation: 📝 Minor Comments

  • [WARNING] Arguments<T> does not forward Traced::needs_drop_detach. A resource, FunctionData or Opaque that holds Arguments<T> is never detached before drop, even when T's Drop moves a jsg::Rc out, so the original visitRef assertion can still fire. Forward T::needs_drop_detach() in that impl. (src/rust/jsg/arguments.rs:90)
View full review

The ReactionData<T> gap is fixed: ReactionData<T> now forwards T::needs_drop_detach() (src/rust/jsg/promise.rs:185), and FunctionData/Opaque use it through detach_before_drop.

Still open: impl<T: Traced> Traced for Arguments<T> (src/rust/jsg/arguments.rs:90-94) forwards trace but not needs_drop_detach, so it returns the default false. Its sibling wrappers (Vec, Option, Nullable, Cell and the rest) all forward the method in this pull request. Suppose a resource field, or the data of a FunctionData or Opaque, is an Arguments<T> whose T has a Drop that moves a jsg::Rc out. The owner is not detached before it is dropped, the moved Rc keeps the dead resource as its parent, and its next owner's visit fails KJ_ASSERT(&p == &visitor.parent). Fix: add fn needs_drop_detach() -> bool { T::needs_drop_detach() } to that impl.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found. The change only decides when GC references are detached before a drop. It adds no input surface and no new unsafe pointer handling.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues.


⚡ Performance: ✅ LGTM

The detach check resolves to a compile-time constant per type, and the new trait methods are trivial.


📝 Documentation: ✅ LGTM

src/rust/jsg/README.md:215-218 now says that the hand-written Traced impl of a custom_trace resource returns true from needs_drop_detach when one of its fields' types needs it.


Reviewed commit: 81ec0e34

@littledivy
littledivy force-pushed the littledivy/2026-11-14-rjsg-drop-detach-nested branch from 81ec0e3 to 15e138b Compare November 14, 2026 05:43

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of f2d99f47.

This review, of 15e138ba

✅ Approved — The last open warning is fixed: Arguments<T> now forwards needs_drop_detach. No new issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only decides when GC references are detached before a drop. It adds no input surface and no new unsafe code.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues.


📋 Code Quality: ✅ LGTM

impl<T: Traced> Traced for Arguments<T> now forwards needs_drop_detach to T (src/rust/jsg/arguments.rs:95-97).

Every wrapper and collection impl now forwards the method: Option, Nullable, Vec, the map and set types, Cell, RefCell, ReactionData and Arguments. Rc, Weak and the types bounded by HoldsNoGcReferences keep the default false, which is correct because they hold no field that could need a detach. FunctionData and Opaque both decide whether to detach from T::needs_drop_detach().


⚡ Performance: ✅ LGTM

Whether to detach is a compile-time constant for each type, and the new trait methods are trivial.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 15e138ba

@codecov-commenter

codecov-commenter commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 19.29825% with 46 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.43%. Comparing base (5c34f50) to head (f2d99f4).
⚠️ Report is 45 commits behind head on main.

Files with missing lines Patch % Lines
src/rust/jsg/wrappable.rs 13.15% 33 Missing ⚠️
src/rust/jsg/cell.rs 0.00% 6 Missing ⚠️
src/rust/jsg/arguments.rs 0.00% 3 Missing ⚠️
src/rust/jsg/opaque.rs 0.00% 3 Missing ⚠️
src/rust/jsg/lib.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8466      +/-   ##
==========================================
- Coverage   66.54%   66.43%   -0.11%     
==========================================
  Files         783      794      +11     
  Lines      201145   203032    +1887     
  Branches    31476    31536      +60     
==========================================
+ Hits       133847   134880    +1033     
- Misses      48860    49691     +831     
- Partials    18438    18461      +23     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@guybedford guybedford 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.

The opt-in and the forwarding look right to me. One case still reaches the visitRef assertion using only generated code; details inline.

Comment thread src/rust/jsg-macros/resource.rs Resolved
A resource's `Rc`s are detached before it is dropped only when the
resource has its own `Drop` impl. A `Drop` impl on a field's type can
move a `jsg::Rc` out just the same, and the moved `Rc` stays weak with
the dead resource as its parent, so its next owner's visit fails
`KJ_ASSERT(&p == &visitor.parent)` in `Wrappable::visitRef`.

In this patch `Traced` gets `needs_drop_detach()`, false by default.
Wrappers and collections return their contents', a hand-written impl
for a type with its own `Drop` returns true, and `#[jsg_resource]`
detaches before drop when the resource implements `Drop` or a field's
type needs it, and a `#[jsg_resource]` held inline in another says
whether it implements `Drop`. `GarbageCollected::has_drop_impl` becomes
`detach_before_drop`. The check is a compile-time constant per type,
so resources without such a field pay nothing.
@littledivy
littledivy force-pushed the littledivy/2026-11-14-rjsg-drop-detach-nested branch from 15e138b to f2d99f4 Compare November 17, 2026 03:38

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — The case raised in the inline comment, a resource with its own Drop held inline in another resource, is now detached, and a test covers it. No other issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only decides when GC references are detached before a drop. It adds no input surface and no new unsafe code.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues.


📋 Code Quality: ✅ LGTM

  • Generated Traced impl: generate_traced_impl (src/rust/jsg-macros/resource.rs:601-615) now emits a needs_drop_detach that returns whether Self implements Drop. It does not look at fields, so a recursive type cannot loop.
  • Outer resource: detach_before_drop (src/rust/jsg-macros/resource.rs:693-697) combines the type's own Drop check with the answers from its field types.
  • Test: rc_moved_out_in_an_inline_resource_drop_can_be_reowned covers the inline-resource case.
  • Forwarding: the wrapper and collection impls forward the method. Rc and Weak keep the default false.

⚡ Performance: ✅ LGTM

Whether to detach is a compile-time constant for each type. Resources with no field that needs the detach pay nothing.


📝 Documentation: ✅ LGTM

The README and the Traced::needs_drop_detach doc comment match the behaviour of this change. The claim that #[jsg_struct] and #[jsg_oneof] types can't implement Drop holds: their generated ToJS::to_js moves fields or variants out of self, which Rust does not allow for a type that implements Drop.


Reviewed commit: f2d99f47

@littledivy
littledivy merged commit 155b529 into main Nov 17, 2026
36 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-14-rjsg-drop-detach-nested branch November 17, 2026 05:15
littledivy added a commit that referenced this pull request Nov 20, 2026
A resource that holds GC references can't implement Drop any more, so
nothing moves a reference out of a dropping resource's fields. The
machinery that made a dropping resource's references strong and
parentless first, for its own Drop (#8403), a field type's Drop (#8466)
and its TracedGlobals (#8600), has nothing left to do.

In this patch VisitMode::DetachForDrop, GcVisitor::detach_for_drop,
GarbageCollected::detach_before_drop, Traced::needs_drop_detach and its
forwarding impls, and the macro's Drop probes go away.
wrappable_invoke_drop drops the resource without visiting it first, and
GC and detaching visits no longer check for a drop visit.
TracedGlobal::let_go_of_traced stays, since GC and detaching visits use
it.
littledivy added a commit that referenced this pull request Nov 20, 2026
A resource that holds GC references can't implement Drop any more, so
nothing moves a reference out of a dropping resource's fields. The
machinery that made a dropping resource's references strong and
parentless first, for its own Drop (#8403), a field type's Drop (#8466)
and its TracedGlobals (#8600), has nothing left to do.

In this patch VisitMode::DetachForDrop, GcVisitor::detach_for_drop,
GarbageCollected::detach_before_drop, Traced::needs_drop_detach and its
forwarding impls, and the macro's Drop probes go away.
wrappable_invoke_drop drops the resource without visiting it first, and
GC and detaching visits no longer check for a drop visit.
TracedGlobal::let_go_of_traced stays, since GC and detaching visits use
it.
littledivy added a commit that referenced this pull request Nov 20, 2026
A resource that holds GC references can't implement Drop any more, so
nothing moves a reference out of a dropping resource's fields. The
machinery that made a dropping resource's references strong and
parentless first, for its own Drop (#8403), a field type's Drop (#8466)
and its TracedGlobals (#8600), has nothing left to do.

In this patch VisitMode::DetachForDrop, GcVisitor::detach_for_drop,
GarbageCollected::detach_before_drop, Traced::needs_drop_detach and its
forwarding impls, and the macro's Drop probes go away.
wrappable_invoke_drop drops the resource without visiting it first, and
GC and detaching visits no longer check for a drop visit.
TracedGlobal::let_go_of_traced stays, since GC and detaching visits use
it. TracedGlobal::is_empty and try_as_local (#8659) go too: they were
for a handle that a resource's Drop moved out, which can't happen any
more.
littledivy added a commit that referenced this pull request Nov 21, 2026
A resource that holds GC references can't implement Drop any more, so
nothing moves a reference out of a dropping resource's fields. The
machinery that made a dropping resource's references strong and
parentless first, for its own Drop (#8403), a field type's Drop (#8466)
and its TracedGlobals (#8600), has nothing left to do.

In this patch VisitMode::DetachForDrop, GcVisitor::detach_for_drop,
GarbageCollected::detach_before_drop, Traced::needs_drop_detach and its
forwarding impls, and the macro's Drop probes go away.
wrappable_invoke_drop drops the resource without visiting it first, and
GC and detaching visits no longer check for a drop visit.
TracedGlobal::let_go_of_traced stays, since GC and detaching visits use
it. TracedGlobal::is_empty and try_as_local (#8659) go too: they were
for a handle that a resource's Drop moved out, which can't happen any
more.
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.

3 participants