From 1efb87774d72a1b4cee916daaf387c00e43933f3 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 7 Dec 2019 16:58:18 +0100 Subject: [PATCH] net: noop destroyed socket _write and _read can be called from 'connect' after Socket.destroy() has been called. This should be a noop. Refs: https://github.com/nodejs/node/pull/30837 --- lib/net.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/net.js b/lib/net.js index 02fd18748036a3..f3c0ff62235b1f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -91,7 +91,8 @@ const { ERR_SERVER_ALREADY_LISTEN, ERR_SERVER_NOT_RUNNING, ERR_SOCKET_BAD_PORT, - ERR_SOCKET_CLOSED + ERR_SOCKET_CLOSED, + ERR_STREAM_DESTROYED }, errnoException, exceptionWithHostPort, @@ -571,7 +572,11 @@ Socket.prototype._read = function(n) { if (this.connecting || !this._handle) { debug('_read wait for connection'); - this.once('connect', () => this._read(n)); + this.once('connect', () => { + if (!this.destroyed) { + this._read(n) + } + }); } else if (!this._handle.reading) { tryReadStart(this); } @@ -757,7 +762,11 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) { this._pendingData = data; this._pendingEncoding = encoding; this.once('connect', function connect() { - this._writeGeneric(writev, data, encoding, cb); + if (this.destroyed) { + cb(new ERR_STREAM_DESTROYED('write')); + } else { + this._writeGeneric(writev, data, encoding, cb); + } }); return; }