Skip to content

Commit 5c0ddbc

Browse files
ronagcodebytere
authored andcommitted
net: fix invalid write after end error
Don't error if not ended. Fixes: #36029 PR-URL: #36043 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 59af919 commit 5c0ddbc

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎lib/net.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
NumberParseInt,
3131
ObjectDefineProperty,
3232
ObjectSetPrototypeOf,
33+
ReflectApply,
3334
Symbol,
3435
}=primordials;
3536

@@ -434,6 +435,11 @@ function afterShutdown() {
434435
// of the other side sending a FIN. The standard 'write after end'
435436
// is overly vague, and makes it seem like the user's code is to blame.
436437
functionwriteAfterFIN(chunk,encoding,cb){
438+
if(!this.writableEnded){
439+
returnReflectApply(
440+
stream.Duplex.prototype.write,this,[chunk,encoding,cb]);
441+
}
442+
437443
if(typeofencoding==='function'){
438444
cb=encoding;
439445
encoding=null;
@@ -947,7 +953,6 @@ Socket.prototype.connect = function(...args) {
947953
this._unrefTimer();
948954

949955
this.connecting=true;
950-
this.writable=true;
951956

952957
if(pipe){
953958
validateString(path,'options.path');

‎test/parallel/test-net-writable.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
constnet=require('net');
5+
6+
constserver=net.createServer(common.mustCall(function(s){
7+
server.close();
8+
s.end();
9+
})).listen(0,'localhost',common.mustCall(function(){
10+
constsocket=net.connect(this.address().port,'localhost');
11+
socket.on('end',common.mustCall(()=>{
12+
assert.strictEqual(socket.writable,true);
13+
socket.write('hello world');
14+
}));
15+
}));

‎test/parallel/test-socket-write-after-fin-error.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ const server = net.createServer(function(sock) {
2626
});
2727
sock.on('end',function(){
2828
gotServerEnd=true;
29-
sock.write(serverData);
30-
sock.end();
29+
setImmediate(()=>{
30+
sock.write(serverData);
31+
sock.end();
32+
});
3133
});
3234
server.close();
3335
});

0 commit comments

Comments
 (0)