diff --git a/lib/fs.js b/lib/fs.js index f0a9b676b34..ebf70948192 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2128,7 +2128,7 @@ ReadStream.prototype._destroy = function(err, cb) { return; } - closeFsStream(this, cb); + closeFsStream(this, cb, err); this.fd = null; }; diff --git a/test/parallel/test-fs-stream-destroy-emit-error.js b/test/parallel/test-fs-stream-destroy-emit-error.js new file mode 100644 index 00000000000..c0405ce5f15 --- /dev/null +++ b/test/parallel/test-fs-stream-destroy-emit-error.js @@ -0,0 +1,20 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +test(fs.createReadStream(__filename)); +test(fs.createWriteStream(`${tmpdir.path}/dummy`)); + +function test(stream) { + const err = new Error('DESTROYED'); + stream.on('open', function() { + stream.destroy(err); + }); + stream.on('error', common.mustCall(function(err_) { + assert.strictEqual(err_, err); + })); +}