Skip to content

fix(udp): re-arm the wheel when an expiry finds nothing due - #1354

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/udp-timer-lost-wakeup
Sep 19, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/udp-timer-lost-wakeup

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Idle UDP flows stop being evicted after a wheel entry fires early. Two production lines.

The defect

Timer::duration_to_tick rounds to the nearest tick, not up — (elapsed + T/2) / T — so an entry
can be delivered up to half a tick early, 50 ms at the default 100 ms. Its own doc comment said
"rounding up to the closest one", which is where the misunderstanding began. Two existing tests
already encoded early fires without saying so (110ms delivered at tick 100, 230ms at 200).

The chain:

  1. Timer::poll delivers the listener's entry early and removes it from its slab — the entry is
    consumed.
  2. UdpListenerSession::timeout calls UdpManager::handle_timeout(now).
  3. Nothing is actually due (flow.idle_deadline <= now is false for every flow), so no close_flow
    runs, and it falls through to reschedule().
  4. reschedule() memoizes on if next != self.armed_deadline — the minimum has not changed — so it
    emits no ArmTimer.
  5. timer_handle was never cleared on fire.

The wheel entry is gone and nothing re-arms it. Idle flows are never evicted until some other flow's
deadline happens to move the minimum.

The fix

handle_timeout clears armed_deadline before collecting what is due, so an unchanged deadline
re-emits; the shell drops its now-dangling timer_handle in the same place. Consume, then reschedule.

armed_deadline is cleared at the top rather than just before the tail reschedule(), so the
reschedule() calls inside close_flow also see the truth. Either placement yields one ArmTimer in
the nothing-due case; the top is the honest statement.

The timer_handle half is hygiene, not a second fix

A delivered handle is already inert. set_timeout_at clamps every new entry with
if tick <= self.tick { tick = self.tick + 1 }, and self.tick is monotonically non-decreasing, so a
delivered handle's tick is forever <= self.tick while every later entry is >= self.tick + 1.
cancel_timeout's tick guard therefore rejects it 100% of the time — it cannot cancel the stranger
that reused its slab slot.

Clearing it keeps the field's meaning local instead of resting on that two-hop argument from another
module. The precondition the argument needs is now pinned by
test_a_delivered_timeout_handle_cannot_cancel_its_slot_successor, which asserts the slot is
reused, the successor's tick is strictly greater, and the cancel is refused.

Behaviour change

  • An early-fired expiry costs one extra wheel wakeup.
  • An expiry closing several flows at once emits an intermediate ArmTimer per close_flow whose
    recomputed minimum differs — two where HEAD emitted one, measured. These do not become extra
    wakeups: drain_outputs is synchronous and arm_timer cancels the previous entry before arming the
    next, so they collapse into wheel insert/cancel churn within the one drain.

Why nothing caught it

ArmTimer appears nowhere in sim/, e2e/ or fuzz/ — the emission contract had zero assertions
anywhere in the tree. The simulator drives handle_timeout straight off a virtual clock with no wheel,
so step 1 cannot happen there. e2e/src/tests/udp_tests.rs has no idle-eviction test at all.

The nearest miss is idle_race_resolved_by_generation_token, which calls handle_timeout at a moment
nothing is due — the exact trigger — but only asserts the flow count.

The UDP listener was the one site that hand-rolled its own Option<Timeout> instead of using
TimeoutContainer, which re-arms unconditionally and so has no memoized re-arm to swallow. The ten
TimeoutContainer::triggered() call sites were re-derived: six close the session, two in mux re-arm
explicitly when they survive (with a comment recording that upstream hit this same class), and two in
kawa_h1 hand off to the sibling container.

Tests — four seen red, plus two pinning the arguments

  • early_expiry_that_finds_nothing_due_still_rearms
  • repeated_early_expiries_each_rearm
  • an_early_wheel_fire_still_evicts_the_idle_flow — drives a real TIMER, a real
    UdpListenerSession and a real UdpManager; pre-fix the wheel is left holding the TICK_MAX
    sentinel with the flow alive forever
  • test_timeout_fires_up_to_half_a_tick_early — characterizes the rounding the other three depend on,
    so a future div_ceil "fix" cannot silently make them untriggerable
  • test_a_delivered_timeout_handle_cannot_cancel_its_slot_successor
  • test_maximum_earliness_is_a_full_half_tick — the interval is closed on the early side, so the
    attained maximum is a full 50 ms and it is lateness that caps at 49 ms

The wall-clock test was measured across 234 runs in four load conditions — isolated, under 40
spinner processes, in the full parallel suite sharing the thread-local TIMER, and 144 concurrent
copies on an oversubscribed box — with zero failures. It reads the wheel's grid phase from a probe
rather than assuming it, and asserts the earliness against the wheel.

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      858 passed, 0 failed

`Timer::duration_to_tick` rounds to the nearest tick, not up, so an entry can
be delivered up to half a tick early — 50 ms at the default 100 ms. Its own
doc comment said "rounding up to the closest one", which is where the
misunderstanding started; two existing tests already encoded early fires
without saying so.

`UdpListenerSession::timeout` then called `UdpManager::handle_timeout(now)`,
which filtered on `flow.idle_deadline <= now`, found nothing due, and called
`reschedule()`. That memoizes on `if next != self.armed_deadline`, and the
minimum had not changed, so it emitted no `ArmTimer`. Meanwhile `Timer::poll`
had already removed the entry from its slab. The listener's wheel entry was
consumed and nothing re-armed it, so idle UDP flows stopped being evicted
until some other flow's deadline happened to move the minimum.

`handle_timeout` now clears `armed_deadline` before collecting what is due,
so an unchanged deadline re-emits, and the shell drops its now-dangling
`timer_handle` in the same place. Two lines.

The `timer_handle` half is hygiene rather than a second fix: a delivered
handle is already inert, because `set_timeout_at` clamps every new entry past
`self.tick` while a delivered one sat at or below it, so no successor in that
slab slot can ever match its tick. Clearing it keeps the field's meaning local
instead of resting on that two-hop argument from another module, and the
precondition the argument needs is now pinned by
`test_a_delivered_timeout_handle_cannot_cancel_its_slot_successor`.

Behaviour change: an early-fired expiry now costs one extra wheel wakeup, and
an expiry that closes several flows at once emits an intermediate `ArmTimer`
per `close_flow` whose recomputed minimum differs from the previous one — two
where HEAD emitted one, measured. These do not become extra wakeups, since
`drain_outputs` is synchronous and `arm_timer` cancels the previous entry
before arming the next, so they collapse into wheel churn within the one
drain.

This survived unseen because nothing observed the contract: `ArmTimer` appears
nowhere in `sim/`, `e2e/` or `fuzz/`, the simulator drives `handle_timeout`
off a virtual clock with no wheel at all, and `e2e` has no idle-eviction test.
The nearest existing test calls `handle_timeout` at a moment nothing is due —
the exact trigger — but only asserts the flow count.

The UDP listener was the one site that hand-rolled its own `Option<Timeout>`
rather than using `TimeoutContainer`, which re-arms unconditionally and so has
no memoized re-arm to swallow.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 9ef8514 into main Sep 19, 2026
23 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/udp-timer-lost-wakeup branch September 19, 2026 08:28
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