From 2dddcdd9fe610c17beb63bbbafda0adcc44a74c9 Mon Sep 17 00:00:00 2001 From: yoonsooshin Date: Thu, 25 Jul 2024 12:09:13 +0900 Subject: [PATCH] stream: relocate the status checking code in the onwritecomplete relocate the status checking code before verifying if the stream is destroyed --- 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; }