stream: fixup stream/iter to drop at most one entry per share call · nodejs/node@7d245cf · GitHub
Skip to content

Commit 7d245cf

Browse files
jasnelladuh95
authored andcommitted
stream: fixup stream/iter to drop at most one entry per share call
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #66028 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent 30e1f76 commit 7d245cf

3 files changed

Lines changed: 63 additions & 11 deletions

File tree

‎doc/api/stream_iter.md‎

Lines changed: 9 additions & 1 deletion

‎lib/internal/streams/iter/share.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ class SyncShareImpl {
583583
}
584584

585585
// Check buffer limit
586+
let dropped = false;
586587
if (self.#bufferedBytes >= self.#options.budget) {
587588
switch (self.#options.backpressure) {
588589
case 'strict':
@@ -605,17 +606,20 @@ class SyncShareImpl {
605606
self.#recomputeMinCursor();
606607
break;
607608
case 'drop-newest':
608-
while (self.#bufferedBytes >= self.#options.budget &&
609-
!self.#sourceExhausted &&
610-
!self.#cancelled &&
611-
self.#sourceError === kNoShareError) {
612-
self.#pullFromSource(true);
613-
}
609+
// Discarding does not reclaim budget, and the slowest
610+
// consumer cannot advance while this synchronous next() is
611+
// running, so at most one entry may be dropped per call.
612+
// Looping here would spin forever on an unbounded source
613+
// and drain a finite one in a single call.
614+
self.#pullFromSource(true);
615+
dropped = true;
614616
break;
615617
}
616618
}
617619

618-
self.#pullFromSource();
620+
if (!dropped) {
621+
self.#pullFromSource();
622+
}
619623

620624
if (self.#sourceError !== kNoShareError) {
621625
state.detached = true;

‎test/parallel/test-stream-iter-share-sync.js‎

Lines changed: 43 additions & 3 deletions

0 commit comments

Comments
 (0)