-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: multiple listen() events fail silently #13149
Conversation
hi there @jasnell @rvagg @addaleax @cjihrig @joyeecheung @evanlucas @gibfahn have updated pr, please run CI |
doc/api/http.md
Outdated
*Note*: The `server.listen()` method may be called multiple times. Each | ||
subsequent call will *re-open* the server using the provided options. | ||
*Note*: | ||
The `server.listen()` method can be called again if and only if there was an error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no reason for the line break after *Note*:
...
*Note*: The `server.listen()` method...
doc/api/http.md
Outdated
subsequent call will *re-open* the server using the provided options. | ||
*Note*: | ||
The `server.listen()` method can be called again if and only if there was an error | ||
during the first *listen* call or you explicitly called `server.close()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
during the first `listen()` call ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid the use of you
in docs :-)
lib/net.js
Outdated
return this.emit('error', er); | ||
} | ||
|
||
this._serverListenHasBeenCalled = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than introducing a new state variable for tracking, just look for the existence of the _handle
. If it is not undefined
or null
, the server is listening, otherwise it is not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the server._handle
has been manually set to a non-listening wrap though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. But I think that the _ sign clearly says "okay, you know what you doing by accessing , sort of, private field. You are on your own way now".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea but the implementation and docs need a bit of work. Added a few comments. Thank you for working on this.
b07d6f5
to
fbe11dc
Compare
@jasnell updated pr |
lib/net.js
Outdated
@@ -1404,6 +1404,12 @@ Server.prototype.listen = function() { | |||
var options = normalized[0]; | |||
var cb = normalized[1]; | |||
|
|||
if (this._handle) { | |||
var er = new Error('listen method has been called more than once.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other thing I would recommend here is using the new internal/errors
when introducing a new error.
068bcbf
to
c566e77
Compare
@jasnell moved away from EINVAL error, added new one inside errors called ERR_SERVER_ALREADY_LISTEN and updated tests. |
This is an interesting edge case. Would calling |
@cjihrig I'd say it's a programmer error. |
@cjihrig I think the theory is, if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work, very clean PR! 👍
@eduardbcom this needs a rebase and as far as I see it the emit should be changed to throwing right away. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One comment, otherwise LGTM
c566e77
to
eadf97a
Compare
@BridgeAR done |
})); | ||
} | ||
|
||
// Thrid test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: typo (can be fixed while landing though)
@gibfahn @cjihrig @joyeecheung @jasnell you might want to have another look as the emit now changed to a throw instead. |
Still LGTM but I'd like to get more @nodejs/ctc reviews |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
doc/api/http.md
Outdated
*Note*: The `server.listen()` method may be called multiple times. Each | ||
subsequent call will *re-open* the server using the provided options. | ||
*Note*: The `server.listen()` method can be called again if and only if there was an error | ||
during the first `server.listen()` call or `server.close()` method has been called. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop the word "method" after server.close()
in this sentence.
doc/api/http.md
Outdated
subsequent call will *re-open* the server using the provided options. | ||
*Note*: The `server.listen()` method can be called again if and only if there was an error | ||
during the first `server.listen()` call or `server.close()` method has been called. | ||
Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error would be thrown. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be -> will be
doc/api/http.md
Outdated
*Note*: The `server.listen()` method may be called multiple times. Each | ||
subsequent call will *re-open* the server using the provided options. | ||
*Note*: The `server.listen()` method can be called again if and only if there was an error | ||
during the first `server.listen()` call or `server.close()` method has been called. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comments apply to this paragraph and the one below.
const server = net.Server(); | ||
|
||
server.listen(common.mustCall( | ||
() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit, but could you move this to the previous line. It saves a level of indentation.
Problem: It's possible to run listen() on a net.Server that's already listening to a port. The result is silent failure, with the side effect of changing the connectionKey and or pipeName. Solution: throw an error if listen method called more than once. close() method should be called between listen() method calls. Refs: nodejs#8294 Fixes: nodejs#6190 Fixes: nodejs#11685
eadf97a
to
2cd52c9
Compare
Thank you folks for your comments. Updated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM if CI is happy
CI failures are unrelated, this can land. |
Landed as b24e269. |
Thanks @eduardbcom for your contribution! |
Problem: It's possible to run listen() on a net.Server that's already listening to a port. The result is silent failure, with the side effect of changing the connectionKey and or pipeName. Solution: throw an error if listen method called more than once. close() method should be called between listen() method calls. Refs: #8294 Fixes: #6190 Fixes: #11685 PR-URL: #13149 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Problem: It's possible to run listen() on a net.Server that's already listening to a port. The result is silent failure, with the side effect of changing the connectionKey and or pipeName. Solution: throw an error if listen method called more than once. close() method should be called between listen() method calls. Refs: nodejs/node#8294 Fixes: nodejs/node#6190 Fixes: nodejs/node#11685 PR-URL: nodejs/node#13149 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
net
Description of change
Problem: It's possible to run listen() on a net.Server that's already listening to a port.
The result is silent failure, with the side effect of changing the _connectionKey and or _pipeName.
Solution: emit error if listen method called more than once.
close() method should be called between listen() method calls.
Refs: #8294
Fixes: #6190
Fixes: #11685
Previous PR #8419