Skip to content

upgrade: an inherited listener fd is closed unused when the listener is already active #1342

Closed

Description

@FlorentinDUBOIS

Summary

On a worker upgrade, a listener's inherited file descriptor is closed unused instead of being
adopted. All four protocols. The address keeps being served because of SO_REUSEPORT, so there is no
outage — what is lost is the point of the hand-off: whatever was queued on the inherited socket.

Mechanism

XxxListener::activate short-circuits before it can consume the socket it was handed:

  • lib/src/http.rs:1295, lib/src/https.rs:1433, lib/src/tcp.rs:2377, lib/src/udp.rs:287 all open
    with if self.active { return Ok(self.token); }.

The caller has already built an owning wrapper around the inherited fd
(.map(|fd| unsafe { MioUdpSocket::from_raw_fd(fd) }) and the TCP equivalents), and
ScmListeners::get_* (command/src/scm_socket.rs:473-528) removes the entry as it hands it over.
So on the early return the wrapper is dropped and the fd is closed, with the entry already consumed.

Why it is reachable on an ordinary upgrade

Not an exotic path — it is the normal sequence:

  1. fork_main_into_worker writes the initial state from produce_initial_state
    (command/src/state.rs:2737), which is generate_requests() — including one ActivateListener
    per active listener
    .
  2. Server::try_new_from_config applies that initial state (lib/src/server.rs:958) before
    receive_listeners() (lib/src/server.rs:1005). scm_listeners is still None, so the new
    worker binds its own socket, which succeeds because server_bind / udp_bind set
    SO_REUSEPORT (lib/src/socket.rs:1136, :1213). The listener is now active.
  3. server.scm_listeners = Some(listeners) (lib/src/server.rs:1020) — the old worker's fds arrive
    after the listener is already up.
  4. UpgradeWorkerTask scatters generate_activate_requests() to the new worker
    (bin/src/command/upgrade.rs:177-185). Repeat activate → short-circuit → the inherited fd is
    closed unused
    , once per listener, on every protocol.

Impact

Medium. It is a close, not a leak, and SO_REUSEPORT means the address stays served by the worker's
own socket. What is lost is the queued state the hand-off exists to preserve:

  • TCP / HTTP / HTTPS: the inherited accept backlog is discarded — connections that completed their
    handshake against the old socket get an RST instead of being served.
  • UDP: the inherited receive buffer is dropped. Mitigating: lib/src/protocol/udp/LIFECYCLE.md:353-356
    already documents that UDP flow state is deliberately not migrated, so the expectation there is
    lower.

Note on from_scm

The from_scm field is dead — zero from_scm: true anywhere in the tree, and nothing reads it. It is
not the gate. The real condition is scm_listeners still holding an unconsumed fd for a listener that
is already active.

Suggested direction

Give the four activate implementations an early-return path that hands the socket back to the caller
rather than dropping it, so the caller can either adopt it or close it deliberately. Whether an
already-active listener should replace its socket with the inherited one, or explicitly discard it
after draining, is the design question — replacing mid-flight has its own hazards, so this wants a
decision rather than a reflex.

Provenance

Found while fixing a separate defect in the UDP ActivateListener arm; not introduced by that work.
Source-derived — the upgrade path was traced through the code, not reproduced on a live upgrade.
Confirming it with an actual sozu upgrade and an ss/lsof observation of the inherited fd would be
worthwhile before choosing a fix.

Activity

  1. added 5 commits that reference this issue on Sep 20, 2026
    765f6cd
    a5e5114
    1c2bea7
    faa7d67
    86cafab
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