Skip to content

Concurrent sozu-lib test processes share listener ports through provide_port #1659

Closed

Description

@FlorentinDUBOIS

Summary

sozu_lib::testing::provide_port hands out ports from a fixed per-process counter (AtomicU16::new(10000)). Every sozu-lib test binary therefore walks the same sequence 10000, 10001, … . When two test processes run at the same time on one machine — several worktrees, parallel CI jobs on a shared runner, a pre-push hook next to an interactive cargo test — they receive the same ports.

The collision is silent rather than a bind error: server_bind and udp_bind (lib/src/socket.rs) set SO_REUSEPORT, so both processes (same UID) bind the same address successfully and the kernel load-balances incoming connections and datagrams between them. A test then sees its connection or datagram served by the other process, or never sees it at all.

Reproduction

On main at b3f2c78, build the lib test binary once and run it twice concurrently:

cargo test -p sozu-lib --lib --locked --no-run   # note the executable path
cd lib
./target/debug/deps/sozu_lib-<hash> & ./target/debug/deps/sozu_lib-<hash> & wait

Two of three pairs failed here:

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

Other runs also failed server::listener_lifecycle_tests::a_duplicate_udp_activate_strands_no_upstream_slab_slot and hung http::tests::keep_alive for more than 60 s. Each binary passes when run alone.

The e2e suite

e2e/src/port_registry.rs already probes every candidate with an exclusive bind and keeps the reserved listener until the test adopts it, so e2e listener ports do not collide across processes. provide_unbound_port releases its probe immediately, and every e2e process starts its cursor at 20000, so that path keeps a small check-then-use window; no failure from it was observed.

Proposed fix

Keep the provide_port() -> u16 signature and its call sites, and make each issued port unavailable to other processes: probe the candidate with exclusive TCP and UDP binds, then keep the TCP socket bound (not listening) for the life of the test process, with SO_REUSEADDR and SO_REUSEPORT set after the bind so the process's own server_bind still succeeds while another process's exclusive probe fails. Draw ports below the Linux ephemeral range so the kernel never auto-binds a client socket onto an issued port.

Activity

  1. FlorentinDUBOIS commented on Sep 28, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    Two more instances of the same mechanism, found while fixing this:

    1. e2e, a port released between a deactivate and a re-activate. test_tcp_listener_serves_after_reactivation deactivates its TCP listener (to_scm: false, so the socket is closed) and activates it again, which binds the same address anew. e2e/src/port_registry.rs holds a reserved listener only until the test adopts it, and every e2e process starts its cursor at 20000, so another e2e process can probe the released port as free and take it in between. Running the test three processes at a time, 15 times, next to a full e2e suite, failed 6 of 45 and 2 of 45 processes:

      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
      
    2. Hardcoded ports. protocol::proxy_protocol::send::send_test::it_should_send_a_proxy_protocol_header_to_the_upstream_backend binds the fixed 127.0.0.1:6666 and 127.0.0.1:2001, and fails with could not start backend: ... AddrInUse when two lib test processes run it at the same moment.

    The fix will reserve every port issued by either allocator for the rest of the process, so that it also covers a port whose listener the test closes on purpose, and it will move the proxy-protocol test to provide_port.

  2. added a commit that references this issue on Sep 28, 2026
    6192876
  3. added a commit that references this issue on Sep 28, 2026
    ed70b9e
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions