Skip to content

Commit f1fba8d

Browse files
kanongilbnoordhuis
authored andcommitted
fs: fix ReadStream / WriteStream missing callback
The (undocumented) callback argument to .destroy() was not called if the stream was no longer readable / writable.
1 parent 23f09d7 commit f1fba8d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/fs.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,10 @@ ReadStream.prototype._emitData = function(d) {
13671367
ReadStream.prototype.destroy = function(cb) {
13681368
var self = this;
13691369

1370-
if (!this.readable) return;
1370+
if (!this.readable) {
1371+
if (cb) process.nextTick(function() { cb(null); });
1372+
return;
1373+
}
13711374
this.readable = false;
13721375

13731376
function close() {
@@ -1570,7 +1573,10 @@ WriteStream.prototype.end = function(data, encoding, cb) {
15701573
WriteStream.prototype.destroy = function(cb) {
15711574
var self = this;
15721575

1573-
if (!this.writable) return;
1576+
if (!this.writable) {
1577+
if (cb) process.nextTick(function() { cb(null); });
1578+
return;
1579+
}
15741580
this.writable = false;
15751581

15761582
function close() {

test/simple/test-fs-read-stream.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ var file2 = fs.createReadStream(fn);
8686
file2.destroy(function(err) {
8787
assert.ok(!err);
8888
callbacks.destroy++;
89+
90+
file2.destroy(function(err) {
91+
assert.ok(!err);
92+
callbacks.destroy++;
93+
});
8994
});
9095

9196
var file3 = fs.createReadStream(fn, {encoding: 'utf8'});
@@ -107,7 +112,7 @@ file3.on('close', function() {
107112
process.on('exit', function() {
108113
assert.equal(1, callbacks.open);
109114
assert.equal(1, callbacks.end);
110-
assert.equal(1, callbacks.destroy);
115+
assert.equal(2, callbacks.destroy);
111116

112117
assert.equal(2, callbacks.close);
113118

0 commit comments

Comments
 (0)