fix(tui/panic): restore terminal and exit on any thread panic to prevent broken raw mode/alt-screen state on Windows and heavy-load errors\n\nThis ensures we don’t leave bracketed focus/mouse reporting enabled or raw mode active when a background thread panics (e.g., during rate limits or multi-agent heavy output).\n\nCloses #80

This commit is contained in:
James Peter
2025-09-08 12:47:34 +10:00
parent 60f9db8ca5
commit 34ffe467d8
+18 -3
View File
@@ -177,10 +177,25 @@ fn query_terminal_info() -> TerminalInfo {
}
fn set_panic_hook() {
let hook = std::panic::take_hook();
// Chain to any previously installed hook so users still get rich reports.
let prev = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
let _ = restore(); // ignore any errors as we are already failing
hook(panic_info);
// Always attempt to restore the terminal state before printing the panic.
// This is crucial on Windows and when background threads panic — otherwise
// raw mode, mouse/focus reporting, and the alt screen can be left enabled,
// causing sequences like "[A", "[B", or "[I" to appear and making Ctrl+C
// ineffective. Ignore any restore error as we're already failing.
let _ = restore();
// Delegate to the previous hook (color-eyre or default) to render details.
prev(panic_info);
// Ensure the process terminates. Without exiting here, a panic in a
// background thread (e.g., streaming/agent worker) would leave the main
// UI thread running after we've torn down the terminal, which manifests
// as the "CLI bugs out" behavior described in issue #80.
// Exiting avoids that half‑alive state and returns control to the shell.
std::process::exit(1);
}));
}