Skip to content

Bun.serve: make every reader of the listen address exhaustive - #44012

Merged
Jarred-Sumner merged 5 commits into
mainfrom
robobun/bf6b14f4/serve-address-readers
Sep 26, 2026
Merged

Jarred-Sumner merged 5 commits into
mainfrom
robobun/bf6b14f4/serve-address-readers

Conversation

@robobun

@robobun robobun commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #22559

Behaviour change: none

Problem

  • Address, the listen target of Bun.serve, has two variants. Most of its readers cannot take a third one. Four end in unreachable!() (ServerConfig.rs:666, :675, :1385, server_body.rs:2581). Seven more use if let or matches!.
  • A new variant compiles at those sites and then panics or takes the wrong branch at run time. Bun.serve({ fd }) and node:http listen({ fd }): serve on an inherited socket #44013 adds one for Bun.serve({ fd }).

Fix

  • ServerConfig::from_js keeps the port, the hostname and the unix path in locals and builds Address in one place.
  • Address::is_unix() replaces the matches! readers. The if let readers become exhaustive matches. No unreachable!() arm on Address is left.
  • HttpContext::listen and listen_unix share tcpListenOptions and unrefListenSocket. This PR deletes uws_app_listen, which has no caller.
  • Verified: the serve-listen, bun-serve-args, serve-http3, node-http, hot and bake/dev/esm tests pass. serve.test.ts fails the same 2 tests as main (Notes).

Background

  • ServerConfig::from_js parses the options of Bun.serve() and server.reload().
  • requestIP, timeout and the /bun:info route read the variant on each call.
  • Weighed: add the new arm at each reader in the feature PR. A reader that the PR misses still compiles with if let or matches!. With an exhaustive match the compiler finds it.

Downsides

  • Release text grows by 512 B. ServerConfig::from_js goes from 16,314 to 16,587 B.
  • None found at run time. requestIP, timeout and is_allowed_host_header have the same instructions as on main. 60 inputs to http.listen and Bun.serve give the same outcome.
