Repository navigation
test(e2e): hold every fixture port until its child binds it - #2731
Merged
Merged
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Code coverage overviewLanguages: Rust Rust / code-coverage/cargo-llvm-cov
View coverage of the 4 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. |
free_ports() bound N ephemeral loopback ports, read the numbers and dropped the listeners. A parallel nextest process could then get one of those ports from bind(0) or as a connect() source port before the spawned decdn-node, anvil or gateway bound it, and the child failed at bring-up with "Address already in use". The QUIC bind_port is UDP, but free_ports() reserved TCP, so nothing reserved it at all. Remove the race by construction: - anvil binds --port 0. The fixture reads the port from its "Listening on" stdout line and drains the rest of stdout. The retry of an anvil that died at startup is gone. - ports::PortReservation holds a TCP port for the child's lifetime: a SO_REUSEADDR socket bound to 127.0.0.1 that never listens. Linux hands a held port to no bind(0) and to no connect(), and a child that binds it explicitly with SO_REUSEADDR still succeeds. decdn-node's admin and metrics listeners, tokio's TcpListener::bind (gateway, dashboard) and anvil all set it. - ports::UdpPortClaim picks the QUIC port below the kernel's ephemeral range, where the kernel never hands a port out, and locks the number across fixtures with a TCP socket on it. iroh binds UDP without SO_REUSEADDR, so a held UDP socket would block the daemon. - NodeFixture keeps its three ports for its lifetime, so restart() respawns on ports nothing else can take in between (#2727 candidate 2). - The gateway journeys, the decdn-node anvil tests and the sponsord binary anvil test hold their ports the same way. Closes #2729 Co-Authored-By: Claude Opus 5.5 <[email protected]>
thiras
force-pushed
the
fix/e2e-port-toctou
branch
from
October 28, 2026 22:18
85f9b2a to
38cdc8a
Compare
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.
What
free_ports()bound N ephemeral loopback ports, read the numbers, and dropped the listeners. A parallel nextest process could then get one of those ports, frombind(…:0)or as aconnect()source port, before the spawneddecdn-node,anvilor gateway bound it. The child then failed at bring-up withAddress already in use. Two examples,g_node_04_blacklist_compliance::regional_blacklist_scopeandcli_bundle_pull_range_dedup, are in #2729. The QUICbind_portis UDP, butfree_ports()reserved TCP, so nothing reserved the UDP number at all.This PR removes the race by construction. It adds no retries.
anvil(ChainFixture, chain unit test)--port 0. The fixture reads the port from anvil'sListening on 127.0.0.1:<port>stdout line, then drains stdout. The retry of an anvil that died at startup is gone.decdn-node(net::bind_reuseaddr)ports::PortReservationheld for the fixture's lifetime--listen/--metrics-listen, dashboard--listendecdn gateway/decdn dashboard(tokioTcpListener::bind)PortReservationheld until the test endsbind_port(UDP, registered on-chain)ports::UdpPortClaimbelow the ephemeral rangedecdn-nodeanvil tests (anvil, admin, metrics),sponsordbinary_anvilsponsordPortReservation. A socket bound to127.0.0.1:0withSO_REUSEADDRthat never listens. On Linux, the ephemeral search (inet_csk_find_open_port,relax = false) treats it as a conflict, andconnect()skips every port that abind()holds. An explicitbindwithSO_REUSEADDRfollowed bylistenstill succeeds, because two reuse sockets conflict on an explicit bind only when the other one listens. The daemon, std, tokio and anvil all setSO_REUSEADDR. On other platforms the reservation releases its socket at once, which matches the old behaviour. The anvil e2e runs on Linux only.UdpPortClaim. iroh binds UDP withoutSO_REUSEADDR, so a held UDP socket would block the daemon. The claim picks a port in[ephemeral_low / 2, ephemeral_low), read from/proc/sys/net/ipv4/ip_local_port_range. The kernel never hands out a port in that range. A TCP socket on the same number, bound withoutSO_REUSEADDR, is a lock that keeps parallel fixtures off it. A probe bind checks that UDP is free when the port is claimed.restart().NodeFixturekeeps its three ports for its whole lifetime, sorestart()respawns on the same ports and nothing can take them during the SIGKILL gap. The stored addresses do not change, sog_node_07_rotate_keyand the other restart callers need no edits. This closes candidate 2 of #2727.Closes #2729
Reviewer notes
crates/e2e/src/ports.rsfirst. Everything else is wiring.decdn-nodealready binds withSO_REUSEADDR.admin_port = 0disables the admin server, so a port-0 path would have needed a product change, and this PR does not need one.sponsord*tests that bind and drop a port to get an unreachable URL. No child binds that port, so the TOCTOU does not apply in the same way.Listening onprefix. If a Foundry release renames that line,ChainFixture::launchfails with a clear error within 20 s.Verification
Old race, measured in Python on Linux 6.17: 64 ports picked and released the
free_portsway, then 20 000bind(127.0.0.1:0)calls. 95 of the calls landed on a released port. With the same 64 ports held as reservations, nobind(0)call (40 000, with and withoutSO_REUSEADDR) and noconnect()(20 000) landed on one. anvil binds a held port.New unit tests in
crates/e2e/src/ports/tests.rs:TIME_WAITMutation check: with the reservation releasing its socket,
held_reservation_is_never_an_ephemeral_bindandreservation_refuses_a_plain_bindfail. Aconnect()variant was dropped because Linux givesbind(0)odd ports andconnect()even ones, so that test could not fail.cargo build -p decdn-cli -p decdn-node, thencargo nextest run -p decdn-e2e --features anvil-e2e --retries 0 --no-fail-fastthree times at load average 28 to 34 on 16 cores:No
Address already in usefailures. The one failure in runs 1 and 2 ispeer_store_probeless_second_fetch_and_cross_process_persistence("fetch docs: Generalize architecture to content-agnostic CDN, refocus slashing on bad edges #2 probed the holders N time(s)"). That is the open flake e2e: peer_store_probeless fetch #2 intermittently counts holder probes #2677, which fails 3 of 6 runs on unmodified main.cargo nextest run -p decdn-node --features anvil-e2e --test anvil_settlement_e2e --test anvil_bringup_shutdown_e2e: 5/5.cargo nextest run -p sponsord --features sponsord/anvil-e2e --test binary_anvil: 1/1.Exit code 0 for each of:
cargo clippy --workspace --all-targets --all-features -- -D warningscargo fmt -- --checkCARGO_BUILD_WARNINGS=deny cargo doc --workspace --no-deps --document-private-itemspre-commit runon the changed filesCHANGELOG.md: a Fixed entry.🤖 Generated with Claude Code