Skip to content

fix(test): reserve test ports against concurrent test processes - #1662

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/test-port-collisions
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/test-port-collisions

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1659

Mechanism

testing::provide_port counted up from a fixed AtomicU16::new(10000), so every sozu-lib test binary walked the same port sequence. Two binaries running at the same time (several worktrees, parallel CI jobs, a pre-push hook) received the same ports. The collision is silent: server_bind and udp_bind set SO_REUSEPORT, so both processes (same UID) bind the same address without error and the kernel splits connections and datagrams between them.

The e2e registry (e2e/src/port_registry.rs) probed with a bind before issuing a port, but it held the port only until the test adopted its listener, and every e2e process started its cursor at 20000. When a test closes its listener on purpose (DeactivateListener { to_scm: false } followed by ActivateListener), another e2e process can probe the released port as free and take it. The re-activate then fails with Address already in use, and test_tcp_listener_serves_after_reactivation reports cycled=false.

it_should_send_a_proxy_protocol_header_to_the_upstream_backend also bound the fixed ports 6666 and 2001.

Change

  • sozu_lib::testing::reserve_port(port) -> bool reserves 127.0.0.1:port only when an exclusive TCP bind (no SO_REUSEADDR, no SO_REUSEPORT) and an exclusive UDP bind on it both succeed. It then keeps the TCP socket bound and not listening, sets SO_REUSEADDR and SO_REUSEPORT on it after the bind, and holds it while the process reserves its next 255 ports.
    • The process's own server_bind and std/mio/tokio listeners still bind the port.
    • Another process's exclusive probe fails, so it moves on.
    • A connect to a reserved port with no listener is still refused.
    • The kernel's automatic TCP port selection skips it.
  • provide_port keeps its signature. It now draws from 12000–19999, below the Linux ephemeral range (32768–60999), through reserve_port.
  • The e2e registry passes every port it issues, both reserved-listener and unbound, through reserve_port.
  • The proxy-protocol send test takes its two ports from provide_port.
  • doc/testing.md and CHANGELOG.md are updated.

No call site changes and no dependency is added: socket2 is already a sozu-lib dependency, and e2e goes through sozu_lib::testing. The only non-test file touched is the pub mod testing block of lib/src/lib.rs, the test-support module compiled into the lib. No runtime behaviour changes.

Trade-offs

  • The guarantee holds only between processes that all reserve through reserve_port. Because the reservation carries SO_REUSEADDR and SO_REUSEPORT, a process that binds without reserving still succeeds on a reserved port. That covers a server_bind (SO_REUSEPORT) from a test binary built before this change, and a std::net::TcpListener::bind (SO_REUSEADDR) while nothing listens on the port, including between a deactivate and the re-activate. The reservation itself does not prevent either case. Old lib binaries count from 10000 and stay outside 12000–19999, but older e2e binaries still walk 20000 and up.
  • Only 256 reservations are held, oldest dropped first. Every held reservation costs a descriptor, and the full e2e suite reserves thousands of ports. With unbounded holding, a fixed e2e run reached 2322 open descriptors, which is over a default soft RLIMIT_NOFILE of 1024. With the bound, a run peaked around 390, and the full e2e suite passes under ulimit -Sn 1024. A test that deactivates and re-activates its listener only after its process has reserved 256 further ports has lost its reservation in between, so that window is unprotected again.
  • UDP is only probed, not held. A bound UDP socket in the SO_REUSEPORT group would receive a share of the datagrams.

Reproduction

Build the test binary once and run copies of it at the same time. Other agents ran sozu tests on the same machine throughout.

Before, lib (unfixed sozu_lib, 3 concurrent pairs): 2 of 3 pairs failed.

pair 1: a exit=101 b exit=101
  scm_listener_handoff_tests::an_inherited_descriptor_with_no_listener_yet_is_adopted_by_a_later_activation
    the connection queued on the retained HTTPListen descriptor must be served by the later activation, got [HTTPSListen, TCPListen]
  scm_listener_handoff_tests::inherited_listener_sockets_are_adopted_by_the_initial_activation
    the connection queued on the inherited HTTP socket must survive the hand-off, got [HTTPSListen, TCPListen]
