Skip to content

Make a Rust JSG TracedGlobal moved out in Drop strong - #8600

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-18-rjsg-drop-traced-global
Nov 19, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-18-rjsg-drop-traced-global

Conversation

@littledivy

@littledivy littledivy commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

A resource's Drop can move a TracedGlobal out of a field the GC has put in traced mode. The drop visit from #8403 made the resource's jsg::Rcs strong first but skipped its TracedGlobals, so the moved one stayed weak with nothing tracing it. Once nothing else held its value, the next GC collected it and as_local panicked with "C++ passed an empty v8::Local".

In this patch the drop visit also clears the traced reference and makes the strong handle strong, as moving a C++ jsg::V8Ref does. This is safe during the sweep: V8 resets the weak handles of dead values before cppgc starts sweeping, so a weak handle that isn't empty still points to a live value, and an empty one stays empty. The new drop_moved_traced_global tests cover a value held elsewhere, a value only the resource holds whose wrapper a scavenge drops, and a value that died with the resource. The first two fail without the fix.

@littledivy
littledivy requested review from a team as code owners November 18, 2026 08:30

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

This review, of 2aed3447

✅ Approved — The drop visit now makes a moved TracedGlobal strong and leaves the other visit modes as they were. A test covers both the live and the dead value, and no 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

No security concerns in this change.


📖 AGENTS.md: ✅ LGTM

No problems with the agent guidance files.


📋 Code Quality: ✅ LGTM

visit_traced_global now uses may_change_in and TracedGlobal::let_go_of_traced, and the detach, GC and first-strong-reference visits behave as they did before. The drop visit makes a moved TracedGlobal strong only under its isolate's lock. global_clear_weak skips empty handles, so a handle whose value is already dead stays empty during the sweep. The new tests cover both the live and the dead value.


⚡ Performance: ✅ LGTM

No performance concerns. The drop visit adds the same constant-time work per handle that the detach visit already does.


📝 Documentation: ✅ LGTM

The safety-rule text in README.md and the doc comments in v8.rs describe the new drop-visit behaviour and the new test.


Reviewed commit: 2aed3447

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

This review, of d2412476

✅ Approved — The drop visit now makes a moved TracedGlobal strong, and a test covers both the live and the dead value. The merge from main changed only the test module registration, and nothing new turned up.

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 concerns in this change.


📖 AGENTS.md: ✅ LGTM

No problems with the agent guidance files.


📋 Code Quality: ✅ LGTM

visit_traced_global makes a moved TracedGlobal strong under its isolate's lock, and the other visit modes behave as they did before. The new test module is registered in mod.rs.


⚡ Performance: ✅ LGTM

The drop visit adds only constant-time work per handle.


📝 Documentation: ✅ LGTM

The README.md safety rules and the doc comments in v8.rs describe how the drop visit now works.


Reviewed commit: d2412476

@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 strengthening in the drop visit is sound. V8 nulls a dead weak handle's slot in the same pause that finds its value dead, which is before the cppgc sweep on a full GC and before the epilogue detach on a scavenge, so a non-empty handle seen by Drop always points at a live value. I confirmed locally that traced_global_moved_out_in_drop_keeps_its_value fails without the fix.

Non-blocking comments inline. Nit: the branch has a merge commit from main; a rebase would keep the history clean.

Comment thread src/rust/jsg/README.md Resolved
Comment thread src/rust/jsg/v8.rs Resolved

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

This review, of 485217dd

✅ Approved — No issues found. The commits since the last review add the scavenge test that was asked for and fix the GC-visit assert label, and the drop-visit fix is unchanged.

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 drop visit makes a moved handle strong only under its isolate's lock. A handle whose value is dead is already empty, so making it strong cannot revive a freed value.


📖 AGENTS.md: ✅ LGTM

The TracedGlobal guidance in src/rust/AGENTS.md still matches the code: letting go of the traced reference includes ClearWeak.


📋 Code Quality: ✅ LGTM

