Skip to content

node:http: a cluster worker's listen({ fd }) listens on the socket of the primary - #44015

Open
robobun wants to merge 1 commit into
robobun/bf6b14f4/serve-listen-fdfrom
robobun/bf6b14f4/http-worker-listen-fd
Open

robobun wants to merge 1 commit into
robobun/bf6b14f4/serve-listen-fdfrom
robobun/bf6b14f4/http-worker-listen-fd

Conversation

@robobun

@robobun robobun commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #22559

Stacked on #44013. #45186 contains this change.

Problem

Fix

  • The worker asks the primary through cluster._getServer and passes the descriptor that it receives to Bun.serve({ fd }).
  • close() and a disconnect tell the primary that the worker left. When the last worker leaves, the primary closes its descriptor, as in Node.
  • The worker drops an answer that arrives after close() or one more listen(), also one that throws.
  • Verified: serve-listen.test.ts (5 tests fail on the base branch, Node v26.3.0 passes the same fixtures), node-http.test.ts, cluster.test.ts.

Background

  • A cluster primary talks to each worker over an IPC channel. Its SharedHandle sends the descriptor of a shared socket over that channel.
  • The query sets sharedOnly, so the primary never answers with a round-robin handle. An http server cannot take connections that the primary accepted.
  • Weighed: keep ENOTSUP, which is stricter than Node. Or listen on descriptor N of the worker, which is another socket.

Downsides

  • Two listen({ fd }) on one descriptor in one worker make the primary close it too early. The defect is in primary.ts on main, for net.Server too.
  • Outside a worker, listen() executes 1 more branch on a module constant and close() 2 more. Each createServer() stores 2 more fields. _http_server.js grows by 1,747 B.
