From 7494d731702a1347feccff80fe23e056690530f6 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 13 Apr 2020 11:18:56 +0200 Subject: [PATCH 1/2] fs: use finished over destroy w/ cb destroy w/ is undocumented API which also will cause a race if the stream is already destroying and potentially invoking the callback too early and withou error. --- lib/internal/fs/streams.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index a76a8f6895c887..1e9b50834cf137 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -23,7 +23,7 @@ const { copyObject, getOptions, } = require('internal/fs/utils'); -const { Readable, Writable } = require('stream'); +const { Readable, Writable, finished } = require('stream'); const { toPathIfFileURL } = require('internal/url'); const kIoDone = Symbol('kIoDone'); const kIsPerformingIO = Symbol('kIsPerformingIO'); @@ -273,7 +273,8 @@ function closeFsStream(stream, cb, err) { } ReadStream.prototype.close = function(cb) { - this.destroy(null, cb); + finished(this, cb); + this.destroy(); }; ObjectDefineProperty(ReadStream.prototype, 'pending', { From f229caceb73fdada91e08c02c97cdc6c0d344c7c Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 13 Apr 2020 12:31:48 +0200 Subject: [PATCH 2/2] fixup: handle cb null case --- lib/internal/fs/streams.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 1e9b50834cf137..2f5f8948eea458 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -273,7 +273,7 @@ function closeFsStream(stream, cb, err) { } ReadStream.prototype.close = function(cb) { - finished(this, cb); + if (typeof cb === 'function') finished(this, cb); this.destroy(); };