cdp: flush cookies on SIGTERM in serve (#333)

serve --storage-dir saved cookies only on Ctrl-C (SIGINT). docker stop
and kill send SIGTERM, which was not caught, so the save_cookies() call
at shutdown never ran and cookies were lost on restart. Watch SIGTERM as
well as Ctrl-C on Unix.
This commit is contained in:
SGavrl committed 2026-06-29 10:48:20 +00:00
1 parent 2840b08b56
commit b63125faed
1 file changed
+27 -5
+27 -5
View File
@@ -366,11 +366,33 @@ async fn cdp_processor(
let mut deferred: std::collections::VecDeque<ServerMessage> =
std::collections::VecDeque::new();
// Subscribe to Ctrl-C once. The future is single-shot, so we break out of
// the outer loop when it fires and never poll it again. Without this the
// accept loop just exits and any cookies set during the session are lost
// before `BrowserContext::save_cookies()` runs.
let mut shutdown = Box::pin(tokio::signal::ctrl_c());
// Subscribe to graceful-shutdown signals once. The future is single-shot,
// so we break out of the outer loop when it fires and never poll it again.
// Without this the accept loop just exits and any cookies set during the
// session are lost before `BrowserContext::save_cookies()` runs. We watch
// SIGTERM as well as Ctrl-C (SIGINT) so `docker stop` / `kill`, which send
// SIGTERM, also flush cookies to the storage dir (issue #333).
let mut shutdown = Box::pin(async {
#[cfg(unix)]
{
use tokio::signal::unix::{signal, SignalKind};
match signal(SignalKind::terminate()) {
Ok(mut term) => {
tokio::select! {
_ = tokio::signal::ctrl_c() => {}
_ = term.recv() => {}
}
}
Err(_) => {
let _ = tokio::signal::ctrl_c().await;
}
}
}
#[cfg(not(unix))]
{
let _ = tokio::signal::ctrl_c().await;
}
});
loop {
// Drain any deferred messages from the previous interception window