Skip to content

One listen socket per resolved address (#6) - #13

Merged
Khang5687 merged 5 commits into
fork-mainfrom
feat/multi-bind
Sep 16, 2026
Merged

Khang5687 merged 5 commits into
fork-mainfrom
feat/multi-bind

Conversation

@Khang5687

@Khang5687 Khang5687 commented Sep 16, 2026 •

Copy link
Copy Markdown
Owner

Ticket: #6 (parent spec #1, ADR 0003).

One listen socket per resolved address: the HTTP server layer is made once per address and fanned out; nothing binds 0.0.0.0 unless the legacy wildcard is requested. Loopback is bound alongside every selected interface again, so the single-socket interim paragraph is removed from ADR 0003. Runtime state and startup output list every bound address.

Verification: Node is the CI runtime, proven by the Seam 1 suite (9 cases): every bound address serves /api/auth/session over HTTP (not just an open socket), a real address survives a skipped explicit one (lan,203.0.113.7), and shutdown releases every listener (rebinding the addresses after scope close). Bun verified manually: two Bun.serve instances on one ephemeral port, both returning 200. The {loopback, tailnet} case runs only on a tailnet-connected machine and skips with a message elsewhere, as the ticket requires.

Behaviour note: the server's own origin URL now uses the first bound address, which is loopback for any interface selection. The four consumers are local CLIs (connect, project, pair, auth) that previously depended on the tailnet interface being up, so this is an improvement.

Review items declined, with reasons: ResolvedListenAddress.kind is pre-existing dead weight unrelated to this ticket; persisted addresses is spec-mandated even before a UI reads it; --port 0 collision retry is unwarranted for a failure not observed in 500 trials; skipIf for the tailnet case is what the ticket asks for.

Known gap: the desktop still expresses a single address; #7 changes that.

A listen selection could resolve to several addresses, but the server
opened a single socket, so `--host tailnet` bound the tailnet address and
dropped loopback. That contradicted ADR 0003, whose rule is that loopback
is always bound alongside every selected interface.

`ResolvedListenAddress` now carries `bindHosts`, every address to bind,
loopback first. `HttpServerLive` calls the platform server constructor
once per address and fans `serve` out over all of them, so each address
gets its own listening socket and nothing binds a wildcard unless a
wildcard was asked for. The first bind fixes the port, which keeps
`--port 0` landing every address on one ephemeral port. Local clients and
URLs stay on loopback; `connectionHost` points remote clients at the
first non-loopback address.

The Seam 1 harness probes the runner's own LAN and Tailscale addresses
rather than faking unbindable ones, and skips when the runner has none.
Its TCP probe now closes politely and waits for the socket to close: the
old `destroy()` left the server reaping a half-open connection, which
raced scope teardown and failed the multi-bind cases about 15% of runs.
With more than one listener, naming a single origin hid where the server
actually answers. Persisted runtime state gains an `addresses` field and
the headless startup output gains a `Listening on:` line. Both list every
bound address; `origin` and the connection string still name the one
address a local CLI or a remote client should dial.
The interim recorded that a non-loopback selection bound its first
non-loopback address and skipped loopback. Multi-bind retires it, so the
ADR reads as its original rule again.
Carrying the first listener's port to the rest cast its address to
`TcpAddress`, which lies when the bind is a Unix socket. Narrow on the
tag instead and fall back to the configured port, and give the second
diagnostic suppression the same reason as the first.
The Seam 1 multi-bind cases only probed raw TCP, so a fan-out that
installed the request handler on the primary listener alone would still
have reported both addresses accepting. The harness can now read the
`/api/auth/session` descriptor over a specific bound address, and the
multi-bind cases read it over each one, which is what separates a real
per-address bind from a single listener.

Adds the case the acceptance criteria asked for, an explicit address on
no interface alongside a real one, so the surviving address is bound
rather than only loopback. Adds a shutdown case proving every listener is
released, by taking the addresses for this process once the scope closes:
a connect to a closed port on a LAN address is dropped rather than
refused, so probing it would only hang. Runtime state now has a case
covering a selection that bound more than one address.
@Khang5687
Khang5687 merged commit ba28012 into fork-main Sep 16, 2026
5 of 13 checks passed
@github-actions github-actions Bot added the vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. label Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant