From a495a5ea4ac740f33bf716b2fb8f1fe8ba8f4bad Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 13 Apr 2020 11:18:56 +0200 Subject: [PATCH] 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. PR-URL: https://github.com/nodejs/node/pull/32809 Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca Reviewed-By: Denys Otrishko --- 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..2f5f8948eea458 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); + if (typeof cb === 'function') finished(this, cb); + this.destroy(); }; ObjectDefineProperty(ReadStream.prototype, 'pending', {