fix(desktop): stop the sidecar with SIGTERM first, and free a stale WhatsApp profile (UF-046)
On macOS and Linux the shell stopped the sidecar with Child::kill(), which is SIGKILL, so the sidecar never ran its shutdown: the processes it started (MCP servers, a managed Ollama, the WhatsApp browser) kept running. After an app restart the orphaned WhatsApp browser still held the session profile, and the channel failed with "The browser is already running". The shell now sends SIGTERM, waits up to 5 seconds for the sidecar to exit, then sends SIGKILL; Windows keeps its kill-on-close job. For crashes, where no shutdown runs, the WhatsApp channel stops a browser left over from an earlier run before starting its own. It only stops a process whose command line uses this profile and whose parent is gone; a browser another running Waggle server owns is left alone, and the channel says so instead of retrying.
This commit is contained in:
@@ -17,6 +17,8 @@ use uuid::Uuid;
|
||||
|
||||
const WATCHDOG_FAILURE_THRESHOLD: u32 = 3;
|
||||
const WATCHDOG_INITIAL_STARTUP_GRACE: Duration = Duration::from_secs(600);
|
||||
/// How long a stopping sidecar gets to shut down on its own before SIGKILL.
|
||||
const SERVICE_STOP_GRACE: Duration = Duration::from_secs(5);
|
||||
const READY_RECORD_MAX_BYTES: u64 = 16 * 1024;
|
||||
#[cfg(windows)]
|
||||
const CREATE_NO_WINDOW: u32 = 0x08000000;
|
||||
@@ -974,8 +976,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn service_script_resolves_from_a_macos_app_bundle() {
|
||||
let root =
|
||||
std::env::temp_dir().join(format!("waggle-app-bundle-{}", std::process::id()));
|
||||
let root = std::env::temp_dir().join(format!("waggle-app-bundle-{}", std::process::id()));
|
||||
let macos = root.join("Waggle.app").join("Contents").join("MacOS");
|
||||
let resources = root
|
||||
.join("Waggle.app")
|
||||
@@ -1190,6 +1191,43 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(root);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn stopping_the_service_lets_it_exit_on_sigterm() {
|
||||
let mut child = Command::new("sh")
|
||||
.args(["-c", "trap 'exit 0' TERM; while :; do sleep 0.05; done"])
|
||||
.spawn()
|
||||
.expect("starts service child");
|
||||
// Let the shell install its trap before the signal arrives.
|
||||
thread::sleep(Duration::from_millis(200));
|
||||
|
||||
let status =
|
||||
stop_service_child(&mut child, Duration::from_secs(5)).expect("reaps service child");
|
||||
|
||||
assert!(
|
||||
status.success(),
|
||||
"expected a clean exit on SIGTERM, got {status:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn stopping_the_service_kills_it_when_sigterm_is_ignored() {
|
||||
use std::os::unix::process::ExitStatusExt;
|
||||
let mut child = Command::new("sh")
|
||||
.args(["-c", "trap '' TERM; while :; do sleep 0.05; done"])
|
||||
.spawn()
|
||||
.expect("starts service child");
|
||||
thread::sleep(Duration::from_millis(200));
|
||||
let started = Instant::now();
|
||||
|
||||
let status = stop_service_child(&mut child, Duration::from_millis(300))
|
||||
.expect("reaps service child");
|
||||
|
||||
assert_eq!(status.signal(), Some(libc::SIGKILL));
|
||||
assert!(started.elapsed() < Duration::from_secs(3));
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn closing_the_service_job_terminates_its_child() {
|
||||
@@ -1595,12 +1633,35 @@ fn spawn_managed_service_with(
|
||||
spawn_locked(&mut runtime, state.preferred_port, build_command)
|
||||
}
|
||||
|
||||
/// Stops a sidecar process. On Unix it first sends SIGTERM, so the service closes
|
||||
/// what it started (the WhatsApp browser, MCP servers, a managed Ollama) before
|
||||
/// it exits, and gets SIGKILL only if it is still running after `grace`. Windows
|
||||
/// has no SIGTERM; there the kill-on-close job ends the whole process tree.
|
||||
fn stop_service_child(child: &mut Child, grace: Duration) -> Option<std::process::ExitStatus> {
|
||||
#[cfg(unix)]
|
||||
if let (Ok(None), Ok(pid)) = (child.try_wait(), libc::pid_t::try_from(child.id())) {
|
||||
// SAFETY: kill(2) on the pid of a child this process has not reaped yet.
|
||||
unsafe { libc::kill(pid, libc::SIGTERM) };
|
||||
let deadline = Instant::now() + grace;
|
||||
while Instant::now() < deadline {
|
||||
match child.try_wait() {
|
||||
Ok(Some(status)) => return Some(status),
|
||||
Ok(None) => std::thread::sleep(Duration::from_millis(50)),
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
let _ = grace;
|
||||
let _ = child.kill();
|
||||
child.wait().ok()
|
||||
}
|
||||
|
||||
pub(crate) fn stop_service_sync(state: &ServiceState) -> Result<(), String> {
|
||||
let mut runtime = state.runtime.lock().map_err(|error| error.to_string())?;
|
||||
runtime.desired_running = false;
|
||||
if let Some(mut launch) = runtime.active.take() {
|
||||
let _ = launch.child.kill();
|
||||
let _ = launch.child.wait();
|
||||
let _ = stop_service_child(&mut launch.child, SERVICE_STOP_GRACE);
|
||||
remove_launch_ready_file(&launch.config.ready_path);
|
||||
}
|
||||
Ok(())
|
||||
@@ -1662,8 +1723,7 @@ fn recover_service_with(
|
||||
}
|
||||
|
||||
if let Some(mut launch) = runtime.active.take() {
|
||||
let _ = launch.child.kill();
|
||||
let _ = launch.child.wait();
|
||||
let _ = stop_service_child(&mut launch.child, SERVICE_STOP_GRACE);
|
||||
remove_launch_ready_file(&launch.config.ready_path);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user