Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: noop destroyed socket #30839

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_handle.onread is already set to a noop after destroy so I don't think this is needed. Also 'connect' should not be emitted if the socket is destroyed while connecting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I see. I can remove this. I still kind of like it since it makes it more explicit.

Copy link
Member

@lpinca lpinca Dec 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be code that is impossible to cover with tests and this is already explicit

node/lib/net.js

Lines 1106 to 1108 in cf5ce2c

if (self.destroyed) {
return;
}

this._read(n)
}
});
} else if (!this._handle.reading) {
tryReadStart(this);
}
Expand Down Expand Up @@ -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;
}
Expand Down