Skip to content

Commit 87cef63

Browse files
mcollinajasnell
authored andcommitted
stream: fix destroy(err, cb) regression
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 <cjihrig@gmail.com> Reviewed-By: Calvin Metcalf <calvin.metcalf@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent b6e1d22 commit 87cef63

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

lib/internal/streams/destroy.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ function destroy(err, cb) {
88
this._writableState.destroyed;
99

1010
if (readableDestroyed || writableDestroyed) {
11-
if (err && (!this._writableState || !this._writableState.errorEmitted)) {
11+
if (cb) {
12+
cb(err);
13+
} else if (err &&
14+
(!this._writableState || !this._writableState.errorEmitted)) {
1215
process.nextTick(emitErrorNT, this, err);
1316
}
1417
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
const assert = require('assert');
6+
7+
const server = net.createServer();
8+
server.listen(0, common.mustCall(function() {
9+
const port = server.address().port;
10+
const conn = net.createConnection(port);
11+
12+
conn.on('connect', common.mustCall(function() {
13+
conn.destroy();
14+
conn.on('error', common.mustCall(function(err) {
15+
assert.strictEqual(err.message, 'This socket is closed');
16+
}));
17+
conn.write(Buffer.from('kaboom'), common.mustCall(function(err) {
18+
assert.strictEqual(err.message, 'This socket is closed');
19+
}));
20+
server.close();
21+
}));
22+
}));

test/parallel/test-stream-readable-destroy.js

+14
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,17 @@ const { inherits } = require('util');
160160

161161
new MyReadable();
162162
}
163+
164+
{
165+
// destroy and destroy callback
166+
const read = new Readable({
167+
read() {}
168+
});
169+
read.resume();
170+
171+
const expected = new Error('kaboom');
172+
173+
read.destroy(expected, common.mustCall(function(err) {
174+
assert.strictEqual(expected, err);
175+
}));
176+
}

test/parallel/test-stream-writable-destroy.js

+15
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,18 @@ const { inherits } = require('util');
170170

171171
new MyWritable();
172172
}
173+
174+
{
175+
// destroy and destroy callback
176+
const write = new Writable({
177+
write(chunk, enc, cb) { cb(); }
178+
});
179+
180+
write.destroy();
181+
182+
const expected = new Error('kaboom');
183+
184+
write.destroy(expected, common.mustCall(function(err) {
185+
assert.strictEqual(expected, err);
186+
}));
187+
}

0 commit comments

Comments
 (0)