Repository navigation
fix(bin): stop leaking worker SCM sockets and upgrade files into later processes - #1670
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 18:54
FlorentinDUBOIS
force-pushed
the
fix/scm-fd-leaks
branch
from
September 28, 2026 19:26
6dbca43 to
0293d45
Compare
…r processes Descriptors the main holds without FD_CLOEXEC are inherited by every process it execs. `fork_main_into_worker` cleared the flag on the main end of each new worker's SCM socket and nothing set it back, so every later worker inherited the main end of every earlier worker's SCM socket, stopped ones included, and the main never saw EOF from those peers. A main upgrade also cleared the flag on the channel of every worker, while the new main adopts only those that are neither `Stopped` nor `Stopping`, so the others stayed open and inheritable there; `enable_cloexec_after_upgrade` restored the flag on `Running` channels only. That leaked channel was also the only thing keeping a draining worker's command channel open across a main upgrade. The new main kept its unlinked upgrade file and its confirmation channel open, and each worker kept its state file. On a real main with two workers, `upgrade --worker 0`, `upgrade --worker 1` and `upgrade` left 8 sockets shared by the main and both workers, and deleted files open in every worker. Keep `main_to_worker_scm` close-on-exec, and clear the flag on it only in `SerializedWorkerSession::try_from`, beside the channel, for the workers the next main adopts. The new main now adopts `Stopping` workers too, in their `Stopping` state, and `generate_upgrade_data` skips only `Stopped` ones: a worker still draining after `upgrade --worker` returns from its event loop as soon as its command channel closes, cutting its in-flight requests, so it must keep a main. It is never fanned out to, does not count as alive, and turns `Stopped` when its channel closes. `sozu upgrade` skips such workers instead of asking the new main for a second upgrade it refuses. `enable_cloexec_after_upgrade` restores the flag on every adopted worker's channel and SCM socket, and the old main calls it on both failure branches of `upgrade_main`. The new main closes the upgrade file and the confirmation channel once used, and a worker closes its state file once read. A stopped worker's channel and SCM socket still stay open in the main, close-on-exec, until the next main upgrade; closing them earlier means dropping the session from `status` or restructuring `WorkerSession`. The process-level `bin/tests/upgrade_fd_inheritance_e2e.rs` runs `sozu start` with two workers, both worker upgrades and a main upgrade, and fails when a socket inode is held by two sozu processes or twice by one, or a deleted file stays open. `bin/tests/upgrade_keeps_draining_worker_e2e.rs` holds a request on a backend that answers after 8 seconds, starts `upgrade --worker 0`, runs a main upgrade while worker 0 still drains, and requires the request to complete. CI runs both with the other process-level tests. `bin/src/command/LIFECYCLE.md` now names the `peer_comm` guard instead of a `server.rs` line number this change moves. Closes #1668 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/scm-fd-leaks
branch
from
September 28, 2026 20:34
0293d45 to
e6c3d57
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 #1668. Follow-up to #1665 / #1666.
Mechanism
Every process the main execs inherits each descriptor the main holds without
FD_CLOEXEC.fork_main_into_workercleared the flag on the main end of each new worker's SCM socket, and nothing set it back. Every later worker therefore inherited the main end of every earlier worker's SCM socket, stopped ones included, and the main never saw EOF from those peers.StoppednorStopping. The other channels stayed open, and inheritable, in the new main.enable_cloexec_after_upgraderestored the flag onRunningchannels only, never on SCM sockets.Change
main_to_worker_scmstays close-on-exec.SerializedWorkerSession::try_fromclears the flag on the SCM socket, beside the channel, for the upgrade hand-off only.Stoppingworkers too (in theirStoppingstate), andgenerate_upgrade_dataserializes every worker it adopts, that is every worker notStopped. A worker still draining afterupgrade --workerreturns fromServer::runas soon as its command channel closes, cutting its in-flight requests. It must therefore keep a main across a main upgrade. Such a worker is never fanned out to, does not count as alive, and turnsStoppedwhen its channel closes.sozu upgrade(the CLI's main-upgrade loop) skipsStoppingandStoppedworkers instead of asking the new main for a second upgrade, which it refuses.enable_cloexec_after_upgraderestores the flag on every adopted worker's channel and SCM socket. The old main calls it on both failure branches ofupgrade_main.bin/src/command/LIFECYCLE.mdnow names thepeer_commguard instead of aserver.rsline that this change moves.I checked every intentional inheritance path before the change, and they still hold:
new_to_old, and the channel and SCM socket of each adopted worker.Out of scope, stated: a stopped worker's channel and SCM socket stay open in the main, close-on-exec, until the next main upgrade. That is two descriptors per stopped worker, and they are not inherited. Closing them earlier means removing the
STOPPEDrow fromstatusor restructuringWorkerSession.Evidence (real
sozu start, 2 workers, thenupgrade --worker 0,upgrade --worker 1,upgrade)Every socket and deleted file per process, from
/proc/<pid>/fdandfdinfo:(deleted)is in the main and both workersDraining worker across a main upgrade (review of 6dbca43)
Scenario: HTTP, 1 worker, and a backend that answers
/slowafter 8 s. The test sendsGET /slow, startsupgrade --worker 0, then runsupgradeof the main while worker 0 still drains.WRK-00 command channel was closedand an empty response.closing session of worker 0when it exits.bin/tests/upgrade_keeps_draining_worker_e2e.rs(#[ignore], run by CI) holds this exact scenario.I kept this approach over the worker-side alternative (not returning from
Server::runon a closed channel during a soft stop). A worker without a main can no longer be reconfigured, reports nothing, and is only reaped by init. Changing the worker would also mean it never notices its main dying while it drains, which is exactly whatenable_cloexec_after_upgradeprotects.Test
bin/tests/upgrade_fd_inheritance_e2e.rsis#[ignore]d, on thesigterm_soft_stop_e2e.rsmodel, and added to the CI process-level step. It runs the sequence above and fails when a socket inode is held by two sozu processes, or twice by one, or when a deleted file stays open (fds 0–2 excluded).upgrade_fd_inheritance_e2e: onmain(8615a01), with this change'sbin/srcreverted, it fails with 25 violations. With the change, it passes 5 runs out of 5.upgrade_keeps_draining_worker_e2e: red on 6dbca43 (2 out of 2), green with this revision (5 out of 5), and green on 8615a01.Validation (after rebase on 8615a01, all exit 0)
The pre-push hook passed.
Signed-off-by: Florentin Dubois [email protected]