Skip to content

Listener descriptors received over SCM are not close-on-exec, so upgraded workers inherit untracked listeners #1665

Closed

Description

@FlorentinDUBOIS

Summary

ScmSocket::receive_listeners (command/src/scm_socket.rs) calls recvmsg without MSG_CMSG_CLOEXEC, so every listener descriptor received over the SCM socket is installed without FD_CLOEXEC.

During a worker upgrade, the main process receives the retiring worker's listeners over SCM and then calls fork_main_into_worker, which forks and execs sozu worker. At that point the main still holds the received listeners, so the new worker inherits a second copy of every listener across exec, in addition to the copy it then receives over SCM. The worker never tracks the inherited copy.

When that worker later deactivates the listener and activates it again, the untracked descriptor keeps the old socket open and listening. server_bind sets SO_REUSEPORT, so the new bind succeeds, and the kernel spreads incoming connections across the new socket and the orphaned one. Nobody accepts on the orphaned socket, so a share of the new connections hangs in its backlog.

Reproduction with a real sozu (main + 2 workers, one TCP listener, one HTTP listener)

Listening sockets held by each process, read from /proc/<pid>/fd and /proc/<pid>/fdinfo (TCP listener on port 47911, HTTP on 59689), on main at ed70b9e.

At start, and after listener tcp deactivate then activate, each worker holds exactly its own two listeners, all close-on-exec:

pid 3473222 [worker --id 0]   fd=10 tcp:59689 cloexec=yes   fd=12 tcp:47911 cloexec=yes
pid 3473223 [worker --id 1]   fd=9  tcp:59689 cloexec=yes   fd=12 tcp:47911 cloexec=yes

After upgrade --worker 0, the new worker 2 holds every listener twice, and none of the copies is close-on-exec:

pid 3474204 [worker --id 2]
    fd=8  tcp:59689 inode=1393800958 cloexec=NO
    fd=9  tcp:47911 inode=1393811972 cloexec=NO
    fd=11 tcp:59689 inode=1393800958 cloexec=NO
    fd=13 tcp:47911 inode=1393811972 cloexec=NO

Then listener tcp deactivate + activate on worker 2. The new socket is bound, but the old one (inode 1393811972) is still listening through the untracked fd 13:

pid 3474204 [worker --id 2]
    fd=8  tcp:59689 inode=1393800958 cloexec=NO
    fd=9  tcp:47911 inode=1393845825 cloexec=yes
    fd=11 tcp:59689 inode=1393800958 cloexec=NO
    fd=13 tcp:47911 inode=1393811972 cloexec=NO

I then opened 40 connections to the TCP listener, which has no frontend, so sozu closes them at once. Before the upgrade, all 40 were closed by a worker. After the upgrade and the deactivate/activate cycle, 26 were closed and 14 hung in the orphaned socket's backlog.

After a main upgrade (sozu upgrade), the re-launched workers also hold every listener twice (cloexec=NO).

The same mechanism in the e2e suite

cargo test -p sozu-e2e --features splice -- tcp_ --test-threads=8 also runs tests::fuzz_tests::fuzz_tcp_clienthello, which spawns cargo fuzz while the other tests' workers run as threads in the same process. The fuzzer inherits their listeners:

LISTEN 127.0.0.1:20000 users:(("fuzz_tcp_client",pid=3412200,fd=99),("fuzz_tcp_client",pid=3410382,fd=99),("cargo-fuzz",pid=3410277,fd=99))

So test_tcp_listener_serves_after_reactivation fails with Could not activate TCP listener: could not bind to socket 127.0.0.1:200xx: … Address already in use: the leaked copy is a plain std listener without SO_REUSEPORT, so the rebind is refused. On ed70b9e this failed 4 runs out of 5. Related to #1659, which fixed a different cause of the same symptom.

Proposed fix

Receive with MSG_CMSG_CLOEXEC. Every path that hands a descriptor across exec already clears FD_CLOEXEC explicitly on that descriptor with util::disable_close_on_exec: the state file, the command and SCM channels, the upgrade file, the confirmation channel and the command socket. No path relies on a received listener surviving exec: listeners always travel to the new worker over SCM, after the fork.

Activity

  1. added a commit that references this issue on Sep 28, 2026
    8615a01
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