Skip to content

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

Closed

Description

@thiras

Summary

The anvil e2e fixtures pick loopback ports with free_ports() / free_port() (crates/e2e/src/lib.rs). These bind N ephemeral 127.0.0.1 ports, read the numbers, and drop the listeners. The spawned process (decdn-node, anvil, decdn gateway, decdn dashboard) binds the number later. The function's own docs admit the TOCTOU.

Nextest runs the anvil e2e journeys as parallel processes. The kernel's ephemeral range also serves outbound source ports and every bind(…:0) on the box. So another process can get a released port before the child binds it, and the child fails at bring-up.

Observed locally under load:

g_node_04_blacklist_compliance::regional_blacklist_scope:
  failed to bind admin listener: admin bind 127.0.0.1:42515 failed: Address already in use (os error 98)

cli_bundle_pull_range_dedup::cli_bundle_pull_pays_exactly_a_scattered_complement:
  metrics port already in use

#2727 lists the same race (candidate 2) for NodeFixture::restart(), which respawns the daemon on the same ports after a SIGKILL. Nothing holds those ports between the kill and the respawn.

Two more gaps in the same code:

  • The QUIC bind_port is a UDP port, but free_ports() reserves a TCP port. Nothing reserves the UDP number at all.
  • The gateway e2e journeys (gateway_*.rs) and the decdn-node in-process anvil tests (anvil_bringup_shutdown_e2e.rs, anvil_settlement_e2e.rs) each carry their own copy of the same release-then-bind free_port().

Measured

On Linux 6.17, with 64 ports picked and released the free_ports way, 20 000 later bind(127.0.0.1:0) calls landed on one of those 64 ports 95 times.

Fix direction

Remove the race by construction rather than by retry:

  • anvil accepts --port 0 and prints Listening on 127.0.0.1:<port>. Let it bind port 0 and read the port back.
  • Hold a TCP port reservation for the life of the child. A socket bound with SO_REUSEADDR but never put in the listening state is never handed out by the kernel for bind(…:0) or as a connect() source port. A child that binds the same address explicitly with SO_REUSEADDR still succeeds. decdn-node's admin and metrics listeners (net::bind_reuseaddr), tokio's TcpListener::bind (the gateway and the dashboard) and anvil all set it.
  • iroh binds its UDP socket without SO_REUSEADDR, so a held UDP socket would block the daemon. Pick the QUIC port below the kernel's ephemeral range instead, where the kernel never hands a port out, and claim the number across parallel fixtures with a TCP lock socket on the same number.

Activity

  1. added
    prio:medium
    kind:test
    area:ci
    on Oct 28, 2026
  2. moved this from Backlog to In progress in deCDNon Oct 28, 2026
  3. added a commit that references this issue on Oct 28, 2026
    38cdc8a
  4. added a commit that references this issue on Oct 28, 2026
    b8ef2e9
  5. moved this from In progress to Done in deCDNon Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:cikind:testprio:medium

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions