From f975271f3f30d6652323fe19bd8b007b82b00e07 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Mon, 5 Jan 2026 09:03:45 -0800 Subject: [PATCH] Use Ordering::Acquire in Arc::try_unwrap After unwrapping the Arc, the reader should observe all writes that occurred before the penultimate refcount was dropped. Note that this is consistent with the ordering used in `Arc::is_unique` and `Arc::make_mut`, both of which have similar function. --- library/alloc/src/sync.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 4180fe91cb558..1542d7b47574b 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -1104,7 +1104,11 @@ impl Arc { #[inline] #[stable(feature = "arc_unique", since = "1.4.0")] pub fn try_unwrap(this: Self) -> Result { - if this.inner().strong.compare_exchange(1, 0, Relaxed, Relaxed).is_err() { + // This `Acquire` synchronizes with the `Release` in the `Arc::drop` + // of the penultimate `Arc`, allowing the resulting value to contain + // all modifications that may have happened due to that copy of the + // `Arc`. + if this.inner().strong.compare_exchange(1, 0, Acquire, Relaxed).is_err() { return Err(this); }