Skip to content

test(e2e): hold every fixture port until its child binds it - #2731

Merged
thiras merged 1 commit into
mainfrom
fix/e2e-port-toctou
Oct 28, 2026
Merged

thiras merged 1 commit into
mainfrom
fix/e2e-port-toctou

Conversation

@thiras

@thiras thiras commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

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, from bind(…:0) or as a connect() source port, before the spawned decdn-node, anvil or gateway bound it. The child then failed at bring-up with Address already in use. Two examples, g_node_04_blacklist_compliance::regional_blacklist_scope and cli_bundle_pull_range_dedup, are in #2729. The QUIC bind_port is UDP, but free_ports() reserved TCP, so nothing reserved the UDP number at all.

This PR removes the race by construction. It adds no retries.

Port Consumer Approach
anvil RPC anvil (ChainFixture, chain unit test) --port 0. The fixture reads the port from anvil's Listening on 127.0.0.1:<port> stdout line, then drains stdout. The retry of an anvil that died at startup is gone.
daemon admin + metrics decdn-node (net::bind_reuseaddr) ports::PortReservation held for the fixture's lifetime
gateway --listen / --metrics-listen, dashboard --listen decdn gateway / decdn dashboard (tokio TcpListener::bind) PortReservation held until the test ends
daemon QUIC bind_port (UDP, registered on-chain) iroh / netwatch ports::UdpPortClaim below the ephemeral range
decdn-node anvil tests (anvil, admin, metrics), sponsord binary_anvil in-process runtime / sponsord the same reservation, held for the test

PortReservation. A socket bound to 127.0.0.1:0 with SO_REUSEADDR that never listens. On Linux, the ephemeral search (inet_csk_find_open_port, relax = false) treats it as a conflict, and connect() skips every port that a bind() holds. An explicit bind with SO_REUSEADDR followed by listen still succeeds, because two reuse sockets conflict on an explicit bind only when the other one listens. The daemon, std, tokio and anvil all set SO_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 without SO_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 without SO_REUSEADDR, is a lock that keeps parallel fixtures off it. A probe bind checks that UDP is free when the port is claimed.

restart(). NodeFixture keeps its three ports for its whole lifetime, so restart() respawns on the same ports and nothing can take them during the SIGKILL gap. The stored addresses do not change, so g_node_07_rotate_key and the other restart callers need no edits. This closes candidate 2 of #2727.

Closes #2729

Reviewer notes

  • Read crates/e2e/src/ports.rs first. Everything else is wiring.
  • Product code does not change. decdn-node already binds with SO_REUSEADDR. admin_port = 0 disables the admin server, so a port-0 path would have needed a product change, and this PR does not need one.
  • Not changed: the 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.
  • The anvil stdout reader matches on the Listening on prefix. If a Foundry release renames that line, ChainFixture::launch fails with a clear error within 20 s.

Verification

  • Old race, measured in Python on Linux 6.17: 64 ports picked and released the free_ports way, then 20 000 bind(127.0.0.1:0) calls. 95 of the calls landed on a released port. With the same 64 ports held as reservations, no bind(0) call (40 000, with and without SO_REUSEADDR) and no connect() (20 000) landed on one. anvil binds a held port.

  • New unit tests in crates/e2e/src/ports/tests.rs:

    • a held port is never an ephemeral bind (5 000 picks)
    • a node-style bind and a std bind succeed on a held port, before and after a restart that leaves a TIME_WAIT
    • a plain bind is refused
    • the UDP claim is below the range, is exclusive, and leaves the UDP bind free

    Mutation check: with the reservation releasing its socket, held_reservation_is_never_an_ephemeral_bind and reservation_refuses_a_plain_bind fail. A connect() variant was dropped because Linux gives bind(0) odd ports and connect() even ones, so that test could not fail.

  • cargo build -p decdn-cli -p decdn-node, then cargo nextest run -p decdn-e2e --features anvil-e2e --retries 0 --no-fail-fast three times at load average 28 to 34 on 16 cores:

    • run 1: 121/122
    • run 2: 121/122
    • run 3: 122/122

    No Address already in use failures. The one failure in runs 1 and 2 is peer_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 warnings
    • cargo fmt -- --check
    • CARGO_BUILD_WARNINGS=deny cargo doc --workspace --no-deps --document-private-items
    • pre-commit run on the changed files
  • CHANGELOG.md: a Fixed entry.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings October 28, 2026 21:04
@thiras thiras added kind:test Test coverage, flakes, fixtures or the e2e harness area:ci Workflows, pre-commit hooks, repo-shape guards, release pipeline labels Oct 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main f2bc01c fix/e2e-port-toctou 85f9b2a +/-
77% 77% 0%
View coverage of the 4 most impacted files.
File main f2bc01c fix/e2e-port-toctou 85f9b2a +/-
crates/client/src/lib.rs 89% 89% 0%
crates/e2e/src/chain.rs 8% 8% 0%
crates/e2e/src/node.rs 21% 21% 0%
crates/e2e/src/ports.rs 0% 96% +96%

ℹ️ 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
thiras force-pushed the fix/e2e-port-toctou branch from 85f9b2a to 38cdc8a Compare October 28, 2026 22:18
@thiras
thiras merged commit b8ef2e9 into main Oct 28, 2026
11 checks passed
@thiras
thiras deleted the fix/e2e-port-toctou branch October 28, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci Workflows, pre-commit hooks, repo-shape guards, release pipeline kind:test Test coverage, flakes, fixtures or the e2e harness

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(e2e): free_ports TOCTOU lets a parallel process take a fixture port before the child binds it

2 participants