Repository navigation
fix(udp): re-arm the wheel when an expiry finds nothing due - #1354
Merged
Merged
Conversation
`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
requested review from
Wonshtrum and
llenotre
as code owners
September 19, 2026 07:20
This was referenced Sep 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Idle UDP flows stop being evicted after a wheel entry fires early. Two production lines.
The defect
Timer::duration_to_tickrounds to the nearest tick, not up —(elapsed + T/2) / T— so an entrycan 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 (
110msdelivered at tick 100,230msat 200).The chain:
Timer::polldelivers the listener's entry early and removes it from its slab — the entry isconsumed.
UdpListenerSession::timeoutcallsUdpManager::handle_timeout(now).flow.idle_deadline <= nowis false for every flow), so noclose_flowruns, and it falls through to
reschedule().reschedule()memoizes onif next != self.armed_deadline— the minimum has not changed — so itemits no
ArmTimer.timer_handlewas 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_timeoutclearsarmed_deadlinebefore collecting what is due, so an unchanged deadlinere-emits; the shell drops its now-dangling
timer_handlein the same place. Consume, then reschedule.armed_deadlineis cleared at the top rather than just before the tailreschedule(), so thereschedule()calls insideclose_flowalso see the truth. Either placement yields oneArmTimerinthe nothing-due case; the top is the honest statement.
The
timer_handlehalf is hygiene, not a second fixA delivered handle is already inert.
set_timeout_atclamps every new entry withif tick <= self.tick { tick = self.tick + 1 }, andself.tickis monotonically non-decreasing, so adelivered handle's tick is forever
<= self.tickwhile every later entry is>= self.tick + 1.cancel_timeout's tick guard therefore rejects it 100% of the time — it cannot cancel the strangerthat 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 isreused, the successor's tick is strictly greater, and the cancel is refused.
Behaviour change
ArmTimerperclose_flowwhoserecomputed minimum differs — two where HEAD emitted one, measured. These do not become extra
wakeups:
drain_outputsis synchronous andarm_timercancels the previous entry before arming thenext, so they collapse into wheel insert/cancel churn within the one drain.
Why nothing caught it
ArmTimerappears nowhere insim/,e2e/orfuzz/— the emission contract had zero assertionsanywhere in the tree. The simulator drives
handle_timeoutstraight off a virtual clock with no wheel,so step 1 cannot happen there.
e2e/src/tests/udp_tests.rshas no idle-eviction test at all.The nearest miss is
idle_race_resolved_by_generation_token, which callshandle_timeoutat a momentnothing 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 usingTimeoutContainer, which re-arms unconditionally and so has no memoized re-arm to swallow. The tenTimeoutContainer::triggered()call sites were re-derived: six close the session, two inmuxre-armexplicitly when they survive (with a comment recording that upstream hit this same class), and two in
kawa_h1hand off to the sibling container.Tests — four seen red, plus two pinning the arguments
early_expiry_that_finds_nothing_due_still_rearmsrepeated_early_expiries_each_rearman_early_wheel_fire_still_evicts_the_idle_flow— drives a realTIMER, a realUdpListenerSessionand a realUdpManager; pre-fix the wheel is left holding theTICK_MAXsentinel 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 untriggerabletest_a_delivered_timeout_handle_cannot_cancel_its_slot_successortest_maximum_earliness_is_a_full_half_tick— the interval is closed on the early side, so theattained 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 concurrentcopies 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.