pair 2: a exit=101 b exit=101
  listener_lifecycle_tests::in_flight_udp_flows_survive_a_duplicate_udp_activate (both processes)
    the flow must be established before the duplicate activate (left: None)
  tcp::tests::round_trip
    could not read from socket s2: Os { code: 11, kind: WouldBlock, ... }
pair 3: a exit=0 b exit=0

A pair run with an intermediate revision of this branch, before the send test was moved off its fixed ports, also hit proxy_protocol::send::send_test::it_should_send_a_proxy_protocol_header_to_the_upstream_backend: could not start backend: AddrInUse.

Before, e2e (listener_reactivation_tests::test_tcp, 3 processes at a time × 15, next to a full e2e suite): 6 of 45 and 2 of 45 processes failed.

ERROR TCP-REACTIVATE  error on request ID_6, Could not activate TCP listener: could not bind to socket 127.0.0.1:20006: could not set bind to socket: Address already in use (os error 98)
TCP reactivation: before=true cycled=false after=true stopped=true

After, lib (final code, 5 concurrent pairs): 0 of 10 processes failed, and every run reports 1327 passed; 0 failed.

After, e2e (final code): 0 of 120 processes failed.

  • 15 triples of test_tcp_listener_serves_after_reactivation, twice: 0 of 90.
  • 10 triples of the whole listener_reactivation_tests module: 0 of 30.
  • Earlier runs of this branch next to a full suite of the unfixed binary: 0 of 165.

Validation (all exit 0)

cargo +nightly fmt --all -- --check
cargo build --locked
cargo clippy --all-targets --locked -- -D warnings
cargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warnings
cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8   # lib 1327 passed, e2e 490 passed
ulimit -Sn 1024 && <sozu_e2e binary> --skip tests::fuzz_tests:: --test-threads=8  # 490 passed
python3 .github/scripts/check_doc_citations.py --self-test
python3 .github/scripts/check_doc_citations.py --root .
python3 .github/scripts/check_doc_citations.py --root . --base origin/main
python3 .github/scripts/check_doc_citations.py --audit --root .

Signed-off-by: Florentin Dubois [email protected]

`testing::provide_port` counted up from a fixed 10000, so two sozu-lib
test binaries running at the same time (several worktrees, parallel CI
jobs, a pre-push hook) received the same ports. `server_bind` and
`udp_bind` set SO_REUSEPORT, so both processes bound the same address
without error and the kernel split connections and datagrams between
them. Two copies of the lib test binary run together failed the
listener hand-off tests, `in_flight_udp_flows_survive_a_duplicate_udp_activate`
and `tcp::tests::round_trip`, and the proxy-protocol send test failed
on its fixed ports 6666 and 2001.

The e2e registry probed before issuing a port, but nothing held a port
once the test's own listener was closed, and every e2e process started
its cursor at 20000. Another process could therefore take a port between
a deactivate and the re-activate, and
`test_tcp_listener_serves_after_reactivation` failed with `cycled=false`
on "Address already in use".

Add `sozu_lib::testing::reserve_port`. It reserves a port only when
exclusive TCP and UDP binds on it succeed, then keeps the TCP socket
bound and not listening, with SO_REUSEADDR and SO_REUSEPORT set after
the bind, while the process reserves its next 255 ports. The process's
own listeners still bind the port, a connect to it is still refused
while nothing listens, and another process's exclusive probe fails and
moves on. The bound keeps the descriptors held under a default soft
RLIMIT_NOFILE of 1024, since the full e2e suite reserves thousands of
ports. The UDP side is only probed, because a bound UDP socket in the
SO_REUSEPORT group would receive datagrams.

The guarantee holds only between processes that all reserve through
`reserve_port`. The reservation's own SO_REUSEADDR and SO_REUSEPORT let
a process that binds without reserving still take the port: a
`server_bind` from an older test binary, or a
`std::net::TcpListener::bind` while nothing listens, including between a
deactivate and the re-activate. And only 256 reservations are held, the
oldest dropped first, so a test that re-activates only after its process
has reserved 256 further ports has an unprotected window.

`provide_port` now draws from 12000-19999, below the Linux ephemeral
range, through `reserve_port`; the e2e registry reserves every port it
issues through it; and the proxy-protocol send test takes its ports from
`provide_port`. No call-site signature changes.

Closes #1659

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit ed70b9e into main Sep 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/test-port-collisions branch September 28, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent sozu-lib test processes share listener ports through provide_port

1 participant