From 6d6bb2a3dcd98136e8a1bc970fa001a246b7f1ca 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 afb88d9eb152cb..343e09d7bc271b 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -22,7 +22,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'); @@ -266,7 +266,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', {