Notes
  • node:http: cluster workers listen through the primary's shared handle, Bun.serve({ fd }) #45186 has this change, with the same primary fixture and 4 of the 5 tests. It also sends every other listen() of a worker through the primary, which changes the behaviour of each http server in a cluster. This PR changes only listen({ fd }) in a worker. If node:http: cluster workers listen through the primary's shared handle, Bun.serve({ fd }) #45186 lands, this PR is not needed.
  • The self-review of Bun.serve({ fd }) and node:http listen({ fd }): serve on an inherited socket #44013 asked for this change and probed a prototype of it. This PR is that prototype with tests.
  • The owner of the shared handle is the server, as in net.ts. The disconnect of a worker calls server.close(callback). Since node:http: the server follows Node (request body, framing, response finish, lifecycle) #43557, close() registers each callback on the close event, so a callback of an earlier close() that still waits cannot make it throw.
  • The errors of the primary arrive as bind errors with address: null, as for net.Server in a worker. A descriptor of the primary that is not a socket gives EINVAL.
  • The primary validates the descriptor with clusterValidateFd (src/js/internal/cluster/SharedHandle.ts), which exists on main for net.Server.
  • As in Node v26.3.0, exclusive has no effect on listen({ fd }): the fd branch of listen in lib/net.js passes no exclusive to listenInCluster.
  • A process with NODE_UNIQUE_ID in its environment and no channel listens on its own descriptor (Bun.serve({ fd }) and node:http listen({ fd }): serve on an inherited socket #44013). A child with a channel to a parent that is not a primary waits for an answer, as net.Server.listen() does on main. node:cluster: do not pass NODE_UNIQUE_ID to a worker's Bun.spawn children #43768 is the open change for the inherited variable.
  • Measured with the release profile, Linux x64, main 179664ea89: _http_server.js 101,195 to 102,942 B. The release binary embeds the file, so its text grows by the same number. Property reads per http.listen(options) 5 to 5. The server object has 2 more fields, kClusterHandle and kClusterListeningId: 2 stores in the constructor. A worker executes 1 read and 1 store of kClusterListeningId per listen() and per close().
  • The current commit is a rebase onto main 179664ea89 with no change of content. Since 2026-10-23 the worker changes its listening id right after the ERR_SERVER_ALREADY_LISTEN check, before it reads an option, which is the order of lib/net.js in Node v26.3.0. So a second listen() that throws also makes the pending answer of the primary invalid. The new test row calls listen({ fd }) and then listen({ fd: -1 }). With the old order the server listens and the row fails.
  • Debug build of the current commit, Linux x64, with --timeout 60000: serve-listen.test.ts 72 pass, node-http.test.ts 329 pass, node-http-server-close-drain.test.ts 31 pass, cluster.test.ts 41 pass. 9 cluster scripts of the Node test suite exit with 0.
  • With the default limit of 5 s, the cluster tests of this PR take 2.4 to 9.6 s on my machine from run to run, because each starts a debug primary and its workers.
  • Not run: macOS outside CI, and a cluster worker that listens on a descriptor on Windows. By reading, the listen of a Windows worker fails with EINVAL, as it does outside a cluster.
  • bun --watch keeping the socket across the restart is not in this stack.

no test proof · iteration 2 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/http/serve-listen.test.ts

@robobun

robobun commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

How I reproduced it: a parent process binds a socket and starts a cluster primary with that socket as descriptor 3. Each worker calls http.createServer().listen({ fd: 3 }). The fixtures are test/js/node/http/node-http-listen-fd-cluster-fixture.ts and its worker.

The stack:

  1. Bun.serve: make every reader of the listen address exhaustive #44012: groundwork, merged.
  2. Bun.serve({ fd }) and node:http listen({ fd }): serve on an inherited socket #44013: Bun.serve({ fd }) and http.Server.listen({ fd }) serve on an inherited socket.
  3. node:http: a cluster worker's listen({ fd }) listens on the socket of the primary #44015 (this PR), based on the branch of Bun.serve({ fd }) and node:http listen({ fd }): serve on an inherited socket #44013.

#45186 contains this change, with the same primary fixture and 4 of the 5 tests. It also sends every other listen() of a worker through the primary. This PR changes only listen({ fd }) in a worker. If #45186 lands, this PR is not needed.

Comment thread src/js/node/_http_server.ts Resolved
Comment thread src/js/node/_http_server.ts Resolved
@robobun

robobun commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator Author
Updated 5:57 PM PT - Sep 25th, 2026

@robobun, your commit 36cc358 is building: #120798

@robobun

robobun commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 9:36 AM PT - Oct 25th, 2026

✅ @robobun, your commit 0fa6dc8e15bedc4d9058b434b968481803526b14 passed in Build #132632! 🎉


🧪   To try this PR locally:

bunx bun-pr 44015

That installs a local version of the PR into your bun-44015 executable, so you can run:

bun-44015 --bun

@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from 36cc358 to 89570a0 Compare September 26, 2026 01:15
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from f8e07c7 to d79722c Compare September 26, 2026 01:15

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread src/js/node/_http_server.ts Resolved
Comment thread src/js/node/_http_server.ts Resolved
Comment thread src/js/node/_http_server.ts Resolved
Comment thread src/js/node/_http_server.ts Resolved
Comment thread src/js/node/_http_server.ts Resolved
Comment thread src/js/node/_http_server.ts Resolved
Comment thread test/js/bun/http/serve-listen.test.ts Resolved
Comment thread test/js/bun/http/serve-listen.test.ts Resolved
Comment thread src/js/node/_http_server.ts Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Findings marked 🟡 are optional suggestions and need no follow-up push.

Still open from earlier reviews (10):

  • 🔴 src/js/node/_http_server.ts:728 — A worker that re-listens on a descriptor after close(cb) and is then disconnected by the primary never finishes disconn…
  • Also unresolved: 9 minor or pre-existing.

If you have decided not to act on one of these findings, resolve its thread (a reply alone leaves it open) and the next review stops counting it. To review this commit again now, use Re-run on its "Claude Code Review" check.

Comment thread test/js/bun/http/serve-listen.test.ts Resolved
@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from 89570a0 to 667efbc Compare September 26, 2026 04:51
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from d79722c to 9450e1e Compare September 26, 2026 04:51

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

@robobun

robobun commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator Author

@robobun wake up!!

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

@robobun wake up!!

@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from 667efbc to 3ad7c22 Compare September 29, 2026 00:21
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from 9450e1e to ff219da Compare September 29, 2026 00:22
@robobun

robobun commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator Author

I rebased this PR onto the new head of #44013. The new head is ff219dab17.

What the rebase changed:

Checked on the new head with a debug build on Linux x64: the 4 cluster tests in serve-listen.test.ts pass. My machine is under heavy load, so they needed more than the 5 s default there. The last CI run passed on every lane.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from 3ad7c22 to ad8ca73 Compare September 29, 2026 06:35
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from ff219da to 5d2d093 Compare September 29, 2026 06:35

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from 53cec12 to 682a069 Compare October 17, 2026 19:55
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from c6d5fa8 to 4628864 Compare October 17, 2026 19:55

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from 682a069 to fa5b0a4 Compare October 21, 2026 09:36
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from 4628864 to 3ac1862 Compare October 21, 2026 09:36

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from fa5b0a4 to 6ef0ac4 Compare October 21, 2026 13:44
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from 3ac1862 to 5132d14 Compare October 21, 2026 13:44

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread src/js/node/_http_server.ts Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from 1540f75 to 61d41c9 Compare October 23, 2026 15:19
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from ed8a9f6 to 225b7ab Compare October 23, 2026 15:20

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

… the primary

In a cluster worker the number names a descriptor of the primary, as in
Node. The worker asks the primary through cluster._getServer, as net.Server
does, and listens on the descriptor that the primary shares.
@robobun
robobun force-pushed the robobun/bf6b14f4/serve-listen-fd branch from 61d41c9 to 9aecc57 Compare October 25, 2026 15:39
@robobun
robobun force-pushed the robobun/bf6b14f4/http-worker-listen-fd branch from 225b7ab to 0fa6dc8 Compare October 25, 2026 15:39

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reviewed the latest push and found no bugs; the points raised in earlier threads (listening-id bump before option validation, ExceptionWithHostPort with the bunHint note, close() queuing its callback on 'close' so _disconnect cannot throw, worker exit wired in the primary fixture, concurrent describe with the https variant) are all reflected in the code now. Because this adds a cluster-IPC handle lifecycle to http.Server and the tests could not be run on this machine, a human look at the ownership paths would still be worthwhile.

What was reviewed:

  • listenFdInWorker against listenInCluster in src/js/node/net.ts:4336-4400 and child.ts shared()/makeSharedHandle: every branch (late answer, errno/no sharedFd, kRealListen throw, success) pairs the handle with close() and clears kClusterOwner; adopted = true only after kRealListen succeeds, matching net.ts.
  • close() in a worker: kClusterListeningId++ precedes the not-running early return (so a close() before the answer invalidates it, as Node's _listeningId++), and releaseClusterHandle runs only once a listener existed; _disconnect calling owner.close(cb) resolves via once("close").
  • Fixture ordering: the primary's SharedHandle.add replies synchronously, so the queryServer answer always precedes the "echo" reply and the act:close precedes each worker's final message, making the fd: "closed" assertions deterministic.
  • Test matrix covers http/https, close vs cluster.disconnect, early close and early throwing listen, and the primary-side EINVAL error path; no per-test timeouts, describe.concurrent, await using before assertions.
Extended reasoning...

The change adds a listenFdInWorker path to src/js/node/_http_server.ts (57 lines) so a cluster worker's http.Server.listen({ fd }) obtains the primary's shared socket via cluster._getServer, plus per-server kClusterHandle/kClusterListeningId state released in close(); it updates one docs line and replaces the old ENOTSUP test with a four-case matrix driven by two new fixtures. It touches no auth, crypto, or injection surface, only process-internal IPC handle ownership between cluster primary and worker. The code mirrors the existing net.ts listenInCluster pattern closely and the test matrix is thorough, but the handle lifecycle across close/disconnect/late-answer paths is subtle, the tests could not be executed in this environment (PR evidence also defers to CI), and a maintainer commented mid-thread with content not visible here, so a human review remains worthwhile rather than approving outright.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants