GeezerVison is showing github.com — stream: address stream/iter review feedback · nodejs/node@5943deb
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/streams/iter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/consumers.js +12-8Lines changed: 12 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ const { | 18 | 18 | Promise, | 19 | 19 | PromisePrototypeThen, | 20 | 20 | SafePromiseAllReturnVoid, | | 21 | + SafeSet, | 21 | 22 | Symbol, | 22 | 23 | SymbolAsyncIterator, | 23 | 24 | TypedArrayPrototypeGetBuffer, | @@ -451,6 +452,7 @@ function merge(...args) { | 451 | 452 | // async tick per batch. Each source has at most one pending .next() | 452 | 453 | // at a time. Every batch from every source is preserved. | 453 | 454 | const ready = []; | | 455 | + const pendingPulls = new SafeSet(); | 454 | 456 | let activeCount = normalized.length; | 455 | 457 | let waitResolve = null; | 456 | 458 | let onAbort; | @@ -472,6 +474,7 @@ function merge(...args) { | 472 | 474 | // Called when a source's .next() settles. Pushes the result into | 473 | 475 | // the ready queue and wakes the consumer if it's waiting. | 474 | 476 | const onSettled = (iterator, result) => { | | 477 | + pendingPulls.delete(iterator); | 475 | 478 | if (stopped) return; | 476 | 479 | if (result.done) { | 477 | 480 | activeCount--; | @@ -489,7 +492,8 @@ function merge(...args) { | 489 | 492 | } | 490 | 493 | }; | 491 | 494 |
| 492 | | - const onRejected = (reason) => { | | 495 | + const onRejected = (iterator, reason) => { | | 496 | + pendingPulls.delete(iterator); | 493 | 497 | if (stopped) return; | 494 | 498 | ArrayPrototypePush(ready, { | 495 | 499 | __proto__: null, | @@ -507,14 +511,14 @@ function merge(...args) { | 507 | 511 | for (let i = 0; i < normalized.length; i++) { | 508 | 512 | const iterator = normalized[i][SymbolAsyncIterator](); | 509 | 513 | ArrayPrototypePush(iterators, iterator); | | 514 | + pendingPulls.add(iterator); | 510 | 515 | PromisePrototypeThen( | 511 | 516 | iterator.next(), | 512 | 517 | (r) => onSettled(iterator, r), | 513 | | - onRejected, | | 518 | + (reason) => onRejected(iterator, reason), | 514 | 519 | ); | 515 | 520 | } | 516 | 521 |
| 517 | | - let completed = false; | 518 | 522 | let primaryError = kNoMergeError; | 519 | 523 | try { | 520 | 524 | while (activeCount > 0 || ready.length > 0) { | @@ -527,10 +531,11 @@ function merge(...args) { | 527 | 531 | throw item.reason; | 528 | 532 | } | 529 | 533 | yield item.value; | | 534 | + pendingPulls.add(item.iterator); | 530 | 535 | PromisePrototypeThen( | 531 | 536 | item.iterator.next(), | 532 | 537 | (r) => onSettled(item.iterator, r), | 533 | | - onRejected, | | 538 | + (reason) => onRejected(item.iterator, reason), | 534 | 539 | ); | 535 | 540 | } | 536 | 541 |
| @@ -545,7 +550,6 @@ function merge(...args) { | 545 | 550 | }); | 546 | 551 | } | 547 | 552 | } | 548 | | - completed = true; | 549 | 553 | } catch (err) { | 550 | 554 | primaryError = err; | 551 | 555 | } finally { | @@ -559,20 +563,20 @@ function merge(...args) { | 559 | 563 | await cleanupIterators( | 560 | 564 | iterators, | 561 | 565 | primaryError, | 562 | | - !completed, | | 566 | + pendingPulls, | 563 | 567 | ); | 564 | 568 | } | 565 | 569 | }, | 566 | 570 | }; | 567 | 571 | } | 568 | 572 |
| 569 | | -async function cleanupIterators(iterators, primaryError, skipAwaitCleanup) { | | 573 | +async function cleanupIterators(iterators, primaryError, pendingPulls) { | 570 | 574 | let cleanupError = kNoMergeError; | 571 | 575 | await SafePromiseAllReturnVoid(iterators, async (iterator) => { | 572 | 576 | if (iterator.return) { | 573 | 577 | try { | 574 | 578 | const result = iterator.return(); | 575 | | - if (skipAwaitCleanup) { | | 579 | + if (pendingPulls.has(iterator)) { | 576 | 580 | markPromiseAsHandled(result); | 577 | 581 | } else { | 578 | 582 | await result; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/duplex.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ const { | 12 | 12 | } = primordials; | 13 | 13 |
| 14 | 14 | const { | | 15 | + isConsumerReturnError, | 15 | 16 | push, | 16 | 17 | } = require('internal/streams/iter/push'); | 17 | 18 | const { | @@ -99,7 +100,11 @@ async function closeDuplexChannel(writer, closeIterator) { | 99 | 100 | const returnPromise = closeIterator.return(); | 100 | 101 |
| 101 | 102 | if (endPromise !== undefined) { | 102 | | - await SafePromiseAllReturnVoid([endPromise, returnPromise]); | | 103 | + try { | | 104 | + await SafePromiseAllReturnVoid([endPromise, returnPromise]); | | 105 | + } catch (error) { | | 106 | + if (!isConsumerReturnError(error)) throw error; | | 107 | + } | 103 | 108 | } else { | 104 | 109 | await returnPromise; | 105 | 110 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/push.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ const { | 11 | 11 | PromiseReject, | 12 | 12 | PromiseResolve, | 13 | 13 | PromiseWithResolvers, | | 14 | + SafeWeakSet, | 14 | 15 | Symbol, | 15 | 16 | SymbolAsyncDispose, | 16 | 17 | SymbolAsyncIterator, | @@ -55,6 +56,11 @@ const { | 55 | 56 | } = require('internal/streams/iter/ringbuffer'); | 56 | 57 |
| 57 | 58 | const kNoFailReason = Symbol('kNoFailReason'); | | 59 | +const consumerReturnErrors = new SafeWeakSet(); | | 60 | + | | 61 | +function isConsumerReturnError(error) { | | 62 | + return consumerReturnErrors.has(error); | | 63 | +} | 58 | 64 |
| 59 | 65 | function raceEndWithSignal(promise, signal) { | 60 | 66 | if (!signal) return promise; | @@ -471,6 +477,7 @@ class PushQueue { | 471 | 477 | if (this.#consumerState !== 'active') return; | 472 | 478 | this.#consumerState = 'returned'; | 473 | 479 | const error = new ERR_INVALID_STATE.TypeError('Stream closed by consumer'); | | 480 | + consumerReturnErrors.add(error); | 474 | 481 | this.#terminateWriterFromConsumer(error); | 475 | 482 | this.#resolvePendingReads(); | 476 | 483 | // Resolve pending drains with false - no more data will be consumed | @@ -791,5 +798,6 @@ function push(...args) { | 791 | 798 | } | 792 | 799 |
| 793 | 800 | module.exports = { | | 801 | + isConsumerReturnError, | 794 | 802 | push, | 795 | 803 | }; |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-consumers-merge.js +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -447,6 +447,27 @@ async function testMergeBreakWithCleanupError() { | 447 | 447 | ); | 448 | 448 | } | 449 | 449 |
| | 450 | +async function testMergeMultiSourceBreakWithCleanupError() { | | 451 | + async function* failingReturnSource() { | | 452 | + try { | | 453 | + yield [new TextEncoder().encode('data')]; | | 454 | + } finally { | | 455 | + await Promise.resolve(); | | 456 | + throwInFinally('async cleanup on break'); | | 457 | + } | | 458 | + } | | 459 | + | | 460 | + await assert.rejects( | | 461 | + async () => { | | 462 | + // eslint-disable-next-line no-unused-vars | | 463 | + for await (const _ of merge(failingReturnSource(), from('other'))) { | | 464 | + break; | | 465 | + } | | 466 | + }, | | 467 | + { message: 'async cleanup on break' }, | | 468 | + ); | | 469 | +} | | 470 | + | 450 | 471 | Promise.all([ | 451 | 472 | testMergeTwoSources(), | 452 | 473 | testMergeSingleSource(), | @@ -468,4 +489,5 @@ Promise.all([ | 468 | 489 | testMergeCleanupErrorOnly(), | 469 | 490 | testMergePrimaryErrorPrecedesCleanupError(), | 470 | 491 | testMergeBreakWithCleanupError(), | | 492 | + testMergeMultiSourceBreakWithCleanupError(), | 471 | 493 | ]).then(common.mustCall()); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-duplex.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,6 +77,20 @@ async function testChannelClose() { | 77 | 77 | assert.strictEqual(batches.length, 0); | 78 | 78 | } | 79 | 79 |
| | 80 | +async function testConcurrentChannelClose() { | | 81 | + const [channelA, channelB] = duplex(); | | 82 | + | | 83 | + const results = await Promise.allSettled([ | | 84 | + channelA.close(), | | 85 | + channelB.close(), | | 86 | + ]); | | 87 | + | | 88 | + assert.deepStrictEqual(results, [ | | 89 | + { status: 'fulfilled', value: undefined }, | | 90 | + { status: 'fulfilled', value: undefined }, | | 91 | + ]); | | 92 | +} | | 93 | + | 80 | 94 | async function testWithOptions() { | 81 | 95 | const [channelA, channelB] = duplex({ | 82 | 96 | budget: 16384, | @@ -223,6 +237,7 @@ Promise.all([ | 223 | 237 | testBidirectional(), | 224 | 238 | testMultipleWrites(), | 225 | 239 | testChannelClose(), | | 240 | + testConcurrentChannelClose(), | 226 | 241 | testWithOptions(), | 227 | 242 | testPerChannelOptions(), | 228 | 243 | testAbortSignal(), | |
|