Repository navigation
fix(test): reserve test ports against concurrent test processes - #1662
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 15:05
`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
force-pushed
the
fix/test-port-collisions
branch
from
September 28, 2026 15:50
9b0c9b3 to
6192876
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.
Closes #1659
Mechanism
testing::provide_portcounted up from a fixedAtomicU16::new(10000), so everysozu-libtest 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_bindandudp_bindsetSO_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 byActivateListener), another e2e process can probe the released port as free and take it. The re-activate then fails withAddress already in use, andtest_tcp_listener_serves_after_reactivationreportscycled=false.it_should_send_a_proxy_protocol_header_to_the_upstream_backendalso bound the fixed ports 6666 and 2001.Change
sozu_lib::testing::reserve_port(port) -> boolreserves127.0.0.1:portonly when an exclusive TCP bind (noSO_REUSEADDR, noSO_REUSEPORT) and an exclusive UDP bind on it both succeed. It then keeps the TCP socket bound and not listening, setsSO_REUSEADDRandSO_REUSEPORTon it after the bind, and holds it while the process reserves its next 255 ports.server_bindandstd/mio/tokiolisteners still bind the port.provide_portkeeps its signature. It now draws from 12000–19999, below the Linux ephemeral range (32768–60999), throughreserve_port.reserve_port.provide_port.doc/testing.mdandCHANGELOG.mdare updated.No call site changes and no dependency is added:
socket2is already asozu-libdependency, and e2e goes throughsozu_lib::testing. The only non-test file touched is thepub mod testingblock oflib/src/lib.rs, the test-support module compiled into the lib. No runtime behaviour changes.Trade-offs
reserve_port. Because the reservation carriesSO_REUSEADDRandSO_REUSEPORT, a process that binds without reserving still succeeds on a reserved port. That covers aserver_bind(SO_REUSEPORT) from a test binary built before this change, and astd::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.RLIMIT_NOFILEof 1024. With the bound, a run peaked around 390, and the full e2e suite passes underulimit -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.SO_REUSEPORTgroup 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.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.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.
test_tcp_listener_serves_after_reactivation, twice: 0 of 90.listener_reactivation_testsmodule: 0 of 30.Validation (all exit 0)
Signed-off-by: Florentin Dubois [email protected]