Repository navigation
Keep a queued ReadableStream's source hooks out of closures - #8880
Conversation
The queued controllers called the underlying source's pull and cancel through two closures per stream that captured the source, and ClearAlgorithms dropped the closures to release the source. A closure that V8 is optimizing can outlive the stream's reference to it: while concurrent Maglev compiles the shared pull closure, the instance that triggered tier-up stays reachable, and its context keeps that stream's source alive after the stream has closed, errored or been cancelled. gc.js sourceReleasedOnceEnded caught this intermittently in the TS cell (about one run in a hundred): looping its error case reproduced it on every run, always on the stream whose pull closure was hot when Maglev picked it up, and never with --no-maglev or --no-concurrent-recompilation. The core now holds the source, the controller and the two hooks in a null-prototype record that ClearAlgorithms drops, and calls the hooks through module-level functions, so no per-stream closure references the source. A source with neither hook gets no record, as before it got no closures, so a stream does not retain a start()-only source. This also saves the two closure allocations per stream. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
✅ Approved — This review found no issues. The SourceHooks record keeps the old closures' pull and cancel behaviour, and it is cleared at every point where the closures used to be dropped.
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 hooks are still called with the source as the receiver, through the captured FunctionPrototypeCall. The record uses nullPrototypeRecord, so it inherits nothing from Object.prototype. After the stream ends, nothing calls the source again.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
pullSource() and cancelSource() behave the same as the closures they replace:
- A synchronous throw still becomes a rejection.
- A missing hook still resolves.
kPullDoneandkPullParkedstill pass through.
#sourceHooks is cleared at the same three points the closures were dropped: the all-consumers-gone hook, cancelSteps and #clearAlgorithms. cancelSteps reads the record into a local before clearing it, so cancel still runs. A source with neither hook still gets no record, so a start()-only source is still not retained.
⚡ Performance: ✅ LGTM
Each stream now allocates one record instead of two closures. The extra property read on each pull costs very little.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: 9926b55f
gc.js'ssourceReleasedOnceEndedfails intermittently in the TypeScript streams cell (about one run in a hundred in an optimized build). Aftercontroller.error(), the underlying source sometimes survives three full GCs.Cause
The queued controllers called the source's
pullandcancelthrough two closures per stream, made bymakeSourceAlgorithms, which captured the source.ClearAlgorithmsdropped the closures to release it. But a closure V8 is optimizing can outlive the stream's reference to it. While concurrent Maglev compiles the sharedpullclosure, the instance that triggered tier-up stays reachable, and its context keeps that stream's source alive after the stream has ended.I reproduced it by looping the test's cases 150 times each per run. The first loop retained exactly one source on every run, at the same iteration, and
--trace-optshowed Maglev markingmakeSourceAlgorithms'spullright there. Results by V8 flag:--no-turbofan--no-use-osr--no-maglev--no-concurrent-recompilation --no-concurrent-osr--no-maglev --no-turbofan --no-sparkplugFix
The core now holds the source, the controller and the two hooks in a null-prototype record (
SourceHooks) thatClearAlgorithmsdrops. Module-levelpullSource()andcancelSource()call the hooks, so no per-stream closure references the source. A source with neither hook gets no record, just as it used to get no closures, so astart()-only source is still not retained. This also saves two closure allocations per stream.With the fix, the same loop retained no source in 10 of 10 runs.
Testing
src/tests/streams/readable:readable-ts@,@all-compat-flagsand@all-autogates: 5/5 in both the debug and release configs.readable-ts@all-compat-flags, release config: 100/100. It failed 1/100 before the fix.src/tests/streams/...,src/per_isolate/...(lint and type check),src/tests/node/stream/...andjs-readable-stream-test: all pass exceptdigest-ts@all-autogates. It timed out in debug ingcDuringCloseOfDroppedStream, a DigestStream (writable) case that Make the slowest stream, snapshot and bench tests faster #8863 already moves out into a dedicated target.🤖 Generated with Claude Code