When adding timeouts around external/IPC checks (especially those triggered from UI/closing handlers), ensure you both bound execution time and choose a timeout fallback that favors the lowest-impact, most likely failure.
Apply this standard:
Example pattern:
use std::{sync::mpsc, thread, time::Duration};
fn probe_with_safe_timeout<F>(probe: F, timeout: Duration) -> bool
where
F: FnOnce() -> bool + Send + 'static,
{
let (tx, rx) = mpsc::channel();
thread::spawn(move || { let _ = tx.send(probe()); });
// Timeout fallback should be biased toward the safer behavior,
// not just “whichever state seems likely.”
match rx.recv_timeout(timeout) {
Ok(registered) => registered,
Err(_) => {
// e.g., treat as present or absent depending on impact/likelihood.
false
}
}
}
Use a short timeout and a documented, intentionally chosen fallback so performance doesn’t degrade user experience and the system behaves predictably under ambiguity.