diff --git a/crates/thing-tui/src/terminal.rs b/crates/thing-tui/src/terminal.rs index 50de37c..4d11024 100644 --- a/crates/thing-tui/src/terminal.rs +++ b/crates/thing-tui/src/terminal.rs @@ -214,12 +214,13 @@ fn write_restore(out: &mut W) -> io::Result<()> { update.and(paste).and(rest) } -/// Whether a panic on `here` gives back the terminal a guard owned by `owner` holds. Unwinding runs -/// the owner's `Drop` for a panic elsewhere, so only the owner's own panic needs the hook (its message -/// would otherwise print on the alternate screen). Where panics abort nothing unwinds to any `Drop`, -/// so every panic restores. -fn restores_here(abort: bool, owner: ThreadId, here: ThreadId) -> bool { - abort || owner == here +/// Whether a panic on `here` gives back the terminal a guard owned by `owner` holds. The panic that +/// ends the process always does: any thread where panics abort, and the main thread (whose panic +/// exits the process without running a guard that lives on another thread). A panic on the owner +/// restores so its message does not print on the alternate screen. Any other worker's panic unwinds +/// without touching the UI, and the owner's `Drop` runs in due course. +fn restores_here(abort: bool, is_main: bool, owner: ThreadId, here: ThreadId) -> bool { + abort || is_main || owner == here } /// Chain a hook ahead of the current one: a panic that `restores_here` gives the live guard's terminal @@ -229,7 +230,7 @@ fn install_panic_hook() { std::panic::set_hook(Box::new(move |info| { let live = active().clone(); if let Some(guard) = live - && restores_here(cfg!(panic = "abort"), guard.owner(), thread::current().id()) + && restores_here(cfg!(panic = "abort"), thread::current().name() == Some("main"), guard.owner(), thread::current().id()) { guard.restore(); } diff --git a/crates/thing-tui/src/terminal/guard_tests.rs b/crates/thing-tui/src/terminal/guard_tests.rs index 6c2cbff..e4093c7 100644 --- a/crates/thing-tui/src/terminal/guard_tests.rs +++ b/crates/thing-tui/src/terminal/guard_tests.rs @@ -318,15 +318,17 @@ fn an_error_mid_draw_does_not_leave_the_synchronized_update_open() { } #[test] -fn a_panic_restores_on_the_owners_thread_and_on_any_thread_when_nothing_unwinds() { +fn a_panic_restores_on_the_owners_thread_the_main_thread_and_any_thread_when_nothing_unwinds() { let here = std::thread::current().id(); let elsewhere = std::thread::spawn(|| std::thread::current().id()).join().unwrap(); - // Unwinding: the owner's panic restores, another thread's leaves the guard to its owner's Drop. - assert!(restores_here(false, here, here)); - assert!(!restores_here(false, here, elsewhere)); - // Aborting: no Drop will follow a panic anywhere, so every panic restores. - assert!(restores_here(true, here, here)); - assert!(restores_here(true, here, elsewhere)); + // Unwinding: the owner's panic restores; so does the main thread's, which ends the process + // without running a guard that lives on another thread; another worker's panic leaves the guard. + assert!(restores_here(false, false, here, here)); + assert!(restores_here(false, true, here, elsewhere)); + assert!(!restores_here(false, false, here, elsewhere)); + // Aborting: no Drop follows a panic anywhere, so every panic restores. + assert!(restores_here(true, false, here, elsewhere)); + assert!(restores_here(true, true, here, elsewhere)); } #[test]