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

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/net.js

+6-1
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
function writeAfterFIN(chunk, encoding, cb) {
438+
if (!this.writableEnded) {
439+
return ReflectApply(
440+
stream.Duplex.prototype.write, this, [chunk, encoding, cb]);
441+
}
442+
437443
if (typeof encoding === '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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
const server = net.createServer(common.mustCall(function(s) {
7+
server.close();
8+
s.end();
9+
})).listen(0, 'localhost', common.mustCall(function() {
10+
const socket = 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

+4-2
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)