Repository navigation
Description
Summary
This is the same class of bug as #1665: descriptors the main process holds without FD_CLOEXEC are inherited by every process it execs. With #1665 fixed, these paths still leak.
- The SCM socket of every worker.
fork_main_into_worker(bin/src/worker.rs) clearsFD_CLOEXEConmain_to_worker_scmin the parent after the fork, andServer::enable_cloexec_after_upgradenever sets it back. Every worker forked later therefore inherits the main end of the SCM socket of every earlier worker, stopped ones included. The main end then has live duplicates, so it never sees EOF from those peers. - Channels and SCM sockets of stopped workers across a main upgrade.
Server::generate_upgrade_dataserializes every worker, which clearsFD_CLOEXECon its channel, whileCommandHub::from_upgrade_dataadopts only the workers that are neitherStoppednorStopping. The skipped descriptors stay open in the new main, never adopted and still inheritable, and every worker it forks inherits them. enable_cloexec_after_upgradeonly covers part of the adopted workers. It re-enablesFD_CLOEXEConly forRunningworkers, and only on their channel, never on their SCM socket.- The new main's upgrade file and confirmation channel.
begin_new_main_process(bin/src/upgrade.rs) keeps the unlinked upgrade-state file and thenew_to_oldconfirmation channel open for its whole life, withoutFD_CLOEXEC, so its workers inherit both. - Each worker's own state file.
begin_worker_processkeeps its unlinked state file open after reading it. - A failed main upgrade. On both failure branches of
upgrade_main, the old main keeps running with the flag still cleared on the command socket and on every worker's channel.
Reproduction
I ran a real sozu start (2 workers, TCP and HTTP listeners), then upgrade --worker 0, upgrade --worker 1 and upgrade, on fix/scm-cloexec (#1666, which receives listeners close-on-exec). Every socket and (deleted) file per process comes from /proc/<pid>/fd and /proc/<pid>/fdinfo. Excerpt:
===== start
SUMMARY: sockets held more than once: 1
socket 362246461 unix: pid 4185002 (main) fd 10, pid 4185005 (worker 1) fd 10
===== after upgrade --worker 0 and --worker 1
SUMMARY: sockets held more than once: 3
socket 362246461 unix: main fd 10, worker 2 fd 10, worker 3 fd 10
socket 362246465 unix: main fd 12, worker 2 fd 12, worker 3 fd 12
socket 361725215 unix: main fd 17, worker 3 fd 17
===== after main upgrade
main: fd=11 /tmp/#2620727 (deleted) cloexec=NO <- upgrade file
worker 4: fd=11 /tmp/#2620727 (deleted), fd=22 /tmp/#2620728 (deleted)
SUMMARY: sockets held more than once: 8 (each shared by the new main and both new workers)
The main's socket count grows by two for every worker it launches, and each new worker holds one more foreign socket than the previous one.
Proposed fix
- Keep
main_to_worker_scmclose-on-exec. Clear the flag on it only inSerializedWorkerSession::try_from, the one hand-off that needs it, beside the channel. - Serialize only the workers the new main adopts.
- Restore the flag on the channel and SCM socket of every adopted worker, and do the same in the old main when the upgrade fails.
- Close the upgrade file and the confirmation channel in the new main once used, and the state file in the worker once read.
Out of scope: 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. Closing them earlier means either dropping the session, which removes its STOPPED row from status, or restructuring WorkerSession.
Activity
fix(bin): stop leaking worker SCM sockets and upgrade files into late…
fix(bin): stop leaking worker SCM sockets and upgrade files into late…
fix(bin): stop leaking worker SCM sockets and upgrade files into late…
fix(command): earliest-deadline poll, cancelled-task routes, stopping…