Repository navigation
Detach a Rust JSG resource's Rcs before its Drop runs - #8403
Conversation
There was a problem hiding this comment.
Superseded by the review of
c90f16bb.
This review, of 5482a236
💬 Approved with comments — The fix is correct and well tested. The only note is a small optional suggestion to update the may_change_in doc comment.
Findings: 1 suggestion (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 suggestion
The change is sound. One small doc-comment gap.
Recommendation: 💬 Optional Nits
- [SUGGESTION] The
may_change_indoc comment does not describe the newDetachForDroprule (src/rust/jsg/v8.rs:5041)
View full review
The detach-before-drop logic is correct. TracedGlobals are skipped during a drop visit, which avoids reviving dead weak values during a sweep. Rcs and C++ Refs are changed only under their own isolate's lock. The Probe autoref check behind has_drop_impl resolves on a concrete Self, and a test covers it. One optional note: the may_change_in doc comment does not yet describe the new drop-visit rule.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found.
📖 AGENTS.md: ✅ LGTM
No issues found.
⚡ Performance: ✅ LGTM
The extra trace walk runs only for resources with their own Drop impl, once per drop, and has_drop_impl is resolved at compile time.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 5482a236
5482a23 to
c90f16b
Compare
There was a problem hiding this comment.
Superseded by the review of
388e1c8b.
This review, of c90f16bb
✅ Approved — The earlier suggestion is fixed. The detach-before-drop logic is correct and tested, and the code changed since the last review has no new issues.
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
No security issues found.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
The may_change_in doc comment now describes the drop-visit rule: a reference is changed only when this thread holds the lock of its isolate. That matches the code.
DetachForDrop is handled in is_detaching, may_change_in and visit_traced_global. Rc::detach is safe to call twice. The has_drop_impl probe is covered by a test.
⚡ Performance: ✅ LGTM
The extra trace walk runs once per drop, and only for resources that have their own Drop impl. The check behind it is resolved at compile time.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: c90f16bb
There was a problem hiding this comment.
Superseded by the review of
c990924c.
This review, of 388e1c8b
✅ Approved — The earlier suggestion is fixed. The detach-before-drop logic is correct and tested, and the code changed since the last review has no new issues.
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
No security issues found.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
The may_change_in doc comment now describes the drop-visit rule: a reference is changed only when this thread holds the lock of its isolate. That matches the code.
DetachForDrop is handled in is_detaching, may_change_in and visit_traced_global. Rc::detach is safe to call twice. The has_drop_impl probe is covered by a test.
⚡ Performance: ✅ LGTM
The extra trace walk runs once per drop, and only for resources that have their own Drop impl. The check behind it is resolved at compile time.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 388e1c8b — same changes as c90f16bb, so this is that commit's review (no new review was run; line numbers are as of c90f16bb)
guybedford
left a comment
There was a problem hiding this comment.
LGTM, I checked locally that rc_moved_out_in_drop_can_be_reowned fails with the detach_for_drop call removed, and passes with it. A few nits inline.
An idea for a follow-on: the remaining hole is a Drop impl on a field's type (a nested struct holding an Rc) that moves the Rc out. The resource itself has no Drop, so nothing detaches it. This could be closed by turning has_drop_impl into a compile-time value over the field tree, something like const NEEDS_DROP_DETACH: bool on Traced, set to "Self has its own Drop || any traced field's NEEDS_DROP_DETACH", with the derive / jsg_traced! macros using the same probe for field types. That closes the nested case and still costs nothing for types that don't need it. A TracedGlobal moved out in Drop can stay a documented rule.
AI generated under my review
A resource's `Drop` can move a `jsg::Rc` out of a field the GC has visited. The moved `Rc` stays weak and keeps the dead resource as its parent, so its next owner's visit fails `KJ_ASSERT(&p == &visitor.parent)` in `Wrappable::visitRef`. In this patch `wrappable_invoke_drop` makes the resource's `Rc`s strong and parentless before its `Drop` runs, as moving a C++ `jsg::Ref` does. It only does this for resources with their own `Drop` impl.
388e1c8 to
c990924
Compare
There was a problem hiding this comment.
✅ Approved — The code changed since the last review has no new issues. The approver's nits are addressed, and the detach-before-drop logic is correct and covered by tests for both the strong and the weak moved-Rc paths.
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
No security issues found.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
DetachForDrop is handled in is_detaching, may_change_in and visit_traced_global, and Rc::detach is safe to call twice. The has_drop_impl macro probe correctly relies on an inherent method taking precedence over the trait's default method. The tests cover the strong and the weak moved-Rc paths, a Drop that keeps its child, and the probe itself. The docs on has_drop_impl and the cost note on detach_for_drop match the code.
⚡ Performance: ✅ LGTM
The extra trace walk runs once per drop, and only for resources that have their own Drop impl. The has_drop_impl check is resolved at compile time. The quadratic cost for a chain of resources with Drop impls is documented on detach_for_drop.
📝 Documentation: ✅ LGTM
No documentation issues found in the code changed since the last review.
Reviewed commit: c990924c
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.
A resource's
Dropcan move ajsg::Rcout of a field the GC has visited. The movedRcstays weak and keeps the dead resource as its parent, so its next owner's visit failsKJ_ASSERT(&p == &visitor.parent)inWrappable::visitRef.In this patch
wrappable_invoke_dropmakes the resource'sRcs strong and parentless before itsDropruns, as moving a C++jsg::Refdoes. It only does this for resources with their ownDropimpl.