Repository navigation
Bun.serve: make every reader of the listen address exhaustive - #44012
Conversation
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.
|
Status This PR changes no behaviour, so it has no reproduction. It is the groundwork for #44013, which fixes #22559. The 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 configurationConfiguration used: Repository: oven-sh/bun/.coderabbit.yaml Review profile: ASSERTIVE Plan: Essentials Run ID: 📒 Files selected for processing (2)
💤 Files with no reviewable changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review. WalkthroughThe 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 ChangesServer address and listener handling
Suggested reviewers: Priority: ⬇️ Low Merge Risk: ⚪ Minimal · up to No actionable merge-blocking risk remains; the change is ready for normal checks. 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
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.
The checks for hostname with unix and for a unix socket with only HTTP/3 had no test that reads their message.
There was a problem hiding this comment.
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_jsend to end:args.addressis not read between struct init and the new late assembly, so deferring it changes nothing; env/transpiler/portprecedence,hostname: ""/unix: ""ignored, and the base_uri0.0.0.0fallback all match the old path. - Compared
get_address/get_hostnameflattening against the old nested match: listener, h3 listener, config-hostname/localhostfallbacks run in the same order with the same early returns. - Confirmed
uws_app_listenhas no remaining caller or declaration outside vendor/,tlsis exported by the harness, andlisten_unixstill omitsLIBUS_LISTEN_DEFER_ACCEPTas 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.
There was a problem hiding this comment.
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.addressis read only at lines 1291 and 1374, both after the single late assignment at 1286, and the function has oneOk(args)exit, so no path observes the unseededAddress::default().get_address/get_hostnameflattening,is_allowed_host_header, and thestop()unlink arm: eachTcp{hostname: None}/Unixarm reproduces the old fall-through result (NULL, "localhost", false, no-op).uws_app_listen: no callers in Rust, C++ or headers;tcpListenOptionskeepsLIBUS_LISTEN_DEFER_ACCEPTon the TCP path only, as before.- New http3 test:
http2defaults to false (ServerConfig.rs:101) andtlsis 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.
Part of #22559
Behaviour change: none
Problem
Address, the listen target ofBun.serve, has two variants. Most of its readers cannot take a third one. Four end inunreachable!()(ServerConfig.rs:666,:675,:1385,server_body.rs:2581). Seven more useif letormatches!.Bun.serve({ fd }).Fix
ServerConfig::from_jskeeps the port, the hostname and the unix path in locals and buildsAddressin one place.Address::is_unix()replaces thematches!readers. Theif letreaders become exhaustive matches. Nounreachable!()arm onAddressis left.HttpContext::listenandlisten_unixsharetcpListenOptionsandunrefListenSocket. This PR deletesuws_app_listen, which has no caller.serve-listen,bun-serve-args,serve-http3,node-http,hotandbake/dev/esmtests pass.serve.test.tsfails the same 2 tests as main (Notes).Background
ServerConfig::from_jsparses the options ofBun.serve()andserver.reload().requestIP,timeoutand the/bun:inforoute read the variant on each call.if letormatches!. With an exhaustive match the compiler finds it.Downsides
ServerConfig::from_jsgoes from 16,314 to 16,587 B.requestIP,timeoutandis_allowed_host_headerhave the same instructions as on main. 60 inputs tohttp.listenandBun.servegive the same outcome.Notes
c86bd18736.sizetext 80,829,494 to 80,830,006 B. Thebunfile is 80,995,912 B in both.on_bun_info_requestdiffers from main in two constants, which are offsets of the same strings. Every other instruction is the same.uws_app_listensymbol: the linker already drops it. The deletion removes source only. The function has no Rust declaration.tcpListenOptionsis the one place that addsLIBUS_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.http.Server.listen(no arguments, port, path, host, options objects,fd: undefined, handles, bad ports) and 13 forms ofBun.serve(port,hostname,host,unix,unixwithport,hostnamewithunix,reusePort,ipv6Only, unknown keys). Each one ran on the build of main and on the build of this PR.bun-serve-args.test.ts. They read the messages of the errors forunixwithhostnameand for a unix socket with only HTTP/3. They pass on main and on this PR.serve.test.tshas 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) andonly 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