Notes
  • Builds compared: release, Linux x64, merge base c86bd18736. size text 80,829,494 to 80,830,006 B. The bun file is 80,995,912 B in both.
  • on_bun_info_request differs from main in two constants, which are offsets of the same strings. Every other instruction is the same.
  • The release binary of main has no uws_app_listen symbol: the linker already drops it. The deletion removes source only. The function has no Rust declaration.
  • tcpListenOptions is the one place that adds LIBUS_LISTEN_DEFER_ACCEPT. node:http: skip TCP_DEFER_ACCEPT so server.setTimeout fires on time for silent clients #36074 removes that flag for node:http.
  • The 60 inputs are 47 forms of http.Server.listen (no arguments, port, path, host, options objects, fd: undefined, handles, bad ports) and 13 forms of Bun.serve (port, hostname, host, unix, unix with port, hostname with unix, reusePort, ipv6Only, unknown keys). Each one ran on the build of main and on the build of this PR.
  • The last commit adds three assertions to bun-serve-args.test.ts. They read the messages of the errors for unix with hostname and for a unix socket with only HTTP/3. They pass on main and on this PR.
  • serve.test.ts has two failures in my container, on main and on this PR: should use correct error when using a root range port(#7187) (the container runs as root) and only serves /bun:info to loopback clients in development mode.

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

ServerConfig::from_js keeps the listen target in locals and builds
Address in one place. Address::is_unix() replaces the matches! readers.
The four unreachable!() arms on Address are gone.
@robobun

robobun commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

This PR changes no behaviour, so it has no reproduction. It is the groundwork for #44013, which fixes #22559.

The stack:

  1. Bun.serve: make every reader of the listen address exhaustive #44012 (this PR): every reader of the listen address is exhaustive.
  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: listen({ fd }) in a node:cluster worker.

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: oven-sh/bun/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Essentials

Run ID: 9c5c74f5-cba3-4885-b6ba-78d90980fa0b

📥 Commits

Reviewing files that changed from the base of the PR and between db065a9 and b060143.

📒 Files selected for processing (2)
  • packages/bun-uws/src/HttpContext.h
  • src/runtime/server/ServerConfig.rs
💤 Files with no reviewable changes (1)
  • src/runtime/server/ServerConfig.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.


Walkthrough

The change restructures server address parsing and updates listener setup, teardown, and runtime address checks. The uWS TCP listener adds deferred accept options and shared socket unreferencing. The older uws_app_listen wrapper is removed.

Changes

Server address and listener handling

Layer / File(s) Summary
Address configuration
src/runtime/server/ServerConfig.rs, test/js/bun/http/bun-serve-args.test.ts
Server configuration stores the port, hostname, and Unix path separately, then constructs the final address after parsing. Base-URI hostname selection uses that address. Tests assert the hostname and Unix path conflict message and the HTTP/3 Unix-socket restriction.
Listener setup and teardown
src/runtime/server/mod.rs, packages/bun-uws/src/HttpContext.h, src/uws_sys/libuwsockets.cpp
Server listen validation and Unix socket cleanup use address matches. The uWS TCP listener adds deferred accept options and shares socket unreferencing. The uws_app_listen wrapper is removed.
Runtime address checks
src/runtime/bake/DevServer.rs, src/runtime/server/server_body.rs
Host-header checks and server request and address accessors use the updated TCP and Unix address logic.

Suggested reviewers: jarred-sumner

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to b0601

No actionable merge-blocking risk remains; the change is ready for normal checks.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: making listen-address readers exhaustive in Bun.serve.
Description check ✅ Passed The description explains the problem, fix, scope, behavior expectations, and verification results. It does not use the exact template headings, but it provides the required information and is substant…

Comment @coderabbitai help to get the list of available commands.

@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.

Beyond the inline finding, I also checked the from_js reordering for observable differences: nothing reads args.address between the removed in-place writes (ServerConfig.rs:1078, :1100) and the late assembly at :1288, so port/hostname precedence and the hostname+unix conflict error are unchanged; the flattened get_address/get_hostname in server_body.rs return the same value on every path (listener, h3 listener, config fallback, unix early-return); and listen_unix in HttpContext.h still passes options without LIBUS_LISTEN_DEFER_ACCEPT, as before.

Extended reasoning...

The change is a behavior-preserving refactor of Bun.serve's listen-address handling across ServerConfig.rs, server_body.rs, mod.rs, DevServer.rs, and the uws listen helpers, plus deletion of the uncalled uws_app_listen export; it touches no auth, crypto, or input-parsing surface beyond the existing hostname/unix option checks. The one confirmed finding is the absence of an automated test for the rewired unix/hostname error paths, which is why the review defers rather than approves.

Comment thread src/runtime/server/ServerConfig.rs Resolved
The checks for hostname with unix and for a unix socket with only HTTP/3
had no test that reads their message.
Comment thread src/runtime/server/ServerConfig.rs 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.

I reviewed the latest push (including the new assertions in bun-serve-args.test.ts) and found no bugs; since this reshapes how every Bun.serve call assembles its listen target, a human look would still be worthwhile.

What was reviewed:

  • Traced from_js end to end: args.address is not read between struct init and the new late assembly, so deferring it changes nothing; env/transpiler/port precedence, hostname: ""/unix: "" ignored, and the base_uri 0.0.0.0 fallback all match the old path.
  • Compared get_address/get_hostname flattening against the old nested match: listener, h3 listener, config-hostname/localhost fallbacks run in the same order with the same early returns.
  • Confirmed uws_app_listen has no remaining caller or declaration outside vendor/, tls is exported by the harness, and listen_unix still omits LIBUS_LISTEN_DEFER_ACCEPT as before.
Extended reasoning...

The PR refactors how Bun.serve builds and reads its listen Address across ServerConfig.rs, server_body.rs, mod.rs and DevServer.rs, extracts two small helpers in HttpContext.h, deletes an uncalled C++ extern, and tightens three test assertions. It touches the host-header check and requestIP paths only by replacing matches! with an equivalent is_unix() helper, so no security-sensitive logic changes. The bug hunt ran dry and my own trace found the refactor behavior-preserving, and the new commit added the exact-message tests my earlier inline comment asked for. I did not approve outright because the change is a moderate-size restructuring of the core server config parser rather than a mechanical edit, and no CODEOWNERS entry covers these files to route it automatically.

@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.

LGTM — the refactor traces as behavior-preserving, and the test gap raised earlier is now closed by db065a9 (exact messages for both unix-conflict errors plus the new http3-only unix case).

What was reviewed:

  • from_js: args.address is read only at lines 1291 and 1374, both after the single late assignment at 1286, and the function has one Ok(args) exit, so no path observes the unseeded Address::default().
  • get_address/get_hostname flattening, is_allowed_host_header, and the stop() unlink arm: each Tcp{hostname: None}/Unix arm reproduces the old fall-through result (NULL, "localhost", false, no-op).
  • uws_app_listen: no callers in Rust, C++ or headers; tcpListenOptions keeps LIBUS_LISTEN_DEFER_ACCEPT on the TCP path only, as before.
  • New http3 test: http2 defaults to false (ServerConfig.rs:101) and tls is parsed before the unix check, so the assertion is live.
Extended reasoning...

The PR rewrites every reader of the Bun.serve listen Address as an exhaustive match, builds the address once at the end of ServerConfig::from_js, dedupes the uWS listen helpers, deletes the dead uws_app_listen C ABI, and tightens two toThrow() assertions plus adds one test. It touches the dev-server host-header allowlist, but the rewritten match returns false for exactly the same inputs as the old if let fall-through, so the security gate's polarity is unchanged. The bug hunt ran dry, no CODEOWNER covers the changed paths, the only prior objection (from this bot) was addressed by a later commit with an exact-message test, and the refactor was verified line-by-line against the old control flow.

@Jarred-Sumner
Jarred-Sumner merged commit b12539c into main Sep 26, 2026
11 checks passed
@Jarred-Sumner
Jarred-Sumner deleted the robobun/bf6b14f4/serve-address-readers branch September 26, 2026 01:50
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