From 2f9bc11826fdc87a9dbafb04981700495e836463 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 12 Jul 2021 09:42:50 +0200 Subject: [PATCH] stream: forward errored to callback Refs: https://github.com/nodejs/node/issues/39356 --- lib/internal/streams/writable.js | 4 ++-- test/parallel/test-stream-writable-destroy.js | 13 +++++++++++++ .../test-stream-writable-end-cb-uncaught.js | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index f41cc183f0939a..e638c13983fa61 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -514,12 +514,12 @@ function errorBuffer(state) { const { chunk, callback } = state.buffered[n]; const len = state.objectMode ? 1 : chunk.length; state.length -= len; - callback(new ERR_STREAM_DESTROYED('write')); + callback(state.errored ?? new ERR_STREAM_DESTROYED('write')); } const onfinishCallbacks = state[kOnFinished].splice(0); for (let i = 0; i < onfinishCallbacks.length; i++) { - onfinishCallbacks[i](new ERR_STREAM_DESTROYED('end')); + onfinishCallbacks[i](state.errored ?? new ERR_STREAM_DESTROYED('end')); } resetBuffer(state); diff --git a/test/parallel/test-stream-writable-destroy.js b/test/parallel/test-stream-writable-destroy.js index 2e6e1f975a2be1..b7101f65427cf1 100644 --- a/test/parallel/test-stream-writable-destroy.js +++ b/test/parallel/test-stream-writable-destroy.js @@ -471,3 +471,16 @@ const assert = require('assert'); write.destroy(); write.destroy(); } + +{ + // https://github.com/nodejs/node/issues/39356 + const s = new Writable({ + final() {} + }); + const _err = new Error('oh no'); + // Remove `callback` and it works + s.end(common.mustCall((err) => { + assert.strictEqual(err, _err); + })); + s.destroy(_err); +} diff --git a/test/parallel/test-stream-writable-end-cb-uncaught.js b/test/parallel/test-stream-writable-end-cb-uncaught.js index 5c1753aa067342..02586b45d99a2f 100644 --- a/test/parallel/test-stream-writable-end-cb-uncaught.js +++ b/test/parallel/test-stream-writable-end-cb-uncaught.js @@ -9,15 +9,16 @@ process.on('uncaughtException', common.mustCall((err) => { })); const writable = new stream.Writable(); +const _err = new Error('kaboom'); writable._write = (chunk, encoding, cb) => { cb(); }; writable._final = (cb) => { - cb(new Error('kaboom')); + cb(_err); }; writable.write('asd'); writable.end(common.mustCall((err) => { - assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED'); + assert.strictEqual(err, _err); }));