Skip to content

lib: make sure close the net server - #51929

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
theanarkh:make_sure_net_server_close
Mar 3, 2024
Merged

lib: make sure close the net server#51929
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
theanarkh:make_sure_net_server_close

Conversation

@theanarkh

Copy link
Copy Markdown
Contributor

If perform asynchronous operations in the listen function of net.js (execute dns.lookup or ask the main process to create a server), the server will not close properly.

The example is as follows.

// The "closed" TCP server will keep the process because // after calling close function, a TCP handle is created in the callback of `dns.lookup`.constnet=require('net');net.createServer().listen(9999,'localhost').close();

See

functionlookupAndListen(self,port,address,backlog,exclusive,flags){
.

  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/net

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem. labels Feb 29, 2024

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollinamcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 29, 2024
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 29, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@mcollinamcollina added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. and removed needs-ci PRs that need a full CI run. labels Mar 1, 2024
@lpinca

Copy link
Copy Markdown
Member

Does it work if the user does something like this?

net.createServer().listen(9999,'localhost').close().listen(8888,'localhost');

@theanarkh

Copy link
Copy Markdown
ContributorAuthor

Yes, close make the callback invalid of the first listen and the second listen executes normally.
I tested it locally, it ended up listening on port 8888 and just emit listening event once(before this PR, it will emit listening event twice).

@marco-ippolitomarco-ippolito added the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 3, 2024
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 3, 2024
@nodejs-github-bot
nodejs-github-bot merged commit 29ec7e9 into nodejs:mainMar 3, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 29ec7e9

targos pushed a commit that referenced this pull request Mar 7, 2024
PR-URL: #51929
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
@targostargos mentioned this pull request Mar 7, 2024
richardlau pushed a commit that referenced this pull request Mar 25, 2024
PR-URL: #51929
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
@richardlaurichardlau mentioned this pull request Mar 25, 2024
alii added a commit to oven-sh/bun that referenced this pull request Jul 8, 2026
- net.Server.close(): bump kClusterListeningId first so a listen() reply
arriving after close() is discarded (nodejs/node#51929).
- ipc: on serialize failure, pop the just-enqueued handle so the next drain
cannot spuriously close the user's socket via close_on_complete; only
pause the sender's socket after serialize succeeds.
- ipc: defer the sent-handle .close() to next tick so complete() no longer
runs synchronous JS while every caller holds &mut SendQueue.
- ipc: on channel-closed drain, do not fire send callbacks with null for
items whose bytes never left the process (Node parity).
- cluster: validate a worker-supplied listen({fd}) is a real socket in the
primary before SharedHandle stores/closes it (Node's guessHandleType gate).
- net: onClusterConnection clears socket.connecting after the fd adopt so
remoteAddress/_write/readyState observe the accepted-socket state.
- ipc: Windows child->parent handle sends now target the pipe's actual
peer PID (uv_pipe_t.ipc_remote_pid) instead of uv_os_getppid().
- clusterRawBind: on POSIX, do not fall back to the v4 wildcard on
EADDRINUSE (a v6-only occupant would mask the error; Node's fallback
never fires on EADDRINUSE either).
- http.Server: replace the stale 'IPC doesn't support handles' TODO with
an accurate note (Bun.serve fd adoption is the remaining gap).
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.netIssues and PRs related to the net subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@theanarkh@nodejs-github-bot@lpinca@mcollina@ShogunPanda@benjamingr@anonrig@marco-ippolito