Skip to main content

install_panic_hook

Function install_panic_hook 

Source
pub fn install_panic_hook()
Expand description

Installs a global panic hook that reports any panic on any thread, then calls whatever hook was previously installed (Rust’s own default, which prints to stderr, unless something else already replaced it): never changing panic behavior itself, the same “report, then don’t change program behavior” rule the .NET middleware and Python excepthook wrapper both follow.

Unlike Go, where only a defer Recover() in the same goroutine can see a panic, Rust’s panic hook is genuinely process-wide: it fires for a panic on any thread, including a web framework’s own worker threads, with no per-framework middleware needed at all. init() calls this automatically unless Configuration.install_panic_hook is set to false; call it directly only if you’re managing configuration some other way.

Always chains onto whatever hook is currently installed via take_hook(), rather than latching “already installed” after the first call: deliberately, even though that means calling this more than once wraps another reporting layer each time (a real panic would then report once per accumulated layer). A one-shot latch was tried first and rejected: it makes this call a silent no-op the moment anything else calls std::panic::set_hook after this one runs (a host app installing its own hook after init(), say), discarding this crate’s reporting entirely with no error or warning. Duplicate reports from calling this redundantly is a far more visible, far less damaging failure mode than reporting silently going dark, and is easily avoided the same way init() already asks to be called: once, at startup.