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:
1 parent
2840b08b56
commit
b63125faed
1 file changed
+27
-5
@@ -366,11 +366,33 @@ async fn cdp_processor(
|
|||||||
let mut deferred: std::collections::VecDeque<ServerMessage> =
|
let mut deferred: std::collections::VecDeque<ServerMessage> =
|
||||||
std::collections::VecDeque::new();
|
std::collections::VecDeque::new();
|
||||||
|
|
||||||
// Subscribe to Ctrl-C once. The future is single-shot, so we break out of
|
// Subscribe to graceful-shutdown signals once. The future is single-shot,
|
||||||
// the outer loop when it fires and never poll it again. Without this the
|
// so we break out of the outer loop when it fires and never poll it again.
|
||||||
// accept loop just exits and any cookies set during the session are lost
|
// Without this the accept loop just exits and any cookies set during the
|
||||||
// before `BrowserContext::save_cookies()` runs.
|
// session are lost before `BrowserContext::save_cookies()` runs. We watch
|
||||||
let mut shutdown = Box::pin(tokio::signal::ctrl_c());
|
// 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 {
|
loop {
|
||||||
// Drain any deferred messages from the previous interception window
|
// Drain any deferred messages from the previous interception window
|
||||||
|
|||||||
Reference in new issue
Block a user