Skip to content

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

Closed

Description

@FlorentinDUBOIS

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.

  1. The SCM socket of every worker. fork_main_into_worker (bin/src/worker.rs) clears FD_CLOEXEC on main_to_worker_scm in the parent after the fork, and Server::enable_cloexec_after_upgrade never 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.
  2. Channels and SCM sockets of stopped workers across a main upgrade. Server::generate_upgrade_data serializes every worker, which clears FD_CLOEXEC on its channel, while CommandHub::from_upgrade_data adopts only the workers that are neither Stopped nor Stopping. The skipped descriptors stay open in the new main, never adopted and still inheritable, and every worker it forks inherits them.
  3. enable_cloexec_after_upgrade only covers part of the adopted workers. It re-enables FD_CLOEXEC only for Running workers, and only on their channel, never on their SCM socket.
  4. The new main's upgrade file and confirmation channel. begin_new_main_process (bin/src/upgrade.rs) keeps the unlinked upgrade-state file and the new_to_old confirmation channel open for its whole life, without FD_CLOEXEC, so its workers inherit both.
  5. Each worker's own state file. begin_worker_process keeps its unlinked state file open after reading it.
  6. 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_scm close-on-exec. Clear the flag on it only in SerializedWorkerSession::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

  1. added 2 commits that reference this issue on Sep 28, 2026
    0293d45
    e6c3d57
  2. added a commit that references this issue on Sep 28, 2026
    07de024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions