Skip to content

Commit

Permalink
net: fix invalid write after end error
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ronag authored and aduh95 committed Nov 10, 2020
1 parent f5c508c commit f7f0a6a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
NumberParseInt,
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectApply,
Symbol,
} = primordials;

Expand Down Expand Up @@ -434,6 +435,11 @@ function afterShutdown() {
// of the other side sending a FIN. The standard 'write after end'
// is overly vague, and makes it seem like the user's code is to blame.
function writeAfterFIN(chunk, encoding, cb) {
if (!this.writableEnded) {
return ReflectApply(
stream.Duplex.prototype.write, this, [chunk, encoding, cb]);
}

if (typeof encoding === 'function') {
cb = encoding;
encoding = null;
Expand Down Expand Up @@ -947,7 +953,6 @@ Socket.prototype.connect = function(...args) {
this._unrefTimer();

this.connecting = true;
this.writable = true;

if (pipe) {
validateString(path, 'options.path');
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-net-writable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

const server = net.createServer(common.mustCall(function(s) {
server.close();
s.end();
})).listen(0, 'localhost', common.mustCall(function() {
const socket = net.connect(this.address().port, 'localhost');
socket.on('end', common.mustCall(() => {
assert.strictEqual(socket.writable, true);
socket.write('hello world');
}));
}));
6 changes: 4 additions & 2 deletions test/parallel/test-socket-write-after-fin-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const server = net.createServer(function(sock) {
});
sock.on('end', function() {
gotServerEnd = true;
sock.write(serverData);
sock.end();
setImmediate(() => {
sock.write(serverData);
sock.end();
});
});
server.close();
});
Expand Down

0 comments on commit f7f0a6a

Please sign in to comment.