Skip to content

Commit 1f73c8a

Browse files
committed
net: Fix invalid write after end error
Don't error if not ended. Fixes: #36029
1 parent adae822 commit 1f73c8a

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/net.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ function afterShutdown() {
434434
// of the other side sending a FIN. The standard 'write after end'
435435
// is overly vague, and makes it seem like the user's code is to blame.
436436
function writeAfterFIN(chunk, encoding, cb) {
437+
if (!this.writabledEnded) {
438+
return stream.Duplex.prototype.write.call(this, chunk, encoding, cb);
439+
}
440+
437441
if (typeof encoding === 'function') {
438442
cb = encoding;
439443
encoding = null;
@@ -947,7 +951,6 @@ Socket.prototype.connect = function(...args) {
947951
this._unrefTimer();
948952

949953
this.connecting = true;
950-
this.writable = true;
951954

952955
if (pipe) {
953956
validateString(path, 'options.path');

test/parallel/test-net-writable.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const net = require('net');
6+
7+
net.createServer(function(s) {
8+
this.close();
9+
s.end();
10+
}).listen(0, 'localhost', function() {
11+
const socket = net.connect(this.address().port, 'localhost');
12+
socket.on('end', common.mustCall(() => {
13+
assert.strictEqual(socket.writable, true);
14+
socket.write('hello world');
15+
}));
16+
});
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Readable, Transform } = require('stream');
4+
5+
const src = new Readable({
6+
read() {
7+
this.push(Buffer.alloc(20000));
8+
}
9+
});
10+
11+
const dst = new Transform({
12+
transform(chunk, output, fn) {
13+
this.push(null);
14+
fn();
15+
}
16+
});
17+
18+
src.pipe(dst);
19+
dst.resume();
20+
dst.once('end', common.mustCall());

0 commit comments

Comments
 (0)