GeezerVison is showing github.com — stream: fix async iterator destroyed error propagation · nodejs/node@499c41d
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/internal/streams/async_iterator.js +13-11Lines changed: 13 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,18 +78,20 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({ |
78 | 78 | } |
79 | 79 |
|
80 | 80 | if (this[kStream].destroyed) { |
81 | | - // We need to defer via nextTick because if .destroy(err) is |
82 | | - // called, the error will be emitted via nextTick, and |
83 | | - // we cannot guarantee that there is no error lingering around |
84 | | - // waiting to be emitted. |
85 | 81 | return new Promise((resolve, reject) => { |
86 | | - process.nextTick(() => { |
87 | | - if (this[kError]) { |
88 | | - reject(this[kError]); |
89 | | - } else { |
90 | | - resolve(createIterResult(undefined, true)); |
91 | | - } |
92 | | - }); |
| 82 | + if (this[kError]) { |
| 83 | + reject(this[kError]); |
| 84 | + } else if (this[kEnded]) { |
| 85 | + resolve(createIterResult(undefined, true)); |
| 86 | + } else { |
| 87 | + finished(this[kStream], (err) => { |
| 88 | + if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') { |
| 89 | + reject(err); |
| 90 | + } else { |
| 91 | + resolve(createIterResult(undefined, true)); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
93 | 95 | }); |
94 | 96 | } |
95 | 97 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-stream-readable-async-iterators.js +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -484,6 +484,23 @@ async function tests() { |
484 | 484 | assert.strictEqual(e, err); |
485 | 485 | })()]); |
486 | 486 | } |
| 487 | + |
| 488 | + { |
| 489 | + const _err = new Error('asd'); |
| 490 | + const r = new Readable({ |
| 491 | + read() { |
| 492 | + }, |
| 493 | + destroy(err, callback) { |
| 494 | + setTimeout(() => callback(_err), 1); |
| 495 | + } |
| 496 | + }); |
| 497 | + |
| 498 | + r.destroy(); |
| 499 | + const it = r[Symbol.asyncIterator](); |
| 500 | + it.next().catch(common.mustCall((err) => { |
| 501 | + assert.strictEqual(err, _err); |
| 502 | + })); |
| 503 | + } |
487 | 504 | } |
488 | 505 |
|
489 | 506 | { |
|