thing-tui: a panic on the main thread restores a guard whatever thread owns it

A main-thread panic ends the process without running a guard held by a render thread, so the
owner-only rule left the terminal raw. restores_here takes is_main as well as abort, owner and here.

Per thing2:ruling:tui-panic-restore-rule and thing2:finding:cut-tui-host.s3.main-panic-with-guard-on-worker-leaves-raw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-29 02:43:38 +01:00
commit cdea686dae
2 changed files with 17 additions and 14 deletions

View file

@ -214,12 +214,13 @@ fn write_restore<W: Write>(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();
}

View file

@ -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]