From 5c0ddbca01335e1eabeb9c389d68b4069f3f3069 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 8 Nov 2020 19:56:56 +0100 Subject: [PATCH] net: fix invalid write after end error Don't error if not ended. Fixes: https://github.com/nodejs/node/issues/36029 PR-URL: https://github.com/nodejs/node/pull/36043 Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca --- lib/net.js | 7 ++++++- test/parallel/test-net-writable.js | 15 +++++++++++++++ .../parallel/test-socket-write-after-fin-error.js | 6 ++++-- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-net-writable.js diff --git a/lib/net.js b/lib/net.js index 45598114770646..4b3b96c8cd2736 100644 --- a/lib/net.js +++ b/lib/net.js @@ -30,6 +30,7 @@ const { NumberParseInt, ObjectDefineProperty, ObjectSetPrototypeOf, + ReflectApply, Symbol, } = primordials; @@ -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; @@ -947,7 +953,6 @@ Socket.prototype.connect = function(...args) { this._unrefTimer(); this.connecting = true; - this.writable = true; if (pipe) { validateString(path, 'options.path'); diff --git a/test/parallel/test-net-writable.js b/test/parallel/test-net-writable.js new file mode 100644 index 00000000000000..dcbb64e272b830 --- /dev/null +++ b/test/parallel/test-net-writable.js @@ -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'); + })); +})); diff --git a/test/parallel/test-socket-write-after-fin-error.js b/test/parallel/test-socket-write-after-fin-error.js index 4626729c0598a0..64c8143abebbd9 100644 --- a/test/parallel/test-socket-write-after-fin-error.js +++ b/test/parallel/test-socket-write-after-fin-error.js @@ -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(); });