diff --git a/CHANGELOG.md b/CHANGELOG.md index 2facc4fbe..827348d33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,15 @@ instructions per `send` (+3.6% over 0.8.0 on `async_ct_send_*`, +0.4% to +1.2% on the async request benchmarks). With the hint every gated async benchmark is below 0.8.0 (send -3.4%, requests -0.6% to -0.9%). +- Fixed: in `threads` mode every `send_after` / `send_interval` left a + callback (and its wake-up channel) on the actor's stop token until the actor + stopped, so a long-lived threads actor that kept arming timers grew without + bound. `spawned_rt::threads::CancellationToken` gains + `register_on_cancel`, whose `CancelRegistration` removes the callback in + O(1) when dropped (callbacks still fire in registration order), and + `callbacks_registered`; the timer thread now holds its registrations and + drops them when it fires or is cancelled. Tasks-mode timers were not + affected (Tokio removes a cancellation waiter when its future is dropped). - Fix the flaky `supervision::transient_startup_timeout_retries_after_actual_cleanup` (a 20 ms startup deadline raced the fast starts on a loaded host, which also made cargo-mutants report surviving mutants as caught): the replacement's diff --git a/concurrency/src/threads/time.rs b/concurrency/src/threads/time.rs index 9b8bd8c14..244b30b43 100644 --- a/concurrency/src/threads/time.rs +++ b/concurrency/src/threads/time.rs @@ -1,7 +1,7 @@ use std::sync::mpsc::{self, RecvTimeoutError}; use std::time::Duration; -use spawned_rt::threads::{self as rt, CancellationToken, JoinHandle}; +use spawned_rt::threads::{self as rt, CancelRegistration, CancellationToken, JoinHandle}; use super::actor::{Actor, Context, Handler}; use crate::message::Message; @@ -16,6 +16,27 @@ pub struct TimerHandle { pub cancellation_token: CancellationToken, } +/// A timer thread's wake-up: either token's cancellation sends on the +/// channel. The thread holds both registrations and drops them when it ends, +/// so the actor's long-lived token does not keep a callback per timer that +/// has already fired or been cancelled. +fn wake_on_cancel( + timer: &CancellationToken, + actor: &CancellationToken, +) -> (mpsc::Receiver<()>, [CancelRegistration; 2]) { + let (wake_tx, wake_rx) = mpsc::channel::<()>(); + let timer_tx = wake_tx.clone(); + let registrations = [ + timer.register_on_cancel(Box::new(move || { + let _ = timer_tx.send(()); + })), + actor.register_on_cancel(Box::new(move || { + let _ = wake_tx.send(()); + })), + ]; + (wake_rx, registrations) +} + /// Send a single message to an actor after a delay. pub fn send_after(period: Duration, ctx: Context, msg: M) -> TimerHandle where @@ -26,24 +47,18 @@ where let timer_token = cancellation_token.clone(); let actor_token = ctx.cancellation_token(); - let (wake_tx, wake_rx) = mpsc::channel::<()>(); - - let wake_tx1 = wake_tx.clone(); - timer_token.on_cancel(Box::new(move || { - let _ = wake_tx1.send(()); - })); + let (wake_rx, registrations) = wake_on_cancel(&timer_token, &actor_token); - actor_token.on_cancel(Box::new(move || { - let _ = wake_tx.send(()); - })); - - let join_handle = rt::spawn(move || match wake_rx.recv_timeout(period) { - Err(RecvTimeoutError::Timeout) => { - if !timer_token.is_cancelled() && !actor_token.is_cancelled() { - let _ = ctx.send(msg); + let join_handle = rt::spawn(move || { + let _registrations = registrations; + match wake_rx.recv_timeout(period) { + Err(RecvTimeoutError::Timeout) => { + if !timer_token.is_cancelled() && !actor_token.is_cancelled() { + let _ = ctx.send(msg); + } } + Ok(()) | Err(RecvTimeoutError::Disconnected) => {} } - Ok(()) | Err(RecvTimeoutError::Disconnected) => {} }); TimerHandle { @@ -67,18 +82,10 @@ where let timer_token = cancellation_token.clone(); let actor_token = ctx.cancellation_token(); - let (wake_tx, wake_rx) = mpsc::channel::<()>(); - - let wake_tx1 = wake_tx.clone(); - timer_token.on_cancel(Box::new(move || { - let _ = wake_tx1.send(()); - })); - - actor_token.on_cancel(Box::new(move || { - let _ = wake_tx.send(()); - })); + let (wake_rx, registrations) = wake_on_cancel(&timer_token, &actor_token); let join_handle = rt::spawn(move || { + let _registrations = registrations; while let Err(RecvTimeoutError::Timeout) = wake_rx.recv_timeout(period) { if timer_token.is_cancelled() || actor_token.is_cancelled() { break; diff --git a/concurrency/src/threads/timer_tests.rs b/concurrency/src/threads/timer_tests.rs index 62d4f45e1..765651599 100644 --- a/concurrency/src/threads/timer_tests.rs +++ b/concurrency/src/threads/timer_tests.rs @@ -247,7 +247,7 @@ struct Clocked { } impl Actor for Clocked {} -#[derive(Debug)] +#[derive(Debug, Clone)] struct Fired; impl Message for Fired { type Result = (); @@ -377,3 +377,54 @@ pub fn threads_send_interval_is_fixed_delay_while_the_actor_stalls() { }, ); } + +/// A long-lived actor's stop token keeps no callback for a timer that fired +/// or was cancelled: each `send_after`/`send_interval` used to leave one on +/// it until the actor stopped, so an actor re-arming timers grew without +/// bound. +#[test] +pub fn threads_timers_release_their_stop_token_callbacks_while_the_actor_runs() { + crate::test_wait::bounded( + concat!( + module_path!(), + "::threads_timers_release_their_stop_token_callbacks_while_the_actor_runs" + ), + move || { + let handled = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); + let actor = Clocked { + handled: handled.clone(), + stall: None, + } + .start(); + let token = actor.context().cancellation_token(); + let baseline = token.callbacks_registered(); + let fired = 50; + for _ in 0..fired { + let _timer = send_after(Duration::from_millis(1), actor.context(), Fired); + } + let long = Duration::from_secs(3600); + let mut cancelled: Vec<_> = (0..50) + .map(|_| send_after(long, actor.context(), Fired)) + .collect(); + cancelled.extend((0..10).map(|_| send_interval(long, actor.context(), Fired))); + assert!(token.callbacks_registered() >= baseline + cancelled.len()); + for timer in &cancelled { + timer.cancellation_token.cancel(); + } + assert!( + wait_for(|| handled.lock().unwrap().len(), |n| *n >= fired) >= fired, + "timers never fired" + ); + let left = wait_for(|| token.callbacks_registered(), |n| *n == baseline); + assert_eq!( + left, + baseline, + "the running actor's stop token kept {} timer callbacks", + left - baseline + ); + assert!(!token.is_cancelled(), "the actor is still running"); + actor.child_handle().stop(); + actor.join(); + }, + ); +} diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 6f53c07b2..589b3f8a2 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -928,7 +928,7 @@ Wraps standard library primitives: | `spawn(f)` | Spawn an OS thread | | `sleep(duration)` | Block the current thread | | `JoinHandle` | Handle to a spawned thread | -| `CancellationToken` | Cooperative cancellation with callback support via `on_cancel()` | +| `CancellationToken` | Cooperative cancellation with callback support: `on_cancel()` keeps a callback until cancellation; `register_on_cancel()` returns a `CancelRegistration` whose drop removes it (for waiters that finish first, such as timers) | | `mpsc` | Multi-producer, single-consumer channel (wraps `std::sync::mpsc`) | | `oneshot` | Single-use channel (wraps `std::sync::mpsc`) | | `ctrl_c()` | Returns a closure that blocks until Ctrl+C. Supports multiple subscribers. | diff --git a/docs/TESTING.md b/docs/TESTING.md index 4adf4741d..1bbef729d 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -1074,7 +1074,8 @@ RUSTFLAGS="--cfg spawned_loom" cargo test -p spawned-rt --test cancellation_loom ``` The custom cfg isolates instrumentation from Tokio's own Loom configuration. -These models cover registration/cancellation races and reentrant callbacks, +These models cover registration/cancellation races, unregistration (a +`CancelRegistration` dropped) racing a cancel, and reentrant callbacks, not the entire actor runtime. Do not run ordinary actor tests with this cfg. The reply and job cores of deferred replies share their source with Loom diff --git a/rt/src/threads/mod.rs b/rt/src/threads/mod.rs index dc0fa4cbc..4a78edc9f 100644 --- a/rt/src/threads/mod.rs +++ b/rt/src/threads/mod.rs @@ -72,14 +72,67 @@ where type CancelCallback = Box; +/// Registered callbacks: a slab, so a [`CancelRegistration`] removes its own +/// entry in O(1). Each entry carries its registration's sequence number, +/// which tells it apart from a later entry reusing the slot and keeps the +/// firing order the registration order. +#[derive(Default)] +struct Callbacks { + slots: Vec>, + free: Vec, + next: u64, + live: usize, +} + +impl Callbacks { + fn insert(&mut self, callback: CancelCallback) -> (usize, u64) { + let seq = self.next; + self.next += 1; + let entry = Some((seq, callback)); + let index = match self.free.pop() { + Some(index) => { + self.slots[index] = entry; + index + } + None => { + self.slots.push(entry); + self.slots.len() - 1 + } + }; + self.live += 1; + (index, seq) + } + + fn remove(&mut self, index: usize, seq: u64) -> Option { + let slot = self.slots.get_mut(index)?; + if !matches!(slot, Some((s, _)) if *s == seq) { + return None; + } + let (_, callback) = slot.take()?; + self.free.push(index); + self.live -= 1; + Some(callback) + } + + /// Every registered callback, in registration order. + fn drain(&mut self) -> Vec { + let mut entries: Vec<_> = self.slots.drain(..).flatten().collect(); + self.free.clear(); + self.live = 0; + entries.sort_unstable_by_key(|(seq, _)| *seq); + entries.into_iter().map(|(_, callback)| callback).collect() + } +} + /// A token that can be used to signal cancellation. /// -/// Supports registering callbacks via `on_cancel()` that fire when -/// the token is cancelled, enabling efficient waiting patterns. +/// Supports registering callbacks that fire when the token is cancelled: +/// `on_cancel()` keeps its callback until then; `register_on_cancel()` +/// removes it when the returned registration is dropped. #[derive(Clone)] pub struct CancellationToken { is_cancelled: Arc, - callbacks: Arc>>, + callbacks: Arc>, } impl std::fmt::Debug for CancellationToken { @@ -100,7 +153,7 @@ impl CancellationToken { pub fn new() -> Self { CancellationToken { is_cancelled: Arc::new(false.into()), - callbacks: Arc::new(Mutex::new(Vec::new())), + callbacks: Arc::new(Mutex::new(Callbacks::default())), } } @@ -111,12 +164,11 @@ impl CancellationToken { pub fn cancel(&self) { self.is_cancelled.store(true, Ordering::SeqCst); // Fire all registered callbacks - let callbacks: Vec<_> = self + let callbacks = self .callbacks .lock() .unwrap_or_else(|e| e.into_inner()) - .drain(..) - .collect(); + .drain(); for cb in callbacks { cb(); } @@ -127,17 +179,80 @@ impl CancellationToken { /// /// This method is thread-safe: the callback is guaranteed to fire exactly /// once, either immediately (if already cancelled) or when `cancel()` is called. + /// It is kept until then: when the callback's purpose can end first (a + /// timer that fires), use [`register_on_cancel`](Self::register_on_cancel). pub fn on_cancel(&self, callback: CancelCallback) { + if let Err(callback) = self.insert(callback) { + callback(); + } + } + + /// Like [`on_cancel`](Self::on_cancel), but dropping the returned + /// registration removes the callback (O(1)) unless `cancel()` already + /// took it, so a long-lived token can serve any number of short-lived + /// waiters without retaining their callbacks. + /// + /// Once the drop returns the callback will not start; if `cancel()` took + /// it first, it runs (or is running) on the cancelling thread. + pub fn register_on_cancel(&self, callback: CancelCallback) -> CancelRegistration { + match self.insert(callback) { + Ok((index, seq)) => CancelRegistration { + entry: Some((self.callbacks.clone(), index, seq)), + }, + Err(callback) => { + callback(); + CancelRegistration { entry: None } + } + } + } + + /// Callbacks registered and not yet fired or unregistered. + pub fn callbacks_registered(&self) -> usize { + self.callbacks + .lock() + .unwrap_or_else(|e| e.into_inner()) + .live + } + + /// Store `callback` (its slot and sequence number), or hand it back when + /// the token is already cancelled. + fn insert(&self, callback: CancelCallback) -> Result<(usize, u64), CancelCallback> { // Hold the lock while checking is_cancelled to avoid a race with cancel(). // cancel() sets the flag BEFORE acquiring the lock, so if we see // is_cancelled=false while holding the lock, cancel() hasn't drained // callbacks yet and will drain ours after we release the lock. let mut callbacks = self.callbacks.lock().unwrap_or_else(|e| e.into_inner()); if self.is_cancelled() { - drop(callbacks); - callback(); - } else { - callbacks.push(callback); + return Err(callback); + } + Ok(callbacks.insert(callback)) + } +} + +/// A callback registered with [`CancellationToken::register_on_cancel`]. +/// Dropping it unregisters the callback unless `cancel()` already took it. +#[must_use = "dropping the registration unregisters the callback"] +pub struct CancelRegistration { + entry: Option<(Arc>, usize, u64)>, +} + +impl std::fmt::Debug for CancelRegistration { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CancelRegistration") + .field("registered", &self.entry.is_some()) + .finish() + } +} + +impl Drop for CancelRegistration { + fn drop(&mut self) { + if let Some((callbacks, index, seq)) = self.entry.take() { + let removed = callbacks + .lock() + .unwrap_or_else(|e| e.into_inner()) + .remove(index, seq); + // The callback's captures are dropped outside the lock. + drop(removed); } } } @@ -273,3 +388,102 @@ mod parking_tests { assert_eq!(value, 42); } } + +#[cfg(all(test, not(spawned_loom)))] +mod cancel_registration_tests { + use super::CancellationToken; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::Arc; + + #[test] + fn dropped_registrations_are_removed_and_slots_reused() { + let token = CancellationToken::new(); + let fired = Arc::new(AtomicUsize::new(0)); + for _ in 0..1000 { + let f = fired.clone(); + let registration = token.register_on_cancel(Box::new(move || { + f.fetch_add(1, Ordering::SeqCst); + })); + assert_eq!(token.callbacks_registered(), 1); + drop(registration); + } + assert_eq!(token.callbacks_registered(), 0); + assert_eq!( + token.callbacks.lock().unwrap().slots.len(), + 1, + "slot reused" + ); + assert_eq!(Arc::strong_count(&fired), 1, "callbacks dropped"); + token.cancel(); + assert_eq!(fired.load(Ordering::SeqCst), 0); + } + + #[test] + fn callbacks_fire_in_registration_order_across_reused_slots() { + let token = CancellationToken::new(); + let order = Arc::new(std::sync::Mutex::new(Vec::new())); + let push = |n| { + let order = order.clone(); + Box::new(move || order.lock().unwrap().push(n)) as Box + }; + let a = token.register_on_cancel(push(0)); + token.on_cancel(push(1)); + drop(a); + let _c = token.register_on_cancel(push(2)); // reuses slot 0 + token.on_cancel(push(3)); + token.cancel(); + assert_eq!(*order.lock().unwrap(), vec![1, 2, 3]); + } + + /// Unregistering from many threads while the token is cancelled: every + /// callback runs at most once, every kept one exactly once, and nothing + /// stays registered. + #[test] + fn unregister_racing_cancel_stress() { + for _ in 0..20 { + let token = CancellationToken::new(); + let workers: Vec<_> = (0..8) + .map(|_| { + let token = token.clone(); + std::thread::spawn(move || { + let mut kept = Vec::new(); + let mut dropped = Vec::new(); + for i in 0..2000 { + let count = Arc::new(AtomicUsize::new(0)); + let c = count.clone(); + let registration = token.register_on_cancel(Box::new(move || { + c.fetch_add(1, Ordering::SeqCst); + })); + if i % 50 == 0 { + kept.push((registration, count)); + } else { + drop(registration); + // A callback `cancel()` already took may + // still run (and drop) on its thread. + assert!(count.load(Ordering::SeqCst) <= 1); + dropped.push(count); + } + } + (kept, dropped) + }) + }) + .collect(); + std::thread::yield_now(); + token.cancel(); + for worker in workers { + let (kept, dropped) = worker.join().unwrap(); + for (registration, count) in kept { + assert_eq!(count.load(Ordering::SeqCst), 1, "kept callback"); + drop(registration); + } + // `cancel()` has returned: every callback it took ran and was + // dropped; every unregistered one was dropped by its drop. + for count in dropped { + assert!(count.load(Ordering::SeqCst) <= 1); + assert_eq!(Arc::strong_count(&count), 1, "callback leaked"); + } + } + assert_eq!(token.callbacks_registered(), 0); + } + } +} diff --git a/rt/tests/cancellation_loom.rs b/rt/tests/cancellation_loom.rs index f793a7e29..6edfeb15d 100644 --- a/rt/tests/cancellation_loom.rs +++ b/rt/tests/cancellation_loom.rs @@ -47,3 +47,63 @@ fn callbacks_can_register_callbacks_without_deadlocking() { assert_eq!(count.load(Ordering::SeqCst), 1); }); } + +/// A registration dropped while the token is cancelled: the callback runs at +/// most once, its captures are released on either path, and nothing stays +/// registered. +#[test] +fn unregister_racing_cancel_fires_at_most_once_and_leaves_nothing() { + loom::model(|| { + let token = CancellationToken::new(); + let count = Arc::new(AtomicUsize::new(0)); + let called = count.clone(); + let registration = token.register_on_cancel(Box::new(move || { + called.fetch_add(1, Ordering::SeqCst); + })); + let unregistering = loom::thread::spawn(move || drop(registration)); + let cancelling = token.clone(); + let cancellation = loom::thread::spawn(move || cancelling.cancel()); + unregistering.join().unwrap(); + cancellation.join().unwrap(); + assert!(count.load(Ordering::SeqCst) <= 1); + assert_eq!(Arc::strong_count(&count), 1, "the callback was not dropped"); + assert_eq!(token.callbacks_registered(), 0); + }); +} + +/// Unregistering one callback while another registration races the cancel: +/// the kept one fires exactly once, the dropped one never after its drop. +#[test] +fn unregister_leaves_other_registrations_to_fire() { + loom::model(|| { + let token = CancellationToken::new(); + let kept = Arc::new(AtomicUsize::new(0)); + let dropped = Arc::new(AtomicUsize::new(0)); + let (k, d) = (kept.clone(), dropped.clone()); + let keep = token.register_on_cancel(Box::new(move || { + k.fetch_add(1, Ordering::SeqCst); + })); + let gone = token.register_on_cancel(Box::new(move || { + d.fetch_add(1, Ordering::SeqCst); + })); + drop(gone); + let cancelling = token.clone(); + let cancellation = loom::thread::spawn(move || cancelling.cancel()); + let registering = token.clone(); + let late = Arc::new(AtomicUsize::new(0)); + let l = late.clone(); + let registration = loom::thread::spawn(move || { + registering.register_on_cancel(Box::new(move || { + l.fetch_add(1, Ordering::SeqCst); + })) + }); + cancellation.join().unwrap(); + let late_registration = registration.join().unwrap(); + assert_eq!(kept.load(Ordering::SeqCst), 1); + assert_eq!(dropped.load(Ordering::SeqCst), 0); + // Registered before or after the cancel, it fired exactly once. + assert_eq!(late.load(Ordering::SeqCst), 1); + drop((keep, late_registration)); + assert_eq!(token.callbacks_registered(), 0); + }); +}