Skip to content

Detach a Rust JSG resource's Rcs before its Drop runs - #8403

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-12-rjsg-drop-moved-rc
Nov 14, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-12-rjsg-drop-moved-rc

Conversation

@littledivy

Copy link
Copy Markdown
Member

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 Rcs 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.

@littledivy
littledivy requested review from a team as code owners November 12, 2026 16:54

@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 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_in doc comment does not describe the new DetachForDrop rule (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

Comment thread src/rust/jsg/v8.rs Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-12-rjsg-drop-moved-rc branch from 5482a23 to c90f16b Compare November 12, 2026 17:06

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

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

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

Comment thread src/rust/jsg/lib.rs Resolved
Comment thread src/rust/jsg/v8.rs Resolved
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.
@littledivy
littledivy force-pushed the littledivy/2026-11-12-rjsg-drop-moved-rc branch from 388e1c8 to c990924 Compare November 13, 2026 18:10

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

@littledivy
littledivy merged commit 5c34f50 into main Nov 14, 2026
35 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-12-rjsg-drop-moved-rc branch November 14, 2026 04:28
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.

2 participants