From 7e33b6a091ac1ffbaf2c11f1a9101482941c8ecf Mon Sep 17 00:00:00 2001 From: YoonSoo_Shin <89785501+MCprotein@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:29:17 +0900 Subject: [PATCH] stream: relocate the status checking code in the onwritecomplete relocate the status checking code before verifying if the stream is destroyed PR-URL: https://github.com/nodejs/node/pull/54032 Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/internal/stream_base_commons.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index f6c3c5a8cf1063..76a87ab74b74e6 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -83,20 +83,18 @@ function onWriteComplete(status) { const stream = this.handle[owner_symbol]; - if (stream.destroyed) { - if (typeof this.callback === 'function') - this.callback(null); - return; + if (status < 0) { + const error = new ErrnoException(status, 'write', this.error); + if (typeof this.callback === 'function') { + return this.callback(error); + } + + return stream.destroy(error); } - // TODO (ronag): This should be moved before if(stream.destroyed) - // in order to avoid swallowing error. - if (status < 0) { - const ex = new ErrnoException(status, 'write', this.error); + if (stream.destroyed) { if (typeof this.callback === 'function') - this.callback(ex); - else - stream.destroy(ex); + this.callback(null); return; }