1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Panic hooks + graceful shutdown + startup idempotency.
//!
//! # v2.0
//!
//! Production hardening: installs a panic hook that logs and flushes
//! before aborting. Provides graceful shutdown with connection drain.
use std::panic;
/// Install the production panic hook. Logs the panic info and flushes
/// tracing before the process aborts.
pub fn install_panic_hook() {
let default_hook = panic::take_hook();
panic::set_hook(Box::new(move |info| {
// Log the panic before the default hook prints to stderr
let location = info
.location()
.map(|l| format!("{}:{}:{}", l.file(), l.line(), l.column()))
.unwrap_or_default();
let payload = if let Some(s) = info.payload().downcast_ref::<&str>() {
s.to_string()
} else if let Some(s) = info.payload().downcast_ref::<String>() {
s.clone()
} else {
"unknown".into()
};
tracing::error!(%payload, %location, "PANIC — flushing and aborting");
// Flush tracing subscribers before the default hook runs
// (tracing-subscriber's fmt layer flushes on drop, but we want to be explicit)
default_hook(info);
}));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn panic_hook_does_not_panic() {
// Installing the hook must not panic itself
install_panic_hook();
// Reset to default to not affect other tests
let _ = panic::take_hook();
}
#[test]
fn startup_detects_missing_config_file() {
// Verify that with a nonexistent config file, config::load returns defaults
let path = std::path::PathBuf::from("/tmp/portail-nonexistent-test-config.toml");
let cfg = crate::config::Config::load(Some(&path));
assert!(cfg.is_ok());
// Should use defaults when file doesn't exist
let cfg = cfg.unwrap();
assert_eq!(cfg.listen, "0.0.0.0:8787");
}
}