Skip to content

Pin return() on an async iterator whose next() rejected - #7810

Merged
jasnell merged 6 commits into
mainfrom
jasnell/ts-streams-duplicate-consolidaton-24
Oct 23, 2026
Merged

jasnell merged 6 commits into
mainfrom
jasnell/ts-streams-duplicate-consolidaton-24

Conversation

@jasnell

@jasnell jasnell commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Multiple ts-streams iterator cleanups. See individual commits.

@jasnell
jasnell added this pull request to stack #7801 October 18, 2026 02:48
@jasnell
jasnell requested review from a team as code owners October 18, 2026 02:48
@clanky-the-clanker

clanky-the-clanker Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

✅ Approved — No issues found. The iterator refactor in readable.ts keeps the existing behaviour, and the new tests cover the edge cases the pull request targets.

The decision is advisory: this comment never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns. The change refactors the internals of stream iterators and adds tests.


📖 AGENTS.md: ✅ LGTM

The edits to src/tests/streams/readable/AGENTS.md match the updated tests.


📋 Code Quality: ✅ LGTM

  • src/per_isolate/webstreams/readable.ts: ReadableStreamAsyncIteratorReadRequest and readableStreamDefaultReaderRead are replaced by then-callbacks written inline in iteratorNextSteps. Behaviour stays the same:
    • current is cleared on settle.
    • done is set on close and on error.
    • The reader is released in both of those cases.
  • The old "reader has been released" error path could never run, because iteratorNextSteps already checks that the reader is bound to a stream. Using getReaderStream for that check removes the second lookup.
  • Moving the iterator state (done and current) up into IteratorInternalState removes one level of indirection and does not change behaviour.
  • The new closeIterator helper keeps the order of steps for both close paths:
    • Looking up return() comes first. A missing return() resolves at once, and a throw from the lookup or the call rejects.
    • On the sync path, a return() result that is not an object still rejects with a TypeError.
  • The new tests in from.js and then-interceptors.js cover:
    • a return that is not callable
    • a sync return() that returns a non-object
    • return() being called only once
    • return() called after next() rejected

⚡ Performance: ✅ LGTM

No performance concerns. The refactor removes one object allocation per next() call (the read-request object) and one lookup of the reader's stream.


📝 Documentation: ✅ LGTM

The new comments describe the code as it is now and follow the repository's comment guidelines.


Reviewed commit: 7b0323b8

@ask-bonk

ask-bonk Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Since last review: 0 resolved, 0 still open, 0 new.
LGTM!

Not re-run: tests, api-compat, docs, compat-flags, design-simplicity (no author changes in their files since the last review)


Reviewed commit: 7b0323b8 · github run

Base automatically changed from jasnell/ts-streams-duplicate-consolidaton-23 to main October 23, 2026 14:48
Once an async iterator's next() rejects, the iterator is finished and
has no ongoing promise, so a later next() or return() runs its steps
at once. Only the first half was tested: a rejection that left the
ongoing promise in place would just delay those calls, which still
report done.

The readable suite's thenGetterPerIteratorReturn gains the errored
case beside the closed one. Running its steps at once, return() looks
then up twice, as on an iterator whose last next() reported done; one
waiting on the rejected promise would look it up a third time, as it
adopts the promise of the step it waited for. C++ rejects the return()
with the stream's error, as it rejects later next() calls; ledger #22
now says so, and #26 notes it.
The async iterator's next() read through a class built for the one
call: ReadableStreamAsyncIteratorReadRequest held the reader, the
iterator's state and the promise next() returns, and its chunk, close
and error steps settled that promise. A module slot assigned in the
default reader's static block took the request, read through the
internal default read, and dispatched the result to those steps.

iteratorNextSteps now makes the read itself, and its reactions do
what the steps did, in the same order, settling the same promise.
The class, the slot and its assignment go, as does one allocation per
next(), with the same reactions. So does the slot's check for a
released reader, which could not fire: next() checks the reader is
bound just before, with no user code in between, and that check now
reads the stream it uses.
The async iterator kept whether it is finished and its ongoing
promise in a record of their own, inside the one the iterator's
methods look up, so the read request could hold that record without
the rest. With the read request inlined into the next steps, which
take the whole state, the split has no reason: done and current move
into the iterator's state, and the separate interface goes.
ReadableStream.from() closes the iterator through return() when its
stream is cancelled. For an async iterator, the cancel's rejection
messages were pinned when return() is not callable and when its
result is not an object. For a sync iterator, which goes through the
async-from-sync steps, neither the messages nor the object check
itself was asserted: a sync return() result that is not an object
could have let the cancel succeed unnoticed.

The readable suite's fromReturnValidationMessages gains both cases
for a sync iterable. TS rejects with the same messages as for an
async one; C++ rejects with "Property 'return' is not a function"
and "Callback returned incorrect type; expected 'Promise'".
When ReadableStream.from() cancels a sync iterable, the steps that
wrap the sync iterator resolve the value of what return() returned,
and a rejection rejects the cancel. Unlike the same steps for next(),
they do not then close the iterator, which would call return() a
second time. The test of a return() result whose value rejects
checked the rejection and the reads of done and value, but not how
often return() ran, so a second call would have gone unnoticed.

fromIteratorProtocolEdges now counts the calls: one, in both
implementations.
Cancelling a stream made by ReadableStream.from() closes its
iterator, and the async and sync paths began the same way, in two
copies: look up return() (absent, the close resolves; not callable,
it rejects), call it with the reason, and reject with anything
thrown. closeIterator() now takes those steps and hands what
return() returned to each path's own continuation: an async
iterator's result is awaited and must be an object; a sync
iterator's must be an object, and its value is resolved through the
async-from-sync steps.

The sync path's check now returns its rejection instead of throwing
it inside the try: a TypeError with the same message, at the same
moment. The pulls keep their own calls to next(), whose shared part
is a few lines; sharing it would cost a closure per chunk.
@jasnell
jasnell force-pushed the jasnell/ts-streams-duplicate-consolidaton-24 branch from b118528 to 7b0323b Compare October 23, 2026 14:52
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@cd20da1). Learn more about missing BASE report.
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7810   +/-   ##
=======================================
  Coverage        ?   42.13%           
=======================================
  Files           ?     1052           
  Lines           ?   298959           
  Branches        ?    28835           
=======================================
  Hits            ?   125964           
  Misses          ?   156109           
  Partials        ?    16886           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jasnell
jasnell merged commit c3d13ef into main Oct 23, 2026
26 checks passed
@jasnell
jasnell deleted the jasnell/ts-streams-duplicate-consolidaton-24 branch October 23, 2026 15:47
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.

3 participants