error-concept

RwLock poisoning: reader versus writer panic

With ordinary unwinding, a panic while holding a write guard can poison an RwLock; a panic under a read guard does not poison it. This isolated fixture suppresses only its intentional panic-hook output and restores the original hook. Expected stdout (not executed): false true Common mistake: Believing every panic under an RwLock poisons it.

Formal Syntax & Structure

rust syntaxrust
RwLock::is_poisoned(); catch_unwind

Working Example

Example Snippetrust
fn main() {    use std::sync::RwLock;    let l = RwLock::new(1);    let hook = std::panic::take_hook();    std::panic::set_hook(Box::new(|_| {}));    let _ = std::panic::catch_unwind(|| { let _g = l.read().unwrap(); panic!("reader"); });    println!("{}", l.is_poisoned());    let _ = std::panic::catch_unwind(|| { let _g = l.write().unwrap(); panic!("writer"); });    std::panic::set_hook(hook);    println!("{}", l.is_poisoned());}
Rust Reference