Repository navigation
Detach a Rust JSG resource's Rcs before a field's Drop runs - #8466
Conversation
There was a problem hiding this comment.
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 forwardneeds_drop_detach, soFunctionData<ReactionData<T>>never detaches before drop and promise-reaction data can still hit thevisitRefassertion. (src/rust/jsg/v8.rs:2103)
View full review
FunctionData<T>::detach_before_dropnow returnsT::needs_drop_detach(). But the promise reactions registered bythen,catchandthen_or_catchuseFunctionData<ReactionData<T>>, andimpl<T: Traced> Traced for ReactionData<T>(src/rust/jsg/promise.rs:180-184) keeps the defaultfalse. Suppose reaction data holds a type whoseDropmoves ajsg::Rcout, and the promise never settles. When the function wrapper is collected, the data is dropped without being detached. The movedRcstays weak with a dead parent, and its next owner's visit failsKJ_ASSERT(&p == &visitor.parent). Fix: forwardT::needs_drop_detach()inReactionData's impl.Arguments<T>(src/rust/jsg/arguments.rs:90-94) is aVecwrapper that forwardstracebut not this method, so it should forward it as well.- The new test covers
Option, a plain struct andVec<Rc>. It does not cover theFunctionData/Opaqueforwarding, 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_traceresource's ownTraced::needs_drop_detachmust cover its fields. (src/rust/jsg/README.md:214)
View full review
src/rust/jsg/README.md:213-215says 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 ownTraced::needs_drop_detach()(drop_detach_typesinjsg-macros/resource.rs) and does not look at its fields. Mention that acustom_traceresource'sTracedimpl must returntruewhen 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
590cb9c to
81ec0e3
Compare
There was a problem hiding this comment.
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 forwardTraced::needs_drop_detach. A resource,FunctionDataorOpaquethat holdsArguments<T>is never detached before drop, even whenT'sDropmoves ajsg::Rcout, so the originalvisitRefassertion can still fire. ForwardT::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
81ec0e3 to
15e138b
Compare
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
guybedford
left a comment
There was a problem hiding this comment.
The opt-in and the forwarding look right to me. One case still reaches the visitRef assertion using only generated code; details inline.
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.
15e138b to
f2d99f4
Compare
There was a problem hiding this comment.
✅ 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
Tracedimpl:generate_traced_impl(src/rust/jsg-macros/resource.rs:601-615) now emits aneeds_drop_detachthat returns whetherSelfimplementsDrop. 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 ownDropcheck with the answers from its field types. - Test:
rc_moved_out_in_an_inline_resource_drop_can_be_reownedcovers the inline-resource case. - Forwarding: the wrapper and collection impls forward the method.
RcandWeakkeep the defaultfalse.
⚡ 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
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.
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.
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.
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.
#8403 detaches a resource's
Rcs before it is dropped only when the resource has its ownDropimpl. ADropimpl on a field's type can move ajsg::Rcout just the same, and the movedRcstays weak with the dead resource as its parent, so its next owner's visit failsKJ_ASSERT(&p == &visitor.parent)inWrappable::visitRef.In this patch
Tracedgetsneeds_drop_detach(), false by default. Wrappers and collections return their contents', a hand-written impl for a type with its ownDropreturns true, and#[jsg_resource]detaches before drop when the resource implementsDropor a field's type needs it. The check is a compile-time constant per type, so resources without such a field pay nothing.