Repository navigation
Refuse to compile a JSG iterator State that skips visitForGc - #8480
Conversation
There was a problem hiding this comment.
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>andRef<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
05a5ab3 to
c2145c0
Compare
There was a problem hiding this comment.
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>andRef<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.
c2145c0 to
487b250
Compare
There was a problem hiding this comment.
✅ 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 akj::Stringand has novisitForGc(), so theJSG_HOLDS_NO_GC_HANDLESmarker is correct and is needed for the newstatic_assertto pass.PendingIterable::State: it already has a publicvisitForGc(), so it needs no marker.- Every other
JSG_ITERATOR/JSG_ASYNC_ITERATOR/*_ITERATOR_TYPEuse insrc/: each has a State that either has a publicvisitForGc()or is now marked or specialized.- With a public
visitForGc(): FormData, URLSearchParams (bothurl.handurl-standard.h), WebSocketPair, EventCounts, the filesystem iterators and ReadableStream. - Marked or specialized: Headers,
IoOwn<SqliteKv::ListCursor>andRef<SqlStorage::Cursor>.
- With a public
- Where the check runs:
holdsNoGcHandles<State>is evaluated only when the iterator templates are instantiated. So the specializations placed after the iterator declarations insql.handsync-kv.hare in time.
📝 Documentation: ✅ LGTM
The README and the doc comments in jsg.h and iterator.h describe the new requirement accurately.
Reviewed commit: 487b250f
IteratorBaseandAsyncIteratorBasevisit theirStateonly if it has a publicvisitForGc(). A State that holds ajsg::Refbut has novisitForGc()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:
visitForGc(), it must be marked with the newJSG_HOLDS_NO_GC_HANDLES, or the iterator fails to compile. For a type you can't add a member to, specializejsg::holdsNoGcHandles<T>.Headers' iterator State and sync KV'sIoOwn<SqliteKv::ListCursor>hold no handles and are marked.SqlStorage::Cursor'sRowIteratorandRawIteratorusejsg::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.