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: close connection if no 'connection' listener #32516

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bnoordhuis
Copy link
Member

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.

@nodejs-github-bot nodejs-github-bot added the net Issues and PRs related to the net subsystem. label Mar 27, 2020
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.
@bnoordhuis bnoordhuis force-pushed the net-server-no-handler branch from deeb645 to 41475dc Compare April 1, 2020 13:43
@addaleax addaleax added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 2, 2020
@nodejs-github-bot
Copy link
Collaborator

@addaleax addaleax removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 5, 2020
@addaleax
Copy link
Member

addaleax commented Apr 5, 2020

@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());
Copy link
Member

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', ...

Copy link
Member

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.

Copy link
Member

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 😕

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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())?

Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

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

@BridgeAR BridgeAR force-pushed the master branch 2 times, most recently from 8ae28ff to 2935f72 Compare May 31, 2020 12:18
@bnoordhuis bnoordhuis requested a review from a team as a code owner August 10, 2020 16:07
@bnoordhuis bnoordhuis requested a review from a team August 10, 2020 16:07
@@ -1536,6 +1536,11 @@ function onconnection(err, clientHandle) {
return;
}

if (self.listenerCount('connection') === 0) {
Copy link
Member

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.

@aduh95 aduh95 added the stalled Issues and PRs that are stalled. label Oct 16, 2020
@github-actions
Copy link
Contributor

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.

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot closed this Nov 16, 2020
@lpinca lpinca reopened this Nov 16, 2020
@lpinca lpinca added request-ci Add this label to start a Jenkins CI on a PR. and removed stalled Issues and PRs that are stalled. labels Nov 16, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 16, 2020
@nodejs-github-bot
Copy link
Collaborator

@aduh95
Copy link
Contributor

aduh95 commented May 11, 2024

This needs a rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
net Issues and PRs related to the net subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.