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: #13156Fixes: 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]>
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‎

Lines changed: 4 additions & 1 deletion
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+
}elseif(err&&
14+
(!this._writableState||!this._writableState.errorEmitted)){
1215
process.nextTick(emitErrorNT,this,err);
1316
}
1417
return;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constnet=require('net');
5+
constassert=require('assert');
6+
7+
constserver=net.createServer();
8+
server.listen(0,common.mustCall(function(){
9+
constport=server.address().port;
10+
constconn=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‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,17 @@ const{inherits } = require('util');
160160

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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,18 @@ const{inherits } = require('util');
170170

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

0 commit comments

Comments
(0)