-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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: close connection if no 'connection' listener #32516
base: main
Are you sure you want to change the base?
Conversation
A `net.Server` without a `'connection'` event listener would accept incoming connections but then proceeded to drop them into the void: the connections stay around, using up resources, but can no longer be reached by the program - i.e., they become resource leaks. Avoid that by closing the connection when there is no listener. It would be even better to not accept connections at all but libuv does not currently support that because It's Complicated.
deeb645
to
41475dc
Compare
@bnoordhuis Looks like the added test times out in CI. |
net.createServer(common.mustCall()).listen(function() { | ||
const server = this; | ||
const { address: host, port } = server.address(); | ||
net.connect(port, host).once('end', () => 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.
should probably be once('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.
@bnoordhuis: I think if you change this to 'close'
we can land this PR.
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.
@ronag At least locally for me, that doesn’t work 😕
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.
I'll take a look this week.
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.
@addaleax This seems to be related to a bug on the tcp handle. _handle.readStart is called but _handle.onread is never invoked, in this case I guess it should be an error such as ECONNRESET.
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.
This is probably a bug that also affects the self.maxConnections
guard.
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.
@ronag I feel like we had a conversation about this recently, assuming that we’re talking about the client handle here – namely that .close()
doesn’t necessarily actually send a RST? I guess that means that we should shutdown the handle here (or .end()
the created stream, which is a bit more expensive but avoids having to manually call .shutdown()
)?
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.
namely that .close() doesn’t necessarily actually send a RST
I thought this wasn't necessary. That's at least what I got from the conversation and that the handle should detect ungraceful disconnection?
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.
8ae28ff
to
2935f72
Compare
@@ -1536,6 +1536,11 @@ function onconnection(err, clientHandle) { | |||
return; | |||
} | |||
|
|||
if (self.listenerCount('connection') === 0) { |
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.
I have a feeling this might be expensive for all incoming connections. Instead, you can check the return value of emit
: https://nodejs.org/api/events.html#events_emitter_emit_eventname_args.
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
Closing this because it has stalled. Feel free to reopen if this PR is still relevant, or to ping the collaborator who labelled it stalled if you have any questions. |
This needs a rebase. |
A
net.Server
without a'connection'
event listener would acceptincoming connections but then proceeded to drop them into the void:
the connections stay around, using up resources, but can no longer
be reached by the program - i.e., they become resource leaks.
Avoid that by closing the connection when there is no listener.
It would be even better to not accept connections at all but libuv
does not currently support that because It's Complicated.