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; }