Skip to content

Commit 06eb782

Browse files
committed
modify LazyLock poison panic message
Fixes an issue where if the underlying `Once` panics because it is poisoned, the panic displays the wrong message. Signed-off-by: Connor Tsui <[email protected]>
1 parent 6ba0ce4 commit 06eb782

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

library/std/src/sync/lazy_lock.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
244244
#[inline]
245245
#[stable(feature = "lazy_cell", since = "1.80.0")]
246246
pub fn force(this: &LazyLock<T, F>) -> &T {
247-
this.once.call_once(|| {
247+
this.once.call_once_force(|state| {
248+
if state.is_poisoned() {
249+
panic_poisoned();
250+
}
251+
248252
// SAFETY: `call_once` only runs this closure once, ever.
249253
let data = unsafe { &mut *this.data.get() };
250254
let f = unsafe { ManuallyDrop::take(&mut data.f) };
@@ -257,8 +261,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
257261
// * the closure was called and initialized `value`.
258262
// * the closure was called and panicked, so this point is never reached.
259263
// * the closure was not called, but a previous call initialized `value`.
260-
// * the closure was not called because the Once is poisoned, so this point
261-
// is never reached.
264+
// * the closure was not called because the Once is poisoned, which we handled above.
262265
// So `value` has definitely been initialized and will not be modified again.
263266
unsafe { &*(*this.data.get()).value }
264267
}

library/std/tests/sync/lazy_lock.rs

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ fn lazy_default() {
3333
assert_eq!(CALLED.load(SeqCst), 1);
3434
}
3535

36-
#[test]
37-
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
38-
fn lazy_poisoning() {
39-
let x: LazyCell<String> = LazyCell::new(|| panic!("kaboom"));
40-
for _ in 0..2 {
41-
let res = panic::catch_unwind(panic::AssertUnwindSafe(|| x.len()));
42-
assert!(res.is_err());
43-
}
44-
}
45-
4636
#[test]
4737
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
4838
fn sync_lazy_new() {
@@ -123,16 +113,6 @@ fn static_sync_lazy_via_fn() {
123113
assert_eq!(xs(), &vec![1, 2, 3]);
124114
}
125115

126-
#[test]
127-
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
128-
fn sync_lazy_poisoning() {
129-
let x: LazyLock<String> = LazyLock::new(|| panic!("kaboom"));
130-
for _ in 0..2 {
131-
let res = panic::catch_unwind(|| x.len());
132-
assert!(res.is_err());
133-
}
134-
}
135-
136116
// Check that we can infer `T` from closure's type.
137117
#[test]
138118
fn lazy_type_inference() {
@@ -145,17 +125,6 @@ fn is_sync_send() {
145125
assert_traits::<LazyLock<String>>();
146126
}
147127

148-
#[test]
149-
#[should_panic = "has previously been poisoned"]
150-
fn lazy_force_mut_panic() {
151-
let mut lazy = LazyLock::<String>::new(|| panic!());
152-
panic::catch_unwind(panic::AssertUnwindSafe(|| {
153-
let _ = LazyLock::force_mut(&mut lazy);
154-
}))
155-
.unwrap_err();
156-
let _ = &*lazy;
157-
}
158-
159128
#[test]
160129
fn lazy_force_mut() {
161130
let s = "abc".to_owned();
@@ -165,3 +134,56 @@ fn lazy_force_mut() {
165134
p.clear();
166135
LazyLock::force_mut(&mut lazy);
167136
}
137+
138+
#[test]
139+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
140+
fn lazy_poisoning() {
141+
let x: LazyCell<String> = LazyCell::new(|| panic!("kaboom"));
142+
for _ in 0..2 {
143+
let res = panic::catch_unwind(panic::AssertUnwindSafe(|| x.len()));
144+
assert!(res.is_err());
145+
}
146+
}
147+
148+
/// Verifies that when a `LazyLock` is poisoned, it panics with the correct error message ("LazyLock
149+
/// instance has previously been poisoned") instead of the underlying `Once` error message.
150+
#[test]
151+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
152+
#[should_panic(expected = "LazyLock instance has previously been poisoned")]
153+
fn lazy_lock_deref_panic() {
154+
let lazy: LazyLock<String> = LazyLock::new(|| panic!("initialization failed"));
155+
156+
// First access will panic during initialization.
157+
let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| {
158+
let _ = &*lazy;
159+
}));
160+
161+
// Second access should panic with the poisoned message.
162+
let _ = &*lazy;
163+
}
164+
165+
#[test]
166+
#[should_panic(expected = "LazyLock instance has previously been poisoned")]
167+
fn lazy_lock_deref_mut_panic() {
168+
let mut lazy: LazyLock<String> = LazyLock::new(|| panic!("initialization failed"));
169+
170+
// First access will panic during initialization.
171+
let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| {
172+
let _ = LazyLock::force_mut(&mut lazy);
173+
}));
174+
175+
// Second access should panic with the poisoned message.
176+
let _ = &*lazy;
177+
}
178+
179+
/// Verifies that when the initialization closure panics with a custom message, that message is
180+
/// preserved and not overridden by `LazyLock`.
181+
#[test]
182+
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
183+
#[should_panic(expected = "custom panic message from closure")]
184+
fn lazy_lock_preserves_closure_panic_message() {
185+
let lazy: LazyLock<String> = LazyLock::new(|| panic!("custom panic message from closure"));
186+
187+
// This should panic with the original message from the closure.
188+
let _ = &*lazy;
189+
}

0 commit comments

Comments
 (0)