Skip to content

fix(command): receive SCM listener descriptors as close-on-exec - #1666

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/scm-cloexec
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/scm-cloexec

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closes #1665. Related to #1659.

Mechanism

ScmSocket::receive_listeners called recvmsg without MSG_CMSG_CLOEXEC, so every listener received over the SCM socket was installed without FD_CLOEXEC. During a worker upgrade, the main process receives the retiring worker's listeners and then forks and execs the new worker (fork_main_into_worker) while it still holds them. The new worker therefore inherits a second copy of every listener across exec, beside the copy it then receives over SCM, and it never tracks the inherited one.

When the worker later deactivates and re-activates the listener, the untracked copy keeps the old socket listening. Because server_bind sets SO_REUSEPORT, the kernel spreads new connections over the new socket and the orphaned one.

Production evidence (real sozu start, 2 workers, TCP :47911 + HTTP :59689)

Listening sockets per process, from /proc/<pid>/fd and /proc/<pid>/fdinfo:

Step Before (ed70b9e) After (this branch)
start, then deactivate/activate each worker holds its 2 listeners, cloexec=yes same
upgrade --worker 0 → new worker 2 fd=8 :59689 NO, fd=9 :47911 NO, fd=11 :59689 NO, fd=13 :47911 NO (each listener twice) fd=8 :59689 yes, fd=9 :47911 yes
worker 2 deactivate + activate :47911 new socket on fd 9, old inode still listening on fd 13 only the new socket
40 connections to :47911 (no frontend, so sozu closes them) 26 closed, 14 hung in the orphaned backlog 40 closed, 0 hung
main upgrade → workers 3 and 4 each listener held twice, cloexec=NO each listener once, cloexec=yes

Why the change is safe

I checked every path that passes a descriptor across exec. Each one clears FD_CLOEXEC explicitly with util::disable_close_on_exec on the descriptor it hands over:

  • fork_main_into_worker: the state file, worker_to_main, worker_to_main_scm, main_to_worker_scm.
  • fork_main_into_new_main: the upgrade file and new_to_old.
  • Server::disable_cloexec_before_upgrade: the command socket.

Received listeners never cross exec: the new worker gets them over SCM (send_listeners) after the fork, and the worker process itself never execs. The two callers of receive_listeners are the main during a worker upgrade (bin/src/command/upgrade.rs) and a worker at start-up (Server::new), and neither needs inheritance. The worker-upgrade and main-upgrade runs above work with the fix, and so do the 14 e2e upgrade tests.

On platforms without MSG_CMSG_CLOEXEC (anything outside Linux, Android and the BSDs, for example macOS), the flag is set with fcntl right after receipt. That leaves a window for a concurrent fork, which the comment states.

The e2e symptom

fuzz_tcp_clienthello spawns cargo fuzz while the other tests' workers run as threads in the same process. With the leak, ss shows cargo-fuzz and the fuzz target holding their listeners, and test_tcp_listener_serves_after_reactivation fails with Address already in use.

cargo test -p sozu-e2e --features splice -- tcp_ --test-threads=8 (fuzz test included, and it did run):

  • before: 4 failed runs out of 5 (ports 20034, 20027, 20150, 20016);
  • after: 0 failed runs out of 5, 32 passed each time.

Regression test

scm_socket::tests::received_listener_descriptors_are_close_on_exec sends a TCP listener and a UDP socket over an ScmSocket pair and checks FD_CLOEXEC on every received descriptor.

  • Without the fix it fails: received fd 7 must be close-on-exec so no exec'd child inherits it.
  • With the fix it passes.

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
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked
cargo test -p sozu-command-lib --locked                       # 185 passed
cargo test -p sozu-e2e --locked -- upgrade                    # 14 passed
cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8   # lib 1334, e2e 490 passed
python3 .github/scripts/check_doc_citations.py --self-test | --root . | --root . --base origin/main | --audit --root .

The pre-push hook (build, clippy, unit tests, fuzz smoke) passed.

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

`ScmSocket::receive_listeners` called `recvmsg` without
`MSG_CMSG_CLOEXEC`, so every listener received over the SCM socket was
installed without `FD_CLOEXEC`. During a worker upgrade the main process
receives the retiring worker's listeners and then forks and execs the
new worker while it still holds them, so the new worker inherited a
second, untracked copy of each listener beside the one it then received
over SCM. On a real main with two workers, `/proc/<pid>/fd` of the
upgraded worker showed every listener twice, none close-on-exec. After
that worker deactivated and re-activated its TCP listener, the untracked
copy kept the old socket listening; with SO_REUSEPORT the kernel spread
new connections over both sockets, and 14 of 40 connections hung in the
orphaned backlog.

The e2e suite hit the same leak: `fuzz_tcp_clienthello` spawns
`cargo fuzz`, which inherited the in-process workers' listeners, and
`test_tcp_listener_serves_after_reactivation` failed on "Address already
in use" in 4 of 5 runs of
`cargo test -p sozu-e2e --features splice -- tcp_`.

Receive with `MSG_CMSG_CLOEXEC`, and where the flag does not exist set
`FD_CLOEXEC` right after receipt. No path relies on a received listener
surviving `exec`: listeners reach a new worker over SCM after the fork,
and the descriptors that do cross `exec` (the state and upgrade files,
the command and SCM channels, the confirmation channel, the command
socket) already clear the flag explicitly with
`util::disable_close_on_exec`.

Closes #1665

Signed-off-by: Florentin Dubois <[email protected]>
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.

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

1 participant