From 0b529b2a83758330820a6c39156e67dc1da0e091 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 26 Apr 2018 11:53:23 +0200 Subject: [PATCH 1/4] stream: fix error handling with async iteration Fix an issue when an error was emitted by the stream before `iterator.next()` is called. --- lib/internal/streams/async_iterator.js | 2 +- .../test-stream-readable-async-iterators.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/internal/streams/async_iterator.js b/lib/internal/streams/async_iterator.js index 9ca8e5ebe23b15..0e34573d877aee 100644 --- a/lib/internal/streams/async_iterator.js +++ b/lib/internal/streams/async_iterator.js @@ -58,7 +58,7 @@ function onError(iter, err) { iter[kLastReject] = null; reject(err); } - iter.error = err; + iter[kError] = err; } function wrapForNext(lastPromise, iter) { diff --git a/test/parallel/test-stream-readable-async-iterators.js b/test/parallel/test-stream-readable-async-iterators.js index b1801a1db3e580..4d0d0033ae159c 100644 --- a/test/parallel/test-stream-readable-async-iterators.js +++ b/test/parallel/test-stream-readable-async-iterators.js @@ -115,6 +115,23 @@ async function tests() { readable.destroy(new Error('kaboom')); })(); + await (async function() { + console.log('call next() after error'); + const readable = new Readable({ + read() {} + }) + + let err + try { + const iterator = readable[Symbol.asyncIterator](); + readable.destroy(new Error('kaboom')); + await iterator.next(); + } catch (e) { + err = e; + } + assert.strictEqual(err.message, 'kaboom'); + })(); + await (async function() { console.log('read object mode'); const max = 42; From 0e5995ac67e8db1403daf8dfdc0f3d372dfde975 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 26 Apr 2018 14:13:15 +0200 Subject: [PATCH 2/4] coding style --- test/parallel/test-stream-readable-async-iterators.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-stream-readable-async-iterators.js b/test/parallel/test-stream-readable-async-iterators.js index 4d0d0033ae159c..877f9eb7407dac 100644 --- a/test/parallel/test-stream-readable-async-iterators.js +++ b/test/parallel/test-stream-readable-async-iterators.js @@ -119,9 +119,9 @@ async function tests() { console.log('call next() after error'); const readable = new Readable({ read() {} - }) + }); - let err + let err; try { const iterator = readable[Symbol.asyncIterator](); readable.destroy(new Error('kaboom')); From fea77e07cfddbae5791abfa7df1b3a14d2098921 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 26 Apr 2018 14:14:33 +0200 Subject: [PATCH 3/4] test by error identity --- .../test-stream-readable-async-iterators.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-stream-readable-async-iterators.js b/test/parallel/test-stream-readable-async-iterators.js index 877f9eb7407dac..5b91b5e4f059d6 100644 --- a/test/parallel/test-stream-readable-async-iterators.js +++ b/test/parallel/test-stream-readable-async-iterators.js @@ -120,16 +120,11 @@ async function tests() { const readable = new Readable({ read() {} }); + const iterator = readable[Symbol.asyncIterator](); - let err; - try { - const iterator = readable[Symbol.asyncIterator](); - readable.destroy(new Error('kaboom')); - await iterator.next(); - } catch (e) { - err = e; - } - assert.strictEqual(err.message, 'kaboom'); + const err = new Error('kaboom'); + readable.destroy(new Error('kaboom')); + await assert.rejects(iterator.next.bind(iterator), err) })(); await (async function() { From c8c410a5aac2310e6e27fff2ecb6f8ccaff8ae55 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 26 Apr 2018 14:41:15 +0200 Subject: [PATCH 4/4] add missing semi --- test/parallel/test-stream-readable-async-iterators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-stream-readable-async-iterators.js b/test/parallel/test-stream-readable-async-iterators.js index 5b91b5e4f059d6..39761b413260f1 100644 --- a/test/parallel/test-stream-readable-async-iterators.js +++ b/test/parallel/test-stream-readable-async-iterators.js @@ -124,7 +124,7 @@ async function tests() { const err = new Error('kaboom'); readable.destroy(new Error('kaboom')); - await assert.rejects(iterator.next.bind(iterator), err) + await assert.rejects(iterator.next.bind(iterator), err); })(); await (async function() {