Repository navigation
fix(command): receive SCM listener descriptors as close-on-exec - #1666
Merged
Merged
Conversation
`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]>
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 17:38
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 #1665. Related to #1659.
Mechanism
ScmSocket::receive_listenerscalledrecvmsgwithoutMSG_CMSG_CLOEXEC, so every listener received over the SCM socket was installed withoutFD_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 acrossexec, 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_bindsetsSO_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>/fdand/proc/<pid>/fdinfo:cloexec=yesupgrade --worker 0→ new worker 2fd=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 yesupgrade→ workers 3 and 4cloexec=NOcloexec=yesWhy the change is safe
I checked every path that passes a descriptor across
exec. Each one clearsFD_CLOEXECexplicitly withutil::disable_close_on_execon 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 andnew_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 ofreceive_listenersare 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 e2eupgradetests.On platforms without
MSG_CMSG_CLOEXEC(anything outside Linux, Android and the BSDs, for example macOS), the flag is set withfcntlright after receipt. That leaves a window for a concurrentfork, which the comment states.The e2e symptom
fuzz_tcp_clienthellospawnscargo fuzzwhile the other tests' workers run as threads in the same process. With the leak,ssshowscargo-fuzzand the fuzz target holding their listeners, andtest_tcp_listener_serves_after_reactivationfails withAddress already in use.cargo test -p sozu-e2e --features splice -- tcp_ --test-threads=8(fuzz test included, and it did run):Regression test
scm_socket::tests::received_listener_descriptors_are_close_on_execsends a TCP listener and a UDP socket over anScmSocketpair and checksFD_CLOEXECon every received descriptor.received fd 7 must be close-on-exec so no exec'd child inherits it.Validation (all exit 0)
The pre-push hook (build, clippy, unit tests, fuzz smoke) passed.
Signed-off-by: Florentin Dubois [email protected]