visit_traced_global behaves as before in every visit mode, and the GC-visit assert label now names only TracedGlobal. The new scavenge test uses existing helpers and runs serialised with the other tests in its module.


⚡ Performance: ✅ LGTM

The drop visit adds only constant-time work per handle.


📝 Documentation: ✅ LGTM

The README.md safety rules and the doc comments in v8.rs describe the drop visit as it now works. Letting go of a traced reference includes making the strong handle strong (let_go_of_traced), so the README's description of a dropped TracedGlobal is still accurate.


Reviewed commit: 485217dd

A resource's `Drop` can move a `TracedGlobal` out of a field the GC has
put in traced mode. The drop visit made the resource's `jsg::Rc`s strong
first but skipped its `TracedGlobal`s, so the moved one stayed weak with
nothing tracing it. Once nothing else held its value, the next GC
collected it and `as_local` panicked with "C++ passed an empty
v8::Local".

In this patch the drop visit also clears the traced reference and makes
the strong handle strong, as moving a C++ `jsg::V8Ref` does. This is
safe during the sweep: V8 resets the weak handles of dead values before
cppgc starts sweeping, so a weak handle that isn't empty still points to
a live value, and an empty one stays empty. `visit_traced_global` now
uses `may_change_in` to decide whether it may touch the handle, as
`visit_rc` does.
@littledivy
littledivy force-pushed the littledivy/2026-11-18-rjsg-drop-traced-global branch from 485217d to 6102993 Compare November 19, 2026 07:34

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

This review, of 61029933

✅ Approved — No issues found. The commits since the last review add the scavenge test that was asked for and fix the GC-visit assert label, and the drop-visit fix is unchanged.

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 drop visit makes a moved handle strong only under its isolate's lock. A handle whose value is dead is already empty, so making it strong cannot revive a freed value.


📖 AGENTS.md: ✅ LGTM

The TracedGlobal guidance in src/rust/AGENTS.md still matches the code: letting go of the traced reference includes ClearWeak.


📋 Code Quality: ✅ LGTM

visit_traced_global behaves as before in every visit mode, and the GC-visit assert label now names only TracedGlobal. The new scavenge test uses existing helpers and runs serialised with the other tests in its module.


⚡ Performance: ✅ LGTM

The drop visit adds only constant-time work per handle.


📝 Documentation: ✅ LGTM

The README.md safety rules and the doc comments in v8.rs describe the drop visit as it now works. Letting go of a traced reference includes making the strong handle strong (let_go_of_traced), so the README's description of a dropped TracedGlobal is still accurate.


Reviewed commit: 61029933 — same changes as 485217dd, so this is that commit's review (no new review was run; line numbers are as of 485217dd)

@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 — No issues found. The commits since the last review add the scavenge test that was asked for and fix the GC-visit assert label, and the drop-visit fix is unchanged.

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 drop visit makes a moved handle strong only under its isolate's lock. A handle whose value is dead is already empty, so making it strong cannot revive a freed value.


📖 AGENTS.md: ✅ LGTM

The TracedGlobal guidance in src/rust/AGENTS.md still matches the code: letting go of the traced reference includes ClearWeak.


📋 Code Quality: ✅ LGTM

visit_traced_global behaves as before in every visit mode, and the GC-visit assert label now names only TracedGlobal. The new scavenge test uses existing helpers and runs serialised with the other tests in its module.


⚡ Performance: ✅ LGTM

The drop visit adds only constant-time work per handle.


📝 Documentation: ✅ LGTM

The README.md safety rules and the doc comments in v8.rs describe the drop visit as it now works. Letting go of a traced reference includes making the strong handle strong (let_go_of_traced), so the README's description of a dropped TracedGlobal is still accurate.


Reviewed commit: 366627f0 — same changes as 485217dd, so this is that commit's review (no new review was run; line numbers are as of 485217dd)

@littledivy
littledivy merged commit c9fa78a into main Nov 19, 2026
26 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-18-rjsg-drop-traced-global branch November 19, 2026 15:19
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