Skip to content

Let Rust JSG code ask whether a TracedGlobal is empty - #8659

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-19-rjsg-traced-is-empty
Nov 20, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-19-rjsg-traced-is-empty

Conversation

@littledivy

@littledivy littledivy commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

A resource's Drop may keep a TracedGlobal it moves out of a field (#8600), but whether that handle still has a value depends on which GC collected the resource's wrapper. If the resource was the value's only holder, a scavenge leaves the handle live, while a full GC finds the value dead in the same pause and empties the handle, so as_local panics on it. Code keeping such a handle had no way to tell the two apart.

In this patch TracedGlobal gains is_empty(lock) and try_as_local(lock), which returns None for an empty handle. Both check the isolate and read the slot under the lock, as as_local does. The README's Drop rule says when the handle is empty and points to them. The drop_moved_traced_global tests now check that the handle is empty after the full GC path and live after the scavenge path.

@littledivy
littledivy requested review from a team as code owners November 19, 2026 07:51

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

This review, of b560369c

✅ Approved — No correctness, security, performance or documentation problems were found in the new TracedGlobal emptiness API, its README rule or its tests.

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 issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

The README's Drop rule and its 'Tested by' paragraph match the new API and tests.


Reviewed commit: b560369c

A resource's `Drop` may keep a `TracedGlobal` it moves out of a field, but whether that handle still has a value depends on which GC collected the resource's wrapper. If the resource was the value's only holder, a scavenge leaves the handle live, while a full GC finds the value dead in the same pause and empties the handle, so `as_local` panics on it. Code keeping such a handle had no way to tell the two apart.

In this patch `TracedGlobal` gains `is_empty(lock)` and `try_as_local(lock)`, which returns `None` for an empty handle. Both check the isolate and read the slot under the lock, as `as_local` does. The README's `Drop` rule says when the handle is empty and points to them. The drop_moved_traced_global tests now check that the handle is empty after the full GC path and live after the scavenge path.
@littledivy
littledivy force-pushed the littledivy/2026-11-19-rjsg-traced-is-empty branch from b560369 to ff14e2d Compare November 19, 2026 15:25

@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 — I found no correctness or documentation problems in the new TracedGlobal::is_empty and try_as_local methods, the README's Drop rule or the updated tests.

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


✅ All checks passed

📋 Code Quality: ✅ LGTM

No issues found.

  • is_empty checks the isolate and reads the strong slot the same way as_local does. Treating ptr == 0 as empty matches the null value the file already uses for an emptied handle.
  • try_as_local checks for an empty handle before it builds a Local, so it never reaches global_to_local with a null handle.
  • The tests check both outcomes: the full-GC path leaves the handle empty and the scavenge path leaves it live.

📝 Documentation: ✅ LGTM

No issues found. The README's Drop rule and the doc comments match the new API and how it behaves.


Reviewed commit: ff14e2d8

@guybedford

Copy link
Copy Markdown
Member

Hmm I'm actually rethinking this direction a bit - is there any way we could rather just say that types that hold GC references must have no destructor?

@littledivy
littledivy merged commit b3fd7cd into main Nov 20, 2026
23 of 25 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-19-rjsg-traced-is-empty branch November 20, 2026 17:11
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