Skip to content

Keep a queued ReadableStream's source hooks out of closures - #8880

Merged
danlapid merged 1 commit into
mainfrom
dlapid/ts-readable-source-hooks-as-data
Nov 25, 2026
Merged

danlapid merged 1 commit into
mainfrom
dlapid/ts-readable-source-hooks-as-data

Conversation

@danlapid

@danlapid danlapid commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

gc.js's sourceReleasedOnceEnded fails intermittently in the TypeScript streams cell (about one run in a hundred in an optimized build). After controller.error(), the underlying source sometimes survives three full GCs.

Cause

The queued controllers called the source's pull and cancel through two closures per stream, made by makeSourceAlgorithms, which captured the source. ClearAlgorithms dropped the closures to release it. But a closure 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 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-opt showed Maglev marking makeSourceAlgorithms's pull right there. Results by V8 flag:

V8 flags retained
default 1+ per run
--no-turbofan 1+ per run
--no-use-osr 1+ per run
--no-maglev none
--no-concurrent-recompilation --no-concurrent-osr none
--no-maglev --no-turbofan --no-sparkplug none

Fix

The core now holds the source, the controller and the two hooks in a null-prototype record (SourceHooks) that ClearAlgorithms drops. Module-level pullSource() and cancelSource() 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 a start()-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-flags and @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/... and js-readable-stream-test: all pass except digest-ts@all-autogates. It timed out in debug in gcDuringCloseOfDroppedStream, 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

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>
@danlapid
danlapid marked this pull request as ready for review November 25, 2026 00:15
@danlapid
danlapid requested review from a team as code owners November 25, 2026 00:15

@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 — 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.
  • kPullDone and kPullParked still 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

@danlapid
danlapid merged commit b7b5d75 into main Nov 25, 2026
35 of 39 checks passed
@danlapid
danlapid deleted the dlapid/ts-readable-source-hooks-as-data branch November 25, 2026 02:36
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