Skip to content

fix(bin): stop leaking worker SCM sockets and upgrade files into later processes - #1670

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

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/scm-fd-leaks

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1668. Follow-up to #1665 / #1666.

Mechanism

Every process the main execs inherits each descriptor the main holds without FD_CLOEXEC.

  • SCM sockets of earlier workers. fork_main_into_worker cleared 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.
  • Channels of stopped workers. A main upgrade cleared the flag on every worker's channel, but the new main adopts only the workers that are neither Stopped nor Stopping. The other channels stayed open, and inheritable, in the new main.
  • Partial restore. enable_cloexec_after_upgrade restored the flag on Running channels only, never on SCM sockets.
  • Temporary files and the confirmation channel. The new main kept its unlinked upgrade file and its confirmation channel open, and each worker kept its state file.
  • Failed main upgrade. The old main kept every cleared flag after a failed upgrade.

Change

  • main_to_worker_scm stays close-on-exec. SerializedWorkerSession::try_from clears the flag on the SCM socket, beside the channel, for the upgrade hand-off only.
  • The new main adopts Stopping workers too (in their Stopping state), and generate_upgrade_data serializes every worker it adopts, that is every worker not Stopped. A worker still draining after upgrade --worker returns from Server::run as 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 turns Stopped when its channel closes.
  • sozu upgrade (the CLI's main-upgrade loop) skips Stopping and Stopped workers instead of asking the new main for a second upgrade, which it refuses.
  • enable_cloexec_after_upgrade restores the flag on every adopted worker's channel and SCM socket. 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.
  • bin/src/command/LIFECYCLE.md now names the peer_comm guard instead of a server.rs line that this change moves.

I checked every intentional inheritance path before the change, and they still hold:

  • worker launch: the state file, channel and SCM socket, on the child side;
  • main upgrade: the command socket, the upgrade file, 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 STOPPED row from status or restructuring WorkerSession.

Evidence (real sozu start, 2 workers, then upgrade --worker 0, upgrade --worker 1, upgrade)

Every socket and deleted file per process, from /proc/<pid>/fd and fdinfo:

Step Before (8615a01) After
start 1 socket shared (worker 1 holds worker 0's SCM main end); each worker holds its deleted state file 0 shared, no deleted file
after both worker upgrades 3 sockets shared; the main holds 11 sockets 0 shared, no deleted file
after main upgrade 8 sockets shared by the new main and both workers; the upgrade file (deleted) is in the main and both workers 0 shared, no deleted file

Draining worker across a main upgrade (review of 6dbca43)

Scenario: HTTP, 1 worker, and a backend that answers /slow after 8 s. The test sends GET /slow, starts upgrade --worker 0, then runs upgrade of the main while worker 0 still drains.

  • On 8615a01 (main): the request completes. The leaked channel kept worker 0's command channel open.
  • On 6dbca43: the request is cut, in 2 runs out of 2, with WRK-00 command channel was closed and an empty response.
  • With this revision: the request completes in 5 runs out of 5. The new main adopts worker 0, and then logs closing session of worker 0 when 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::run on 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 what enable_cloexec_after_upgrade protects.

Test

bin/tests/upgrade_fd_inheritance_e2e.rs is #[ignore]d, on the sigterm_soft_stop_e2e.rs model, 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: on main (8615a01), with this change's bin/src reverted, 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)

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 --locked                                 # unit tests
cargo test -p sozu --locked -- --ignored                    # all bin/tests process-level e2e, including both new ones
cargo test -p sozu-command-lib --locked                     # 185 passed
cargo test -p sozu-e2e --locked -- upgrade                  # 14 passed
python3 .github/scripts/check_doc_citations.py --self-test | --root . | --root . --base origin/main | --audit --root .

The pre-push hook passed.

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

…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
FlorentinDUBOIS merged commit 07de024 into main Sep 28, 2026
2 of 23 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/scm-fd-leaks branch September 28, 2026 20:35
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.

Workers inherit other workers' SCM sockets, stopped workers' channels and the upgrade file

1 participant