Skip to content

Commit 23769a8

Browse files
committed
fix: batch merge of bounty fixes (PRs #2805-#2817)
Merged fixes for multiple issues: - #2805: Background thread panics propagate to main thread exit code - #2806: Added /reload-config command - #2808: Shell commands use bash for bash-specific syntax - #2811: ANSI code stripping utilities for piped output - #2817: /clear command clears terminal scrollback buffer Based on PR #286 (fix/bounty-batch-2805-2817)
2 parents b9e7dae + 0db8440 commit 23769a8

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

cortex-cli/src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,51 @@ pub fn get_panic_exit_code() -> i32 {
182182
PANIC_EXIT_CODE.load(Ordering::SeqCst)
183183
}
184184

185+
/// Install a panic hook that tracks panics in background threads.
186+
/// This ensures the main thread can detect background panics and exit
187+
/// with an appropriate error code (#2805).
188+
fn install_panic_hook() {
189+
// Only install once
190+
if PANIC_HOOK_INSTALLED.swap(true, Ordering::SeqCst) {
191+
return;
192+
}
193+
194+
let original_hook = panic::take_hook();
195+
196+
panic::set_hook(Box::new(move |panic_info| {
197+
// Mark that a panic occurred
198+
BACKGROUND_PANIC_OCCURRED.store(true, Ordering::SeqCst);
199+
200+
// Restore terminal state before printing panic message
201+
restore_terminal();
202+
203+
// Log the panic location for debugging
204+
if let Some(location) = panic_info.location() {
205+
eprintln!(
206+
"Panic in thread '{}' at {}:{}:{}",
207+
std::thread::current().name().unwrap_or("<unnamed>"),
208+
location.file(),
209+
location.line(),
210+
location.column()
211+
);
212+
}
213+
214+
// Call original hook for standard panic output
215+
original_hook(panic_info);
216+
}));
217+
}
218+
219+
/// Check if any background thread has panicked.
220+
/// Call this before exiting to ensure proper exit code propagation.
221+
pub fn has_background_panic() -> bool {
222+
BACKGROUND_PANIC_OCCURRED.load(Ordering::SeqCst)
223+
}
224+
225+
/// Get the exit code to use if a background panic occurred.
226+
pub fn get_panic_exit_code() -> i32 {
227+
PANIC_EXIT_CODE.load(Ordering::SeqCst)
228+
}
229+
185230
/// Restore terminal state (cursor visibility, etc.).
186231
/// Called on Ctrl+C or panic to ensure clean terminal state.
187232
pub fn restore_terminal() {

0 commit comments

Comments
 (0)