Skip to content

Commit 1efb877

Browse files
committed
net: noop destroyed socket
_write and _read can be called from 'connect' after Socket.destroy() has been called. This should be a noop. Refs: nodejs#30837
1 parent 49fb529 commit 1efb877

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/net.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ const {
9191
ERR_SERVER_ALREADY_LISTEN,
9292
ERR_SERVER_NOT_RUNNING,
9393
ERR_SOCKET_BAD_PORT,
94-
ERR_SOCKET_CLOSED
94+
ERR_SOCKET_CLOSED,
95+
ERR_STREAM_DESTROYED
9596
},
9697
errnoException,
9798
exceptionWithHostPort,
@@ -571,7 +572,11 @@ Socket.prototype._read = function(n) {
571572

572573
if (this.connecting || !this._handle) {
573574
debug('_read wait for connection');
574-
this.once('connect', () => this._read(n));
575+
this.once('connect', () => {
576+
if (!this.destroyed) {
577+
this._read(n)
578+
}
579+
});
575580
} else if (!this._handle.reading) {
576581
tryReadStart(this);
577582
}
@@ -757,7 +762,11 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
757762
this._pendingData = data;
758763
this._pendingEncoding = encoding;
759764
this.once('connect', function connect() {
760-
this._writeGeneric(writev, data, encoding, cb);
765+
if (this.destroyed) {
766+
cb(new ERR_STREAM_DESTROYED('write'));
767+
} else {
768+
this._writeGeneric(writev, data, encoding, cb);
769+
}
761770
});
762771
return;
763772
}

0 commit comments

Comments
 (0)