From 5202981b9f2e4937af21e322b3ef7e8fb67759cc Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 18 Nov 2026 18:28:09 +0800 Subject: [PATCH 1/8] refactor(once): initialize only empty value cells OnceCell and LazyCell check the slot under exclusive access before initializing it. Reject an occupied slot before touching its value to avoid leaving a partially dropped value marked as published if its destructor panics. --- asyncband/src/internal/value_cell.rs | 30 ++++++++++------------------ asyncband/src/once/lazy_cell/mod.rs | 2 +- asyncband/src/once/once_cell/mod.rs | 8 ++------ 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/asyncband/src/internal/value_cell.rs b/asyncband/src/internal/value_cell.rs index 8c3f63be..f537baf4 100644 --- a/asyncband/src/internal/value_cell.rs +++ b/asyncband/src/internal/value_cell.rs @@ -96,13 +96,11 @@ impl ValueCell { unsafe { (*slot).assume_init_ref() } } - pub fn write_mut(&mut self, value: T) -> &mut T { - if *self.published.get_mut() { - // SAFETY: `published` indicates an initialized value in `slot`. - unsafe { self.slot.get_mut().assume_init_drop() }; - } + pub fn init_mut(&mut self, value: T) -> &mut T { + assert!(!self.has_value_mut(), "cell already initialized"); + let value = self.slot.get_mut().write(value); *self.published.get_mut() = true; - self.slot.get_mut().write(value) + value } pub fn take(&mut self) -> Option { @@ -187,34 +185,28 @@ mod tests { assert!(cell.get_mut().is_none()); assert!(cell.take().is_none()); - cell.write_mut(DropCounter(&drops)); + cell.init_mut(DropCounter(&drops)); assert!(cell.has_value_mut()); assert_eq!(drops.get(), 0); - // Overwriting an already populated slot must drop the prior value. - cell.write_mut(DropCounter(&drops)); - assert_eq!(drops.get(), 1); - let taken = cell.take(); assert!(taken.is_some()); assert!(!cell.has_value_mut()); - assert_eq!(drops.get(), 1); + assert_eq!(drops.get(), 0); drop(taken); - assert_eq!(drops.get(), 2); + assert_eq!(drops.get(), 1); - // Re-publishing after `take` works and transfers ownership to `into_inner`. - // SAFETY: `cell` is empty and accessed from a single thread. - unsafe { cell.publish(DropCounter(&drops)) }; + cell.init_mut(DropCounter(&drops)); assert!(cell.is_published()); let extracted = cell.into_inner(); assert!(extracted.is_some()); - assert_eq!(drops.get(), 2); + assert_eq!(drops.get(), 1); drop(extracted); - assert_eq!(drops.get(), 3); + assert_eq!(drops.get(), 2); // `with_value` and `Drop` drop the stored value once. let cell = ValueCell::with_value(DropCounter(&drops)); drop(cell); - assert_eq!(drops.get(), 4); + assert_eq!(drops.get(), 3); } } diff --git a/asyncband/src/once/lazy_cell/mod.rs b/asyncband/src/once/lazy_cell/mod.rs index e222ebf7..26563e63 100644 --- a/asyncband/src/once/lazy_cell/mod.rs +++ b/asyncband/src/once/lazy_cell/mod.rs @@ -378,7 +378,7 @@ where // SAFETY: Exclusive access to the pinned cell pins its state in place for this call. let state = unsafe { Pin::new_unchecked(this.state.get_mut()) }; let value = drive_pinned_attempt(state, &this.poisoned).await; - this.value.write_mut(value) + this.value.init_mut(value) } } diff --git a/asyncband/src/once/once_cell/mod.rs b/asyncband/src/once/once_cell/mod.rs index 4a537055..2c7950f1 100644 --- a/asyncband/src/once/once_cell/mod.rs +++ b/asyncband/src/once/once_cell/mod.rs @@ -275,7 +275,7 @@ impl OnceCell { where F: AsyncFnOnce() -> Result, { - // Check the flag before borrowing so the fallback branch can call `set_value_mut`. + // Check the flag before borrowing so the fallback branch can initialize the slot. if self.value.has_value_mut() { return Ok(self .value @@ -284,7 +284,7 @@ impl OnceCell { } let value = init().await?; - Ok(self.set_value_mut(value)) + Ok(self.value.init_mut(value)) } /// Stores `value` if the cell is empty and returns a reference to it. @@ -414,10 +414,6 @@ impl OnceCell { // permits. Holding them through publication excludes other initializers. unsafe { self.value.publish(value) } } - - fn set_value_mut(&mut self, value: T) -> &mut T { - self.value.write_mut(value) - } } impl fmt::Debug for OnceCell { From 12e212ea8b8a40da014b61b6185e46c8b5ac3fb3 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 18 Nov 2026 18:28:15 +0800 Subject: [PATCH 2/8] refactor(once): use Pin::set for lazy cell transitions Pin::set drops the old state in place before installing its replacement. Keep the poison guard around the completed future's destructor. --- asyncband/src/once/lazy_cell/mod.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/asyncband/src/once/lazy_cell/mod.rs b/asyncband/src/once/lazy_cell/mod.rs index 26563e63..b3191431 100644 --- a/asyncband/src/once/lazy_cell/mod.rs +++ b/asyncband/src/once/lazy_cell/mod.rs @@ -408,10 +408,7 @@ where initializer() }; - // SAFETY: The current variant contains no structurally pinned future, and its initializer - // slot is empty. The newly created future is written directly into its final pinned - // location before it is polled. - *unsafe { state.as_mut().get_unchecked_mut() } = State::Attempt(future); + state.set(State::Attempt(future)); } let value = poll_fn(|cx| { @@ -427,11 +424,9 @@ where }) .await; - // Assignment drops the completed future in place before changing the phase. Treat a panic - // from that destructor as an initializer panic as well. + // A panic while dropping the completed future also poisons the cell. let _poison = PoisonOnPanic(poisoned); - // SAFETY: Dropping a pinned value in place is permitted, and no value is moved out. - *unsafe { state.as_mut().get_unchecked_mut() } = State::Complete; + state.set(State::Complete); value } From 1ba312e0fe7d88caada1c31cf9883a0b5a270cd4 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 18 Nov 2026 18:28:30 +0800 Subject: [PATCH 3/8] refactor(watch): share version publication between senders Advance the version, exchange the value and detach the waiters under one lock. Callers retain ownership of the exchanged value across the lock boundary, including if the counter overflows. --- asyncband/src/watch/mod.rs | 52 ++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/asyncband/src/watch/mod.rs b/asyncband/src/watch/mod.rs index 8a14a958..d68b7b08 100644 --- a/asyncband/src/watch/mod.rs +++ b/asyncband/src/watch/mod.rs @@ -74,6 +74,7 @@ use std::task::Poll; pub use self::error::RecvError; pub use self::error::SendError; use crate::internal::mutex::Mutex; +use crate::internal::waker_batch::WakerBatch; use crate::internal::wakerset::WakerSet; use crate::internal::wakerset::WakerToken; @@ -118,6 +119,19 @@ struct State { waiters: WakerSet, } +impl State { + // Leave the old value with the caller so it can be dropped after unlocking. Borrowing the + // input also keeps its destructor outside the lock if the version counter overflows. + fn publish(&mut self, value: &mut T) -> WakerBatch { + self.version = self + .version + .checked_add(1) + .expect("watch channel version counter overflowed"); + mem::swap(&mut self.value, value); + self.waiters.take_all() + } +} + /// The sending side of a watch channel. pub struct Sender { shared: Arc>, @@ -168,23 +182,15 @@ impl Sender { /// # Panics /// /// Panics if the channel has already published `u64::MAX` updates. - pub fn send(&self, value: T) -> Result<(), SendError> { - let mut state = self.shared.state.lock(); - if state.receivers == 0 { - return Err(SendError::new(value)); - } - let version = state - .version - .checked_add(1) - .expect("watch channel version counter overflowed"); - let replaced = mem::replace(&mut state.value, value); - state.version = version; - let mut wakers = state.waiters.take_all(); - drop(state); - - // Waker callbacks and the replaced value's destructor may reenter this channel. + pub fn send(&self, mut value: T) -> Result<(), SendError> { + let mut wakers = { + let mut state = self.shared.state.lock(); + if state.receivers == 0 { + return Err(SendError::new(value)); + } + state.publish(&mut value) + }; wakers.wake_all(); - drop(replaced); Ok(()) } @@ -196,18 +202,10 @@ impl Sender { /// # Panics /// /// Panics if the channel has already published `u64::MAX` updates. - pub fn send_replace(&self, value: T) -> T { - let mut state = self.shared.state.lock(); - let version = state - .version - .checked_add(1) - .expect("watch channel version counter overflowed"); - let replaced = mem::replace(&mut state.value, value); - state.version = version; - let mut wakers = state.waiters.take_all(); - drop(state); + pub fn send_replace(&self, mut value: T) -> T { + let mut wakers = self.shared.state.lock().publish(&mut value); wakers.wake_all(); - replaced + value } /// Creates a receiver that considers the current value already observed. From c7cc07f45d0def0da7fafbe71d5d8ee5fd3bf50c Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 18 Nov 2026 18:29:03 +0800 Subject: [PATCH 4/8] refactor(event): encode waiter notification in a shared enum Both reset modes transfer the waker when detaching a waiting node. Represent that transition once so a notified manual-reset waiter stays ready across reset and cannot retain a waker. --- asyncband/src/event/auto_reset.rs | 11 ++---- asyncband/src/event/manual_reset.rs | 55 ++++++++++++----------------- asyncband/src/event/mod.rs | 17 +++++++++ 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/asyncband/src/event/auto_reset.rs b/asyncband/src/event/auto_reset.rs index 0365fa58..05b7edd7 100644 --- a/asyncband/src/event/auto_reset.rs +++ b/asyncband/src/event/auto_reset.rs @@ -24,6 +24,7 @@ use std::task::Context; use std::task::Poll; use std::task::Waker; +use crate::event::Waiter; use crate::internal::mutex::Mutex; use crate::internal::waitlist::WaitList; use crate::internal::waitlist::WaiterId; @@ -278,10 +279,7 @@ struct State { impl State { fn signal(&mut self) -> Option { if let Some((_, waiter)) = self.waiters.unlink_first_waiter(|_| true) { - let Waiter::Waiting(waker) = mem::replace(waiter, Waiter::Notified) else { - unreachable!("only unselected waits remain queued") - }; - Some(waker) + Some(waiter.mark_notified()) } else { self.is_set = true; None @@ -289,11 +287,6 @@ impl State { } } -enum Waiter { - Waiting(Waker), - Notified, -} - #[must_use = "futures do nothing unless you `.await` or poll them"] struct Wait<'a> { event: &'a AutoResetEvent, diff --git a/asyncband/src/event/manual_reset.rs b/asyncband/src/event/manual_reset.rs index 7a7ec588..c73d36df 100644 --- a/asyncband/src/event/manual_reset.rs +++ b/asyncband/src/event/manual_reset.rs @@ -17,16 +17,16 @@ use std::fmt; use std::future::Future; +use std::mem; use std::pin::Pin; use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use std::task::Context; use std::task::Poll; -use std::task::Waker; +use crate::event::Waiter; use crate::internal::mutex::Mutex; -use crate::internal::register_waker; use crate::internal::waitlist::WaitList; use crate::internal::waitlist::WaiterId; use crate::internal::waker_batch::WakerBatch; @@ -110,13 +110,8 @@ impl ManualResetEvent { self.is_set.store(true, Ordering::Release); // Detach the complete cohort before invoking any waker. A wake callback may reset the // event and register a new wait, which must belong to the state current at that point. - while let Some((_id, waiter)) = waiters.unlink_first_waiter(|waiter| { - waiter.notified = true; - true - }) { - if let Some(waker) = waiter.waker.take() { - wakers.push(waker); - } + while let Some((_, waiter)) = waiters.unlink_first_waiter(|_| true) { + wakers.push(waiter.mark_notified()); } } wakers.wake_all(); @@ -218,7 +213,7 @@ impl ManualResetEvent { /// /// `set` unlinks every queued waiter and marks it notified, and a wait that starts while the /// event is set never enqueues. A linked waiter therefore always belongs to an unset event, so - /// `notified` alone decides whether a registered waiter is already committed. + /// a notified waiter remains ready even if the event is reset. fn poll_wait(&self, waiter_id: &mut Option, cx: &mut Context<'_>) -> Poll<()> { // Only an unregistered wait can skip the lock: a released waiter still owns a node. if waiter_id.is_none() && self.is_set() { @@ -228,27 +223,27 @@ impl ManualResetEvent { let (poll, retired_waker) = { let mut waiters = self.waiters.lock(); match *waiter_id { - Some(id) if waiters.waiter_mut(id).notified => { - let waiter = waiters.remove_unlinked_waiter(id); - *waiter_id = None; - (Poll::Ready(()), waiter.waker) - } - Some(id) => { - debug_assert!( - !self.is_set.load(Ordering::Relaxed), - "a linked waiter must belong to an unset event" - ); - let waiter = waiters.waiter_mut(id); - (Poll::Pending, register_waker(&mut waiter.waker, cx.waker())) - } + Some(id) => match waiters.waiter_mut(id) { + Waiter::Notified => { + waiters.remove_unlinked_waiter(id); + *waiter_id = None; + (Poll::Ready(()), None) + } + Waiter::Waiting(waker) => { + debug_assert!( + !self.is_set.load(Ordering::Relaxed), + "a linked waiter must belong to an unset event" + ); + let retired = (!waker.will_wake(cx.waker())) + .then(|| mem::replace(waker, cx.waker().clone())); + (Poll::Pending, retired) + } + }, // A set after the false fast probe must either be observed here or release // the node that we register while holding this same lock. None if self.is_set.load(Ordering::Relaxed) => (Poll::Ready(()), None), None => { - *waiter_id = Some(waiters.push_back(Waiter { - notified: false, - waker: Some(cx.waker().clone()), - })); + *waiter_id = Some(waiters.push_back(Waiter::Waiting(cx.waker().clone()))); (Poll::Pending, None) } } @@ -283,12 +278,6 @@ impl fmt::Debug for ManualResetEvent { } } -#[derive(Debug)] -struct Waiter { - notified: bool, - waker: Option, -} - #[must_use = "futures do nothing unless you `.await` or poll them"] struct Wait<'a> { event: &'a ManualResetEvent, diff --git a/asyncband/src/event/mod.rs b/asyncband/src/event/mod.rs index 8f94320f..daf98ed3 100644 --- a/asyncband/src/event/mod.rs +++ b/asyncband/src/event/mod.rs @@ -31,5 +31,22 @@ mod auto_reset; mod manual_reset; +use std::mem; +use std::task::Waker; + pub use self::auto_reset::AutoResetEvent; pub use self::manual_reset::ManualResetEvent; + +enum Waiter { + Waiting(Waker), + Notified, +} + +impl Waiter { + fn mark_notified(&mut self) -> Waker { + let Self::Waiting(waker) = mem::replace(self, Self::Notified) else { + unreachable!("only waiting nodes remain queued"); + }; + waker + } +} From 823fa4d560d1988a05804ec894b272a4f3f8f63d Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 18 Nov 2026 18:29:12 +0800 Subject: [PATCH 5/8] refactor(phaser): name the participant's arrived phase The stored phase identifies an arrival that still needs to be observed. The same value prevents a repeated arrival or withdrawal from decrementing the count twice. --- asyncband/src/phaser/mod.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/asyncband/src/phaser/mod.rs b/asyncband/src/phaser/mod.rs index 79bf7ecd..a69951aa 100644 --- a/asyncband/src/phaser/mod.rs +++ b/asyncband/src/phaser/mod.rs @@ -419,8 +419,8 @@ impl Drop for PhaserParticipants { #[derive(Debug)] pub struct PhaserParticipant { phaser: Phaser, - // Cleared only after advancement, so a pending current phase also records arrival. - pending: Option, + // Retain the arrival across cancellation so a retry can observe the same phase. + arrived_phase: Option, registered: bool, } @@ -428,7 +428,7 @@ impl PhaserParticipant { fn new(phaser: Phaser) -> Self { Self { phaser, - pending: None, + arrived_phase: None, registered: true, } } @@ -453,10 +453,10 @@ impl PhaserParticipant { return Err(Closed(())); } let phase = state.phase; - if self.pending != Some(phase) { + if self.arrived_phase != Some(phase) { state.unarrived -= 1; } - self.pending = Some(phase); + self.arrived_phase = Some(phase); let mut wakers = state.advance_if_ready(); drop(state); wakers.wake_all(); @@ -480,12 +480,12 @@ impl PhaserParticipant { /// the pending observation, so retrying this method waits for the same phase without /// counting another arrival. An explicit new `arrive` replaces that pending observation. pub async fn wait(&mut self) -> Result { - let observed = match self.pending { + let observed = match self.arrived_phase { Some(phase) => phase, None => self.arrive()?, }; let next = self.phaser.wait(observed).await?; - self.pending = None; + self.arrived_phase = None; Ok(next) } @@ -506,7 +506,7 @@ impl PhaserParticipant { let mut state = self.phaser.state.lock(); self.registered = false; state.registered -= 1; - if self.pending != Some(state.phase) { + if self.arrived_phase != Some(state.phase) { state.unarrived -= 1; } let result = if state.closed { From 5ff375c283fb7a6e9468d60c38efd6cb67b53854 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 18 Nov 2026 18:29:25 +0800 Subject: [PATCH 6/8] fix(bench): preserve run order in upgradable stage reports Compute the minimum, median and maximum with the harness summary function. Print the original measurements so slow early runs and timing drift remain visible. --- benchmarks/benches/upgradable_stages.rs | 28 +++++++++---------------- benchmarks/src/harness.rs | 3 ++- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/benchmarks/benches/upgradable_stages.rs b/benchmarks/benches/upgradable_stages.rs index b80f59b5..c41ed584 100644 --- a/benchmarks/benches/upgradable_stages.rs +++ b/benchmarks/benches/upgradable_stages.rs @@ -22,6 +22,7 @@ use std::time::Instant; use asyncband::rwlock::RwLockUpgradableReadGuard; use asyncband::rwlock::UpgradableRwLock; +use benchmarks::harness::summary; use benchmarks::support::bench_context; use benchmarks::support::poll_pending; use benchmarks::support::poll_pinned_ready; @@ -165,17 +166,8 @@ fn measure(rounds: usize, repeat: usize) -> Results { results } -fn report(name: &str, rounds: usize, mut samples: Vec) { - samples.sort_unstable(); - let middle = samples.len() / 2; - let median = if samples.len() % 2 == 0 { - let lower = samples[middle - 1]; - lower + (samples[middle] - lower) / 2 - } else { - samples[middle] - }; - let minimum = samples.first().unwrap(); - let maximum = samples.last().unwrap(); +fn report(name: &str, rounds: usize, samples: &[Duration]) { + let (minimum, median, maximum) = summary(samples); let ns_per_round = median.as_nanos() as f64 / rounds as f64; let runs = samples .iter() @@ -213,16 +205,16 @@ fn main() { report( "empty timing interval", options.rounds, - results.empty_interval, + &results.empty_interval, ); - report("acquisition", options.rounds, results.acquisition); - report("promotion", options.rounds, results.promotion); + report("acquisition", options.rounds, &results.acquisition); + report("promotion", options.rounds, &results.promotion); report( "acquisition + reader held", options.rounds, - results.acquisition_with_reader, + &results.acquisition_with_reader, ); - for (&reader_count, samples) in READER_COUNTS.iter().zip(results.promotion_with_readers) { + for (&reader_count, samples) in READER_COUNTS.iter().zip(&results.promotion_with_readers) { report( &format!("promotion + {reader_count} readers"), options.rounds, @@ -232,11 +224,11 @@ fn main() { report( "promotion + writer queued", options.rounds, - results.promotion_with_waiting_writer, + &results.promotion_with_waiting_writer, ); report( "acquire behind queued writer", options.rounds, - results.acquisition_after_waiting_writer, + &results.acquisition_after_waiting_writer, ); } diff --git a/benchmarks/src/harness.rs b/benchmarks/src/harness.rs index a18cae1f..57e65c34 100644 --- a/benchmarks/src/harness.rs +++ b/benchmarks/src/harness.rs @@ -209,7 +209,8 @@ impl Runner { } } -fn summary(samples: &[Duration]) -> (Duration, Duration, Duration) { +/// Returns the minimum, median, and maximum of a nonempty sample. +pub fn summary(samples: &[Duration]) -> (Duration, Duration, Duration) { let mut sorted = samples.to_vec(); sorted.sort_unstable(); let lo = sorted[(sorted.len() - 1) / 2]; From 6fab39f457b85d821f4c89d9c745806661938d41 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 18 Nov 2026 18:29:34 +0800 Subject: [PATCH 7/8] refactor(xtask): list Miri integration test targets once Keep the selected binaries in execution order and use the same command builder for each. The library suite runs first with all features. --- xtask/src/main.rs | 45 ++++++++++++++------------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index bc0419aa..d2751b68 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -129,37 +129,20 @@ struct CommandMiri; impl CommandMiri { fn run(self) { run_command(make_miri_cmd(PACKAGE_NAME, &["--lib", "--all-features"])); - run_command(make_miri_cmd( - "tests-integration", - &["--test", "oneshot_test"], - )); - run_command(make_miri_cmd( - "tests-integration", - &["--test", "unsafe_paths_test"], - )); - run_command(make_miri_cmd( - "tests-integration", - &["--test", "upgradable_rwlock_test"], - )); - run_command(make_miri_cmd("tests-integration", &["--test", "mpsc_test"])); - run_command(make_miri_cmd("tests-integration", &["--test", "mpmc_test"])); - run_command(make_miri_cmd("tests-integration", &["--test", "spmc_test"])); - run_command(make_miri_cmd( - "tests-integration", - &["--test", "waitgroup_test"], - )); - run_command(make_miri_cmd( - "tests-integration", - &["--test", "broadcast_test"], - )); - run_command(make_miri_cmd( - "tests-integration", - &["--test", "phaser_test"], - )); - run_command(make_miri_cmd( - "tests-integration", - &["--test", "semaphore_ordering_test"], - )); + for target in [ + "oneshot_test", + "unsafe_paths_test", + "upgradable_rwlock_test", + "mpsc_test", + "mpmc_test", + "spmc_test", + "waitgroup_test", + "broadcast_test", + "phaser_test", + "semaphore_ordering_test", + ] { + run_command(make_miri_cmd("tests-integration", &["--test", target])); + } } } From 696e4ffaf5df27347f615e641bd5a76ac6e54929 Mon Sep 17 00:00:00 2001 From: tison Date: Wed, 18 Nov 2026 20:39:21 +0800 Subject: [PATCH 8/8] fix(once): clear the value cell before dropping an overwritten value An old value can panic during destruction. Mark the slot empty before calling its destructor and publish the replacement only after writing it. This preserves exclusive overwrite semantics and prevents the cell from dropping the old value again during unwinding. --- asyncband/src/internal/value_cell.rs | 58 ++++++++++++++++++++++------ asyncband/src/once/lazy_cell/mod.rs | 2 +- asyncband/src/once/once_cell/mod.rs | 8 +++- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/asyncband/src/internal/value_cell.rs b/asyncband/src/internal/value_cell.rs index f537baf4..78b6178e 100644 --- a/asyncband/src/internal/value_cell.rs +++ b/asyncband/src/internal/value_cell.rs @@ -96,11 +96,17 @@ impl ValueCell { unsafe { (*slot).assume_init_ref() } } - pub fn init_mut(&mut self, value: T) -> &mut T { - assert!(!self.has_value_mut(), "cell already initialized"); - let value = self.slot.get_mut().write(value); - *self.published.get_mut() = true; - value + pub fn write_mut(&mut self, value: T) -> &mut T { + let published = self.published.get_mut(); + if *published { + *published = false; + // SAFETY: The slot held a live value. Clear the flag before dropping it so an + // unwinding destructor leaves the cell empty. + unsafe { self.slot.get_mut().assume_init_drop() }; + } + let stored = self.slot.get_mut().write(value); + *published = true; + stored } pub fn take(&mut self) -> Option { @@ -134,6 +140,8 @@ impl Drop for ValueCell { #[cfg(test)] mod tests { use std::cell::Cell; + use std::panic::AssertUnwindSafe; + use std::panic::catch_unwind; use std::thread; use super::*; @@ -185,28 +193,54 @@ mod tests { assert!(cell.get_mut().is_none()); assert!(cell.take().is_none()); - cell.init_mut(DropCounter(&drops)); + cell.write_mut(DropCounter(&drops)); assert!(cell.has_value_mut()); assert_eq!(drops.get(), 0); + // Overwriting an already populated slot must drop the prior value. + cell.write_mut(DropCounter(&drops)); + assert_eq!(drops.get(), 1); + let taken = cell.take(); assert!(taken.is_some()); assert!(!cell.has_value_mut()); - assert_eq!(drops.get(), 0); - drop(taken); assert_eq!(drops.get(), 1); + drop(taken); + assert_eq!(drops.get(), 2); - cell.init_mut(DropCounter(&drops)); + // Re-publishing after `take` works and transfers ownership to `into_inner`. + // SAFETY: `cell` is empty and accessed from a single thread. + unsafe { cell.publish(DropCounter(&drops)) }; assert!(cell.is_published()); let extracted = cell.into_inner(); assert!(extracted.is_some()); - assert_eq!(drops.get(), 1); - drop(extracted); assert_eq!(drops.get(), 2); + drop(extracted); + assert_eq!(drops.get(), 3); // `with_value` and `Drop` drop the stored value once. let cell = ValueCell::with_value(DropCounter(&drops)); drop(cell); - assert_eq!(drops.get(), 3); + assert_eq!(drops.get(), 4); + } + + #[test] + fn overwrite_leaves_cell_empty_if_dropping_the_old_value_panics() { + struct PanicOnDrop; + + impl Drop for PanicOnDrop { + fn drop(&mut self) { + panic!("old value dropped"); + } + } + + // Only drop the cell after verifying that it no longer claims to own the old value. + let mut cell = ManuallyDrop::new(ValueCell::with_value(Some(PanicOnDrop))); + let result = catch_unwind(AssertUnwindSafe(|| { + cell.write_mut(None); + })); + assert!(result.is_err()); + assert!(!cell.is_published()); + drop(ManuallyDrop::into_inner(cell)); } } diff --git a/asyncband/src/once/lazy_cell/mod.rs b/asyncband/src/once/lazy_cell/mod.rs index b3191431..a4374d10 100644 --- a/asyncband/src/once/lazy_cell/mod.rs +++ b/asyncband/src/once/lazy_cell/mod.rs @@ -378,7 +378,7 @@ where // SAFETY: Exclusive access to the pinned cell pins its state in place for this call. let state = unsafe { Pin::new_unchecked(this.state.get_mut()) }; let value = drive_pinned_attempt(state, &this.poisoned).await; - this.value.init_mut(value) + this.value.write_mut(value) } } diff --git a/asyncband/src/once/once_cell/mod.rs b/asyncband/src/once/once_cell/mod.rs index 2c7950f1..4a537055 100644 --- a/asyncband/src/once/once_cell/mod.rs +++ b/asyncband/src/once/once_cell/mod.rs @@ -275,7 +275,7 @@ impl OnceCell { where F: AsyncFnOnce() -> Result, { - // Check the flag before borrowing so the fallback branch can initialize the slot. + // Check the flag before borrowing so the fallback branch can call `set_value_mut`. if self.value.has_value_mut() { return Ok(self .value @@ -284,7 +284,7 @@ impl OnceCell { } let value = init().await?; - Ok(self.value.init_mut(value)) + Ok(self.set_value_mut(value)) } /// Stores `value` if the cell is empty and returns a reference to it. @@ -414,6 +414,10 @@ impl OnceCell { // permits. Holding them through publication excludes other initializers. unsafe { self.value.publish(value) } } + + fn set_value_mut(&mut self, value: T) -> &mut T { + self.value.write_mut(value) + } } impl fmt::Debug for OnceCell {