fix(serve): bind before showing addresses and opening the browser - #1092
Open
VXNCXNX wants to merge 2 commits into
Open
fix(serve): bind before showing addresses and opening the browser#1092VXNCXNX wants to merge 2 commits into
VXNCXNX wants to merge 2 commits into
Conversation
axum_server::bind*() binds lazily inside .serve(), so a bind failure only surfaced once the spawned task was polled - after run() had already printed the listening URLs and opened the browser. Bind a TcpListener per address in spawn_server before show_listening and hand the listeners to run_server, which now uses from_tcp/from_tcp_rustls. A bind error propagates out of spawn_server, so the existing ? in run() already prevents the browser from opening. axum-server 0.8 has no from_tcp_openssl, so the native-tls path attaches OpenSSLAcceptor to from_tcp, as from_tcp_rustls does internally. Fixes trunk-rs#946
Binding eagerly made the first bind failure fatal, so a single occupied address took down the whole server. Since the address list is auto-populated with every loopback interface when none is configured, a default 'trunk serve' failed on hosts where one loopback family was already in use. Warn per failed address and carry on, erroring only when no address could be bound at all. show_listening now receives the addresses actually bound, via local_addr(), so the printed URLs match reality and --port 0 shows the resolved port instead of 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
trunk serve --openprints the listening URLs and opens the browser even when the server cannot bind. With the port already taken it opens a tab, then logsAddress already in use.axum_server::bind*()binds lazily inside.serve(), so the failure only surfaces once the spawned task is polled, long afterrun()has calledopen::that.This binds a
std::net::TcpListenerper address inspawn_serverbeforeshow_listening, and passes the listeners torun_server, which now usesfrom_tcp/from_tcp_rustlsinstead ofbind/bind_rustls. A bind error propagates out ofspawn_server, so the existing?inrun()already prevents the browser from opening; no new condition was needed there.axum-server 0.8 has no
from_tcp_openssl, so the native-tls path builds the equivalent fromfrom_tcpplusOpenSSLAcceptor, the same wayfrom_tcp_rustlsdoes internally. All three paths bind eagerly; none is left on the lazy path.Binding up front would make a single unavailable address fatal, which would be a regression: the address list is auto-populated with every loopback interface when none is configured, so a default
trunk servewould fail on a host where one loopback family is occupied. So a failed bind warns and the server carries on with the rest, erroring only if nothing binds at all.show_listeningnow receives the addresses actually bound, taken fromlocal_addr(), which also makes--port 0print the resolved port instead of0.Fixes #946
Verification
cargo build,cargo clippy --all-targets --all-features -- -D warnings,cargo fmt --checkall pass--no-default-features,rustls-aws-lc, andnative-tls,vendoredfailed to bind to any of the requested addresses, prints no URLs, opens no browser[::1]occupied and127.0.0.1free: warns about[::1], prints only the127.0.0.1URL, keeps serving (curlreturns 200)--port 0: prints the resolved ephemeral port#988 took the same approach and stalled on the belief that native-tls had no listener-based constructor. That is correct for
from_tcp_openssl, but the acceptor can be attached tofrom_tcpdirectly, which closes that gap.