Skip to content

Refuse to compile a Rust JSG resource that holds GC references and implements Drop - #8713

Merged
littledivy merged 3 commits into
mainfrom
littledivy/2026-11-20-rjsg-no-drop-gc-refs
Nov 21, 2026
Merged

littledivy merged 3 commits into
mainfrom
littledivy/2026-11-20-rjsg-no-drop-gc-refs

Conversation

@littledivy

@littledivy littledivy commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

A resource's Drop has &mut self, so it can move a jsg::Rc or a TracedGlobal out of a field that the garbage collector made weak. Rust JSG has handled that at run time: before such a resource drops, it detached every reference it traces (#8403, #8466, #8600). No resource outside the tests needs a Drop impl while holding GC references, so it is simpler to rule the case out.

#[jsg_resource]
struct Holder {
    child: jsg::Rc<Leaf>,
}

impl Drop for Holder { // error[E0080]: `Holder` may hold GC references (a field's type isn't
    fn drop(&mut self) {} // `jsg::HoldsNoGcReferences`), so it must not implement `Drop`
}

In this patch:

  • #[jsg_resource] emits a const assert that fails when the resource implements Drop and a field's type isn't HoldsNoGcReferences. A resource whose fields hold no GC references, like MemoryCache, may still implement Drop, and so may a field's type.
  • #[jsg_struct] and #[jsg_oneof] already can't implement Drop, because their ToJS moves the fields out. New compile tests pin that down next to the resource cases.
  • The detach before Drop goes away: VisitMode::DetachForDrop, GcVisitor::detach_for_drop, GarbageCollected::detach_before_drop, Traced::needs_drop_detach and the macro's Drop probes. wrappable_invoke_drop now drops the resource without visiting it first.
  • TracedGlobal::is_empty and try_as_local (Let Rust JSG code ask whether a TracedGlobal is empty #8659) are removed. They were for a handle that a resource's Drop moved out, which can no longer happen.
  • The jsg-test resources that counted their drops in their own Drop now count them with a DropCounter field. The runtime tests for a Drop that moves a reference out are removed, since their resources no longer compile.

Rust has no negative bounds, so a hand-written type that holds GC references can still implement Drop. The README keeps that as a rule for reviewers.

//src/rust/..., //src/workerd/jsg/..., //src/workerd/io/... and //src/workerd/api/memory-cache-api/... pass (374 tests, with jsg-test_test), and each commit builds and passes on its own.

@littledivy
littledivy requested review from a team as code owners November 20, 2026 05:04

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

This review, of abffa61f

💬 Approved with comments — Sound compile-time replacement for the run-time detach before Drop, with good compile-test coverage. Only two INFO notes: the hand-written field-type case now relies on a README rule, and one README sentence is contradictory.

Findings: 2 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.


🔒 Security: 💬 1 info

The compile-time check covers a resource's own Drop; a hand-written field type's Drop is no longer detached at run time.

Recommendation: 💬 Optional Nits

  • [INFO] With the run-time detach removed, only a README rule stops a hand-written field type that holds GC references from moving one out of a field in its own Drop. (src/rust/jsg/wrappable.rs:178)
View full review

generate_drop_check checks only the resource's own Drop impl. Before this change, the run-time detach also covered a hand-written Traced field type that implements Drop and holds a jsg::Rc or TracedGlobal. That case now relies on the README rule for reviewers. The pull request description acknowledges this. A further mitigation would be a doc note on Traced itself.

The const assert emits no unsafe code, and the removed code paths shrink the unsafe surface.


📝 Documentation: 💬 1 info

One README sentence overstates the rule and is contradicted by the sentence after it.

Recommendation: 💬 Optional Nits

  • [INFO] README says a type holding GC references has no Drop impl, then says a hand-written one can. (src/rust/jsg/README.md:218)
View full review

src/rust/jsg/README.md:218-223 says a type that holds GC references "has no Drop impl". A few lines later it says nothing stops a hand-written type from having one.


✅ All other checks passed

📖 AGENTS.md: ✅ LGTM

No issues found in the changed files.


📋 Code Quality: ✅ LGTM

  • The DropCheck inherent-vs-trait const pattern resolves as intended for concrete field types.
  • The removed detach machinery has no remaining uses.
  • The test resources moved cleanly to DropCounter / WaitOnDrop fields.
  • The new compile tests cover both the rejected and the accepted cases.

⚡ Performance: ✅ LGTM

Run-time work goes down and only compile-time const checks are added.


Reviewed commit: abffa61f

Comment thread src/rust/jsg/wrappable.rs Resolved
Comment thread src/rust/jsg/README.md Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-20-rjsg-no-drop-gc-refs branch from abffa61 to 922afff Compare November 20, 2026 05:11

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

This review, of 922afff1

✅ Approved — The README contradiction from the last review is fixed and no new issues turned up. The compile-time check is a sound, conservative replacement for the run-time detach before Drop.

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 compile-time Drop check treats any field type that isn't HoldsNoGcReferences as holding GC references. The const assert emits no unsafe code, and removing the detach code paths shrinks the unsafe surface.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

Run-time work goes down: the visit before Drop and its probes are gone. The only addition is compile-time const checks.


📝 Documentation: ✅ LGTM

The README now states the Drop rule consistently: the macros enforce it for generated types, and reviewers check hand-written types.


Reviewed commit: 922afff1

@jasnell
jasnell requested a review from guybedford November 20, 2026 17:00
@littledivy
littledivy force-pushed the littledivy/2026-11-20-rjsg-no-drop-gc-refs branch from 922afff to 2fa9fac Compare November 20, 2026 17:19

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

This review, of 2fa9fac3

✅ Approved — I found no issues: the compile-time Drop check is a sound, conservative replacement for the run-time detach before Drop, and the changes since the last review keep the code and docs consistent.

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 const assert emits no unsafe code, and removing the detach paths shrinks the unsafe surface.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The __JSG_DROP_CHECK const that the macro generates matches the DropCheck, DropCheckFallback and HoldsNoGcReferences definitions in wrappable.rs. It is evaluated through const _, so the assert fires. Nothing still refers to the removed detach-for-drop code, and the test helpers that stay behind are still used.


⚡ Performance: ✅ LGTM

Run-time work goes down because the visit before Drop is gone. The only addition is compile-time const checks.


📝 Documentation: ✅ LGTM

The README states the Drop rule consistently: the macros enforce it for the types they generate, and reviewers check hand-written types.


Reviewed commit: 2fa9fac3

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

Thanks, this is the direction I was after. Approving with one thing to sort out before it lands.

Comment thread src/rust/jsg/v8.rs Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-20-rjsg-no-drop-gc-refs branch from 2fa9fac to a7926c3 Compare November 20, 2026 17: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 ddb09826.

This review, of a7926c36

✅ Approved — The only change since the last review removes TracedGlobal::is_empty/try_as_local and the pointer to them in as_local's docs, as the approver asked, and I found 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

The v8.rs change only removes code. It adds no unsafe code and no new input paths.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The v8.rs change removes TracedGlobal::is_empty, try_as_local and the pointer to them in as_local's docs. It also drops PartialEq from VisitMode; no remaining code compares modes with ==, so that is safe. The matches! check is now written as self.is_detaching(). Nothing still refers to the removed items.


⚡ Performance: ✅ LGTM

The change only removes code. The wrapper's drop path no longer visits the resource before dropping it, so run-time work goes down.


📝 Documentation: ✅ LGTM

as_local's docs no longer point at the removed APIs, and the README still states the Drop rule consistently.


Reviewed commit: a7926c36

Most jsg-test resources counted their drops in a Drop impl of their own,
and many of them hold a jsg::Rc or a TracedGlobal. A resource that holds
GC references is about to lose its Drop impl, so the counting has to
live somewhere else.

In this patch crate::DropCounter, a #[jsg_traced] field that adds one to
its static counter when it is dropped, takes over from each of those
impls, and from the two private copies of the same type in function_new
and promise. cpp_interop's Host keeps a per-thread count through a
HostDropCounter field, and gc's DisposeGate waits at its barrier from a
WaitOnDrop field. The tests count the same drops as before.
…plements Drop

Drop has &mut self, so a resource's Drop can move a jsg::Rc or a
TracedGlobal out of a field that the garbage collector made weak.
Nothing makes the moved reference strong again on its own: an Rc keeps
the dropped resource as its parent, which its next owner's trace asserts
against, and a TracedGlobal loses its value once nothing else holds it.
Rust JSG has handled this at run time, by detaching every reference
before such a resource drops. No resource outside the tests needs a Drop
impl while holding GC references.

In this patch #[jsg_resource] emits a const assert that fails when the
resource implements Drop and a field's type isn't HoldsNoGcReferences.
It asks the new hidden jsg::DropCheck<T>, whose inherent consts answer
when their bound holds and fall back to false otherwise. A resource
whose fields hold no GC references, like MemoryCache, may still
implement Drop, and so may a field's type. #[jsg_struct] and
the fields out. Compile tests cover all three, and the README's rule 2
says so. The runtime tests for a Drop that moves a reference out go,
since their resources no longer compile.

A hand-written type that holds GC references can still implement Drop,
because Rust has no negative bounds. That stays a rule for reviewers.
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
littledivy force-pushed the littledivy/2026-11-20-rjsg-no-drop-gc-refs branch from a7926c3 to ddb0982 Compare November 21, 2026 01:48

@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 only change since the last review removes TracedGlobal::is_empty/try_as_local and the pointer to them in as_local's docs, as the approver asked, and I found 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

The v8.rs change only removes code. It adds no unsafe code and no new input paths.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The v8.rs change removes TracedGlobal::is_empty, try_as_local and the pointer to them in as_local's docs. It also drops PartialEq from VisitMode; no remaining code compares modes with ==, so that is safe. The matches! check is now written as self.is_detaching(). Nothing still refers to the removed items.


⚡ Performance: ✅ LGTM

The change only removes code. The wrapper's drop path no longer visits the resource before dropping it, so run-time work goes down.


📝 Documentation: ✅ LGTM

as_local's docs no longer point at the removed APIs, and the README still states the Drop rule consistently.


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

@littledivy
littledivy merged commit 255874d into main Nov 21, 2026
35 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-20-rjsg-no-drop-gc-refs branch November 21, 2026 02:40
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