Repository navigation
Pin return() on an async iterator whose next() rejected - #7810
Conversation
|
✅ 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: ✅ LGTMNo security concerns. The change refactors the internals of stream iterators and adds tests. 📖 AGENTS.md: ✅ LGTMThe edits to 📋 Code Quality: ✅ LGTM
⚡ Performance: ✅ LGTMNo performance concerns. The refactor removes one object allocation per 📝 Documentation: ✅ LGTMThe new comments describe the code as it is now and follow the repository's comment guidelines. Reviewed commit: |
|
Since last review: 0 resolved, 0 still open, 0 new. 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 |
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.
b118528 to
7b0323b
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Multiple ts-streams iterator cleanups. See individual commits.