proxy/parallel_handler: Improved panic errors with formatted strings

* Improved errors when panics occur and the panic message is a
formatted (not static) string. This worked already for &str literals,
but not for Strings.

Downcasting to both &str and String is also done by the Rust Standard
Library in the default panic handler. See:
b605c65b6e/library/std/src/panicking.rs (L777)

* Switched from eprintln! to tracing::error when logging panics in the
task scheduler.

Signed-off-by: Laurențiu Leahu-Vlăducu <l.leahu-vladucu@proxmox.com>
This commit is contained in:
Laurențiu Leahu-Vlăducu 2025-01-24 16:29:09 +01:00 committed by Thomas Lamprecht
parent d4468ba6f8
commit 1f24167b4d
2 changed files with 15 additions and 10 deletions

View File

@ -422,11 +422,16 @@ async fn run_task_scheduler() {
tokio::time::sleep_until(tokio::time::Instant::from_std(delay_target)).await;
match schedule_tasks().catch_unwind().await {
Err(panic) => match panic.downcast::<&str>() {
Ok(msg) => eprintln!("task scheduler panic: {msg}"),
Err(_) => eprintln!("task scheduler panic - unknown type"),
Err(panic) => {
if let Some(msg) = panic.downcast_ref::<&str>() {
tracing::error!("task scheduler panic: {msg}");
} else if let Some(msg) = panic.downcast_ref::<String>() {
tracing::error!("task scheduler panic: {msg}");
} else {
tracing::error!("task scheduler panic - cannot show error message due to unknown error type")
}
},
Ok(Err(err)) => eprintln!("task scheduler failed - {err:?}"),
Ok(Err(err)) => tracing::error!("task scheduler failed - {err:?}"),
Ok(Ok(_)) => {}
}
}

View File

@ -135,12 +135,12 @@ impl<I: Send + 'static> ParallelHandler<I> {
let mut i = 0;
while let Some(handle) = self.handles.pop() {
if let Err(panic) = handle.join() {
match panic.downcast::<&str>() {
Ok(panic_msg) => msg_list.push(format!(
"thread {} ({}) panicked: {}",
self.name, i, panic_msg
)),
Err(_) => msg_list.push(format!("thread {} ({}) panicked", self.name, i)),
if let Some(panic_msg) = panic.downcast_ref::<&str>() {
msg_list.push(format!("thread {} ({i}) panicked: {panic_msg}", self.name));
} else if let Some(panic_msg) = panic.downcast_ref::<String>() {
msg_list.push(format!("thread {} ({i}) panicked: {panic_msg}", self.name));
} else {
msg_list.push(format!("thread {} ({i}) panicked", self.name));
}
}
i += 1;