Make shutdown event-driven and address review findings

Replace the non-blocking accept + 50ms poll with a plain blocking
accept(). A signal-hook thread waits for SIGTERM/SIGINT, sets a flag,
and wakes accept() with a throwaway connection to the socket.

On shutdown the listener is closed and the socket file unlinked before
the grace period, so clients that connect late fail fast with ENOENT
instead of queueing and being reset. In-flight handlers are tracked by
a counter + Condvar rather than a Vec of JoinHandles.

Accepted streams now get a 5s read/write timeout so a silent client
cannot pin a handler thread or stretch every shutdown to the full
grace period. Transient accept() errors are logged and retried instead
of exiting the daemon. A second SIGTERM/SIGINT during the grace period
forces an immediate exit with status 1.

Tests: wrap the child in the kill-on-drop guard immediately after
spawn, drop the redundant wall-clock assertion, and add a case for a
client that connects but never sends.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kmrgkpp9YBJ6AMaWYPPZuD
This commit is contained in:
2026-09-12 13:19:36 -07:00
co-authored by Claude Fable 5.1
parent 08aace5428
commit 1467b71037
4 changed files with 174 additions and 90 deletions
+24 -11
View File
@@ -6,6 +6,10 @@ use std::path::PathBuf;
use std::process::{Child, Command};
use std::time::{Duration, Instant};
/// Idle shutdown takes milliseconds; anything approaching the daemon's 3s
/// grace period means it is waiting on something it should not be.
const EXIT_TIMEOUT: Duration = Duration::from_secs(2);
const PASSWORD_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../test_passwords");
fn temp_socket_path(tag: &str) -> PathBuf {
@@ -41,13 +45,15 @@ fn spawn_daemon(socket: &PathBuf) -> Daemon {
.arg(PASSWORD_DIR)
.spawn()
.expect("failed to spawn saslauthd");
// Wrap immediately so a failed startup below still kills the process.
let daemon = Daemon(child);
let deadline = Instant::now() + Duration::from_secs(5);
while !is_socket(socket) {
assert!(Instant::now() < deadline, "daemon never created {socket:?}");
std::thread::sleep(Duration::from_millis(10));
}
Daemon(child)
daemon
}
fn send_signal(daemon: &Daemon, signal: &str) {
@@ -68,8 +74,6 @@ fn wait_for_exit(daemon: &mut Daemon, timeout: Duration) -> std::process::ExitSt
return status;
}
if Instant::now() >= deadline {
let _ = child.kill();
let _ = child.wait();
panic!("daemon did not exit within {timeout:?}");
}
std::thread::sleep(Duration::from_millis(10));
@@ -92,16 +96,10 @@ fn assert_clean_shutdown(signal: &str) {
assert_eq!(authenticate(&socket, "some-rad-password"), "OK");
assert_eq!(authenticate(&socket, "wrong-password"), "NO");
let started = Instant::now();
send_signal(&daemon, signal);
let status = wait_for_exit(&mut daemon, Duration::from_secs(1));
let status = wait_for_exit(&mut daemon, EXIT_TIMEOUT);
assert!(status.success(), "expected exit status 0 on {signal}, got {status}");
assert!(
started.elapsed() < Duration::from_secs(1),
"shutdown took {:?}",
started.elapsed()
);
assert!(!socket.exists(), "socket file {socket:?} was left behind");
}
@@ -124,7 +122,22 @@ fn it_recreates_a_stale_socket_on_startup() {
assert_eq!(authenticate(&socket, "some-rad-password"), "OK");
send_signal(&daemon, "TERM");
let status = wait_for_exit(&mut daemon, Duration::from_secs(1));
let status = wait_for_exit(&mut daemon, EXIT_TIMEOUT);
assert!(status.success());
assert!(!socket.exists());
}
#[test]
fn it_does_not_wait_forever_for_a_silent_client() {
let socket = temp_socket_path("silent");
let mut daemon = spawn_daemon(&socket);
// Connect but never send a request, leaving a handler blocked on read.
let _silent = UnixStream::connect(&socket).expect("connect to daemon");
send_signal(&daemon, "TERM");
// Must finish once the grace period (3s) expires, whatever the client does.
let status = wait_for_exit(&mut daemon, Duration::from_secs(5));
assert!(status.success());
assert!(!socket.exists());
}