Repository navigation
fix(threads): timers release their stop-token callbacks when they end - #298
Conversation
…ning actor On top of the threads timer fix (#298): the timer thread holds a plain Live guard. Baselines are taken once the gauges are quiet, so one test's asynchronous tail cannot shift the next one's baseline.
6f1cf07 to
1caadb8
Compare
🤖 Gemini Code ReviewThe changes look solid and well-tested. Summary of Review
No issues found; this is ready to merge. Automated review by Gemini · gemini-4-argon · custom prompt · 7.0k in / 3.0k out · ~$0.04 |
🤖 Codex Code ReviewNo actionable bugs found. The registration removal is synchronized correctly, callback captures are dropped outside the mutex, and timers retain their registrations until their threads exit. The added tests cover slot reuse, cancellation races, and timer callback cleanup. I couldn’t run the tests: the pinned toolchain is unavailable, and the installed toolchain lacks the cached Automated review by OpenAI Codex · gpt-6.1-sol · custom prompt · 133.2k in / 762 out · ~$0.07 |
🤖 Claude Code ReviewReview: PR #298, unregisterable cancel callbacks for threads-mode timersSummaryThe PR fixes a real leak. Previously every threads-mode
The Loom models and the stress test are a good addition. I found no correctness bugs. The notes below are mostly about memory behaviour, test strength and API surface. Correctness and concurrency (verified)
Suggestions
Tests and docs
Overall: LGTM, with item 1 (shrink the slab) and item 2 (make the Loom model actually race) recommended before merge. Automated review by Claude · claude-opus-5-5 · custom prompt · 9.8k in / 5.6k out · ~$0.15 |
🤖 Kimi Code Review
Otherwise, the change looks sound. The slot/sequence check prevents stale registrations from removing reused entries, callbacks still execute outside the mutex, and the timer closures retain their registrations until every exit path. Slot reuse, ordering, cancellation races, and callback release are well covered by the unit and Loom tests. Automated review by Kimi (Moonshot AI) · kimi-k3.1-pro · custom prompt · 5.9k in / 5.8k out · ~$0.07 |
🤖 Grok Code ReviewThe leak fix is correct. Sequence-numbered slab removal, the existing “flag then lock” cancel protocol, and dropping callbacks outside the mutex give exactly-once firing without retaining finished timers on the actor token. Concurrency. Timers. Nits (non-blocking):
Automated review by Grok (xAI) · grok-4.7 · custom prompt · 7.6k in / 25.5k out · ~$0.17 |
🤖 DeepSeek Code ReviewThe fix looks correct. The slab + sequence-number design is a good fit: unregistering is O(1), stale removal after slot reuse is safe, and A few minor observations:
No correctness or concurrency issues found in the diff. Automated review by DeepSeek · deepseek-v4-pro · custom prompt · 6.4k in / 67.1k out · ~$0.27 |
In threads mode every send_after/send_interval registered an on_cancel callback (holding the timer's wake-up Sender) on the actor's stop token, which only drains on cancel, so a long-lived threads actor that kept arming timers grew one callback per timer until it stopped. spawned_rt::threads::CancellationToken now stores callbacks in a slab and gains register_on_cancel, returning a CancelRegistration whose drop removes the callback in O(1) (sequence-checked, so a reused slot is never removed by a stale handle), plus callbacks_registered. cancel() still drains every callback once, in registration order. The timer thread holds both registrations (its own token's and the actor's) and drops them when it fires or is cancelled. Tasks-mode timers are unaffected: Tokio's cancellation waiter is removed when its future drops. Tests: a failing-first regression (110 callbacks kept on main for 50 fired + 60 cancelled timers on a running actor), Loom models of unregister racing cancel, and a multi-thread unregister/cancel stress test.
1caadb8 to
6561002
Compare
…ning actor On top of the threads timer fix (#298): the timer thread holds a plain Live guard. Baselines are taken once the gauges are quiet, so one test's asynchronous tail cannot shift the next one's baseline.
…ning actor On top of the threads timer fix (#298): the timer thread holds a plain Live guard. Baselines are taken once the gauges are quiet, so one test's asynchronous tail cannot shift the next one's baseline.
Bug
In
threadsmode everysend_after/send_intervalregistered anon_cancelcallback (a boxed closure holding the timer's mpscSender, which keeps the channel alive) on the actor's stop token. That list only drains when the token is cancelled, so the callbacks stayed until the actor stopped. A long-lived threads actor that keeps setting timers grew by one per timer, without bound. Found with thetimers_alivegauge from #297: 200 firedsend_afters left 200 registrations on a running actor.Fix
spawned_rt::threads::CancellationTokenstores callbacks in a slab (free list plus a per-entry sequence number). Newregister_on_cancel(cb) -> CancelRegistration: dropping the registration removes the callback in O(1), unlesscancel()already took it. The sequence check means a stale handle never removes a later entry that reuses the slot.callbacks_registered()for diagnostics and tests.on_cancelis unchanged (it keeps its callback until cancellation).cancel()still drains every callback exactly once, in registration order, outside the lock.threads::send_after/send_interval: the timer thread holds both registrations (its own token's and the actor's) and drops them when it fires, is cancelled or the actor stops.cancelled()futures, whose waiters are removed when the futures are dropped at the end of the timer.Unregister semantics (documented): once the drop returns the callback will not start; if
cancel()took it first, it runs (or is running) on the cancelling thread.Tests
threads::timer_tests::threads_timers_release_their_stop_token_callbacks_while_the_actor_runs. A running actor arms 50 shortsend_afters that fire, plus 50 longsend_afters and 10send_intervals that are cancelled. The actor token'scallbacks_registered()must return to baseline while the actor is still running. Withconcurrency/src/threads/time.rsreverted to main it fails withthe running actor's stop token kept 110 timer callbacks.rt/tests/cancellation_loom.rs, the real token under--cfg spawned_loom):rt/src/threads/mod.rs):cancel(), 20 rounds: at most once, every kept callback exactly once, every callback dropped, 0 left registered. Repeated 10× on fede.fede (toolchain 1.93):
cargo test -p spawned-rt -p spawned-concurrency -p spawned-testand-p spawned-concurrency --no-default-features --features threads,metricspass; the stress test passed 10 repeated runs;cancellation_loom(release) passes 4/4.Perf gate (callgrind, Ir/op, head vs main): no change on the hot paths.
async_ct_burstasync_ct_request_seqasync_ct_request_conc16async_ct_send_p1async_ct_send_p2threads_request_seqModel-sync: no modelled file is touched (
check-model-sync.py pathspasses). All 24 planted patches stillgit apply --check. fmt and clippy-D warningspass for the workspace with and withoutspawned-concurrency/metrics, and for--no-default-features.Merge before #297, which is rebased on this.