Skip to content

Commit

Permalink
stream: fix destroy(err, cb) regression
Browse files Browse the repository at this point in the history
Fixed a regression that caused the callback passed to destroy()
to not be called if the stream was already destroyed.
This caused a regression on the ws module in CITGM introduced by
#12925.

PR-URL: #13156
Fixes: websockets/ws#1118
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Calvin Metcalf <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
  • Loading branch information
mcollina committed May 24, 2017
1 parent 6af72d4 commit ccd3ead
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ function destroy(err, cb) {
this._writableState.destroyed;

if (readableDestroyed || writableDestroyed) {
if (err && (!this._writableState || !this._writableState.errorEmitted)) {
if (cb) {
cb(err);
} else if (err &&
(!this._writableState || !this._writableState.errorEmitted)) {
process.nextTick(emitErrorNT, this, err);
}
return;
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-net-socket-destroy-send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const common = require('../common');
const net = require('net');
const assert = require('assert');

const server = net.createServer();
server.listen(0, common.mustCall(function() {
const port = server.address().port;
const conn = net.createConnection(port);

conn.on('connect', common.mustCall(function() {
conn.destroy();
conn.on('error', common.mustCall(function(err) {
assert.strictEqual(err.message, 'This socket is closed');
}));
conn.write(Buffer.from('kaboom'), common.mustCall(function(err) {
assert.strictEqual(err.message, 'This socket is closed');
}));
server.close();
}));
}));
14 changes: 14 additions & 0 deletions test/parallel/test-stream-readable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,17 @@ const { inherits } = require('util');

new MyReadable();
}

{
// destroy and destroy callback
const read = new Readable({
read() {}
});
read.resume();

const expected = new Error('kaboom');

read.destroy(expected, common.mustCall(function(err) {
assert.strictEqual(expected, err);
}));
}
15 changes: 15 additions & 0 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,18 @@ const { inherits } = require('util');

new MyWritable();
}

{
// destroy and destroy callback
const write = new Writable({
write(chunk, enc, cb) { cb(); }
});

write.destroy();

const expected = new Error('kaboom');

write.destroy(expected, common.mustCall(function(err) {
assert.strictEqual(expected, err);
}));
}

0 comments on commit ccd3ead

Please sign in to comment.