Skip to content

fix(server): keep a listener's slab slot for its whole lifetime - #1333

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/udp-listener-reactivation
Sep 18, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/udp-listener-reactivation

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Four defects, one root cause: a listener's slab slot did not live as long as the listener.

The defect

The four proxies keep a listener's slab token inside the listener: give_back_listener hands
the same token back on deactivate-listener, and a later activate-listener re-registers the
socket under it. deactivate-listener nevertheless freed that slab slot, and Server::ready wraps
its whole body in if self.sessions.borrow().slab.contains(session_token) — a miss is a silent
no-op with no log.

So a reactivated HTTP, HTTPS or TCP listener is deaf: it serves only the backlog its activation
drained, then goes silent, while activate-listener answered ok.

UDP failed harder. Its activation installs a real UdpListenerSession at the listen token
behind a slab.contains guard that was now false, so the socket was registered with no session
behind it and every datagram was dropped. And once the freed key had been handed out to an ordinary
session, that same guard read true and the assignment overwrote a live session.

The fix

A listener now owns exactly one slab slot for its whole add-listener → remove-listener
lifetime. Deactivating puts back the same inert ListenSession placeholder that adding installed
instead of freeing the slot, so the retained token stays valid — and the slab can no longer hand
that key to a session, which removes the overwrite by construction rather than by another check.

remove-listener becomes the release point.

The leak this also closes

No proxy's remove_listener touches the session slab, and the deactivation arms were the only code
that ever freed a listener's slot. Removing a listener that had not been deactivated first therefore
already stranded one slab key per add/remove cycle, while base_sessions_count was decremented
anyway. For UDP the stranded entry was worse than a key: it held the last UdpListenerSession
reference, keeping the listener's UdpSocket open and keeping Server::ready dispatching to a
listener the control plane had removed.

Reserving the slot without making remove-listener release it would have converted that
pre-existing leak into a guaranteed one, which is why the two parts ship together.

Also

ActivateListener for UDP no longer answers WorkerResponse::ok when it cannot install its
session — it returns a worker error. Previously it reached the identical state (active, socket
registered, placeholder slot) and reported success.

Tests — all four seen red

  • a_reactivated_tcp_listener_is_reachable_from_the_event_loop — red with
    reserve_listen_token → slab.remove: "a reactivated listener must still be dispatched by
    ready()"
    .
  • removing_a_listener_releases_its_reserved_slot — red without the RemoveListener release:
    "cycle 0: removing a listener must release its reserved slot".
  • deactivating_a_listener_drops_its_pending_accept_token — red without the accept_ready purge.
  • Plus the UDP arm.

Each carries a To SEE THIS RED: block naming the exact mutation, per the house convention.

Local, on this branch rebased onto 3fcf4ec6:

cargo +nightly fmt --all -- --check                                              exit 0
cargo +1.93.1 clippy --workspace --all-targets --all-features --locked -D warn   exit 0
cargo +1.93.1 test -p sozu-lib --locked --all-features    816 + 1 + 5 passed, 0 failed

The four proxies keep a listener's slab token inside the listener itself:
`give_back_listener` hands the same token back on `deactivate-listener`,
and a later `activate-listener` re-registers the socket under it.
`deactivate-listener` nevertheless freed that slab slot, and
`Server::ready` drops any event whose token the slab does not contain — so
a reactivated HTTP, HTTPS or TCP listener served only the backlog its
activation drained and then went silent, while the request had answered
`ok`.

UDP failed harder. Its activation installs a real `UdpListenerSession` at
the listen token behind a `slab.contains` guard that was now false, so the
socket was registered with no session behind it and every datagram was
dropped. Once the freed key had been handed to an ordinary session, that
same guard read true and the assignment overwrote a live session.

A listener now owns exactly one slab slot for its whole `add-listener` ->
`remove-listener` lifetime: deactivating puts back the same inert
`ListenSession` placeholder that adding installed rather than freeing the
slot. The retained token therefore stays valid, and the slab can no longer
hand that key to a session — the overwrite is removed by construction
rather than by another check.

`remove-listener` becomes the release point, which also closes a leak that
predates this change: no proxy's `remove_listener` touches the session
slab, and the deactivation arms were the only code that ever freed a
listener's slot, so removing a listener that had not been deactivated first
stranded one slab key per add/remove cycle while `base_sessions_count` was
decremented anyway. For UDP the stranded entry was worse than a key: it
held the last `UdpListenerSession` reference, keeping the listener's
`UdpSocket` open and keeping `Server::ready` dispatching to a listener the
control plane had removed.

The UDP activation path no longer answers `ok` when it cannot install its
session; it returns a worker error instead. All four defects seen red.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 0c1d0b6 into main Sep 18, 2026
23 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/udp-listener-reactivation branch September 18, 2026 19:08
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.

1 participant