Skip to content

Refuse to compile a JSG iterator State that skips visitForGc - #8480

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-15-gc-visit-skip-assert
Nov 20, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-15-gc-visit-skip-assert

Conversation

@littledivy

Copy link
Copy Markdown
Member

IteratorBase and AsyncIteratorBase visit their State only if it has a public visitForGc(). A State that holds a jsg::Ref but has no visitForGc() compiles fine and is never visited, so the handle stays strong and a cycle through it is never collected. ReadableStream's async iterator State had exactly this bug until #7957.

In this patch:

  • If a State has no public visitForGc(), it must be marked with the new JSG_HOLDS_NO_GC_HANDLES, or the iterator fails to compile. For a type you can't add a member to, specialize jsg::holdsNoGcHandles<T>.
  • Headers' iterator State and sync KV's IoOwn<SqliteKv::ListCursor> hold no handles and are marked.
  • SqlStorage::Cursor's RowIterator and RawIterator use jsg::Ref<Cursor> itself as the State, so the Ref is never visited. It is opted out explicitly, keeping today's behaviour.

The change is compile-time only.

@littledivy
littledivy requested review from a team as code owners November 16, 2026 03: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 c2145c09.

This review, of 05a5ab3f

✅ Approved — A compile-time-only check with no issues found; every existing iterator State is either visited or explicitly marked, and the documentation matches the code.

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

Every JSG_ITERATOR / JSG_ASYNC_ITERATOR use in src/workerd has a State that either has a public visitForGc() or is now marked or specialized:

  • Public visitForGc(): FormData, URLSearchParams, WebSocketPair, EventCounts, FileSystemDirectoryHandle and ReadableStream.
  • Marked or specialized: Headers, IoOwn<SqliteKv::ListCursor> and Ref<SqlStorage::Cursor>.

The static_assert is in a dependent discarded if constexpr branch, so it is evaluated only when the iterator is instantiated.


📝 Documentation: ✅ LGTM

The README and the doc comments in jsg.h and iterator.h describe the new requirement accurately.


Reviewed commit: 05a5ab3f

@littledivy
littledivy force-pushed the littledivy/2026-11-15-gc-visit-skip-assert branch from 05a5ab3 to c2145c0 Compare November 19, 2026 08:00

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

This review, of c2145c09

✅ Approved — A compile-time-only check with no issues found; every existing iterator State is either visited or explicitly marked, and the documentation matches the code.

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

Every JSG_ITERATOR / JSG_ASYNC_ITERATOR use in src/workerd has a State that either has a public visitForGc() or is now marked or specialized:

  • Public visitForGc(): FormData, URLSearchParams, WebSocketPair, EventCounts, FileSystemDirectoryHandle and ReadableStream.
  • Marked or specialized: Headers, IoOwn<SqliteKv::ListCursor> and Ref<SqlStorage::Cursor>.

The static_assert is in a dependent discarded if constexpr branch, so it is evaluated only when the iterator is instantiated.


📝 Documentation: ✅ LGTM

The README and the doc comments in jsg.h and iterator.h describe the new requirement accurately.


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

`jsg::holdsNoGcHandles<T>` is true for a type that declares `JSG_HOLDS_NO_GC_HANDLES`, or for which the variable is specialized. It lets a template that visits a member only if the member has a public `visitForGc()` tell a type that needs no visit from one that was forgotten.

In this patch `Headers`' iterator State and sync KV's `IoOwn<SqliteKv::ListCursor>` are marked, since they hold no handles. `SqlStorage::Cursor`'s `RowIterator` and `RawIterator` use `jsg::Ref<Cursor>` itself as the State, so the Ref is never visited. It is marked explicitly, keeping today's behaviour. Nothing reads the marker yet.
`IteratorBase` and `AsyncIteratorBase` visit their `State` only if it has a public `visitForGc()`. A State that holds a `jsg::Ref` but has no `visitForGc()` compiles fine and is never visited, so the handle stays strong and a cycle through it is never collected. `ReadableStream`'s async iterator State had exactly this bug until #7957.

In this patch a State with no public `visitForGc()` must be marked `JSG_HOLDS_NO_GC_HANDLES`, or the iterator fails to compile. The change is compile-time only.
@littledivy
littledivy force-pushed the littledivy/2026-11-15-gc-visit-skip-assert branch from c2145c0 to 487b250 Compare November 19, 2026 08:13

@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. This is a compile-time-only check, and every iterator State in the tree, including the test fixtures in memory-test.c++, either has a public visitForGc() or is explicitly marked.

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

I re-checked the change at 487b250. The only file changed since the last review is src/workerd/jsg/memory-test.c++.

  • SyncIteratorState: it holds only a kj::String and has no visitForGc(), so the JSG_HOLDS_NO_GC_HANDLES marker is correct and is needed for the new static_assert to pass.
  • PendingIterable::State: it already has a public visitForGc(), so it needs no marker.
  • Every other JSG_ITERATOR / JSG_ASYNC_ITERATOR / *_ITERATOR_TYPE use in src/: each has a State that either has a public visitForGc() or is now marked or specialized.
    • With a public visitForGc(): FormData, URLSearchParams (both url.h and url-standard.h), WebSocketPair, EventCounts, the filesystem iterators and ReadableStream.
    • Marked or specialized: Headers, IoOwn<SqliteKv::ListCursor> and Ref<SqlStorage::Cursor>.
  • Where the check runs: holdsNoGcHandles<State> is evaluated only when the iterator templates are instantiated. So the specializations placed after the iterator declarations in sql.h and sync-kv.h are in time.

📝 Documentation: ✅ LGTM

The README and the doc comments in jsg.h and iterator.h describe the new requirement accurately.


Reviewed commit: 487b250f

@littledivy
littledivy merged commit 1b2a900 into main Nov 20, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-15-gc-visit-skip-assert branch November 20, 2026 17:49
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