Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

test(daemon): RED stopped child released when another assistant holds the port

Marcelito5ca161e50106
diff --git a/apps/desktop/src-tauri/src/daemon.rs b/apps/desktop/src-tauri/src/daemon.rs
index a9143a5..3c697c0 100644
--- a/apps/desktop/src-tauri/src/daemon.rs
+++ b/apps/desktop/src-tauri/src/daemon.rs
@@ -1161,6 +1161,62 @@ mod windows_ownership_tests {
drop(peer);
}
+ // Its checked shutdown succeeds; a retained failure is observable as a failed launch.
+ struct StoppedChild { calls: Arc<AtomicUsize>, failed: bool }
+ impl ChildProcess for StoppedChild {
+ fn id(&self) -> u32 { 4 }
+ fn launch_failed(&self) -> bool { self.failed }
+ fn retain_failure(&mut self) { self.failed = true; }
+ fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> { Ok(None) }
+ fn kill(&mut self) -> io::Result<()> { panic!("synthetic stopped child") }
+ fn wait(&mut self) -> io::Result<ExitStatus> { panic!("synthetic stopped child") }
+ fn shutdown_owned(&mut self, _: Duration) -> io::Result<()> {
+ self.calls.fetch_add(1, Ordering::SeqCst);
+ Ok(())
+ }
+ }
+
+ #[test]
+ fn stopped_child_is_released_not_retained_when_another_assistant_holds_the_port() {
+ let foreign = Arc::new(std::sync::atomic::AtomicBool::new(true));
+ let mut daemon = Daemon::new(1, PathBuf::new(), None).with_channel(Arc::new(RefusingHandler));
+ daemon.health = Some(Arc::new({ let foreign = foreign.clone(); move || foreign.load(Ordering::SeqCst) }));
+ let calls = Arc::new(AtomicUsize::new(0));
+ *daemon.child.lock().unwrap() = Some(Box::new(StoppedChild { calls: calls.clone(), failed: false }));
+ let (endpoint, peer) = channel_pair().unwrap();
+ let channel = AppChannel::start(endpoint, Arc::new(RefusingHandler)).unwrap();
+ *daemon.channel.lock().unwrap() = Some(channel.clone());
+ assert_eq!(daemon.stop(), Err(foreign_daemon_message(1)));
+ assert_eq!(calls.load(Ordering::SeqCst), 1);
+ assert!(daemon.child.lock().unwrap().is_none(), "a stopped child is finished, not a failed launch");
+ assert!(daemon.channel.lock().unwrap().is_none());
+ assert!(!channel.is_open());
+ assert_eq!(daemon.refusal(), Some(foreign_daemon_message(1)));
+ // While the other assistant runs, restart reports it as such, and never stops ours again.
+ assert!(matches!(daemon.restart(Duration::ZERO), RestartOutcome::External));
+ assert_eq!(daemon.ensure_running(Duration::ZERO), Err(foreign_daemon_message(1)));
+ foreign.store(false, Ordering::SeqCst);
+ assert_eq!(daemon.stop(), Ok(()));
+ assert_eq!(calls.load(Ordering::SeqCst), 1);
+ drop(peer);
+ }
+
+ #[test]
+ fn restart_reports_another_assistant_that_took_the_port_after_ours_stopped() {
+ let mut daemon = Daemon::new(1, PathBuf::new(), None).with_channel(Arc::new(RefusingHandler));
+ daemon.health = Some(Arc::new(|| true));
+ let calls = Arc::new(AtomicUsize::new(0));
+ *daemon.child.lock().unwrap() = Some(Box::new(StoppedChild { calls: calls.clone(), failed: false }));
+ let (endpoint, peer) = channel_pair().unwrap();
+ *daemon.channel.lock().unwrap() = Some(AppChannel::start(endpoint, Arc::new(RefusingHandler)).unwrap());
+ assert!(matches!(daemon.restart(Duration::ZERO), RestartOutcome::External));
+ assert_eq!(calls.load(Ordering::SeqCst), 1);
+ assert!(daemon.child.lock().unwrap().is_none());
+ assert!(daemon.channel.lock().unwrap().is_none());
+ assert_eq!(daemon.refusal(), Some(foreign_daemon_message(1)));
+ drop(peer);
+ }
+
#[test]
fn exited_root_replacement_retains_both_owners_when_old_channel_close_fails() {
let mut daemon = Daemon::new(1, PathBuf::new(), None).with_channel(Arc::new(RefusingHandler));