Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5c0ddbc

Browse filesBrowse 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
Copy full SHA for 5c0ddbc

File tree

Expand file treeCollapse file tree

3 files changed

+25
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+25
-3
lines changed
Open diff view settings
Collapse file

‎lib/net.js‎

Copy file name to clipboardExpand all lines: lib/net.js
+6-1Lines 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
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');
Collapse file

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

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
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+
}));
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-socket-write-after-fin-error.js
+4-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.