Skip to content

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

TypeScript

fix(daemon): type the Windows stop result and release a finished child on replacement too

Marcelito5ca0dd7f642d
diff --git a/apps/desktop/src-tauri/src/daemon.rs b/apps/desktop/src-tauri/src/daemon.rs
index 424c9d5..f4cbe77 100644
--- a/apps/desktop/src-tauri/src/daemon.rs
+++ b/apps/desktop/src-tauri/src/daemon.rs
@@ -377,6 +377,24 @@ pub enum RestartOutcome {
Failed(String),
}
+/// What a successful checked Windows stop left on the port.
+#[cfg(windows)]
+enum WindowsStop {
+ Released,
+ /// Ours is finished and released, and another assistant answers (the refusal).
+ PortHeld(String),
+}
+
+/// Only a stop that released ours while another assistant holds the port is `External`.
+#[cfg(windows)]
+fn windows_restart_stop(stop: Result<WindowsStop, String>) -> Result<(), RestartOutcome> {
+ match stop {
+ Ok(WindowsStop::Released) => Ok(()),
+ Ok(WindowsStop::PortHeld(_)) => Err(RestartOutcome::External),
+ Err(reason) => Err(RestartOutcome::Failed(reason)),
+ }
+}
+
#[cfg(windows)]
struct WindowsRecovery {
owner: windows::RecoveredLaunch,
@@ -550,7 +568,7 @@ impl Daemon {
// Stop callers need this attempt's reason, not an unrelated sticky admission refusal.
#[cfg(windows)]
- fn windows_stop_failure(&self, reason: String) -> Result<(), String> {
+ fn windows_stop_failure<T>(&self, reason: String) -> Result<T, String> {
self.windows_refuse(reason.clone());
Err(reason)
}
@@ -715,10 +733,8 @@ impl Daemon {
#[cfg(windows)]
{
if slot.is_some() {
- if let Err(reason) = self.windows_wait_port() {
- slot.as_mut().unwrap().retain_failure();
- return Err(self.windows_refuse(reason));
- }
+ // Checked shutdown succeeded: ours is finished, so a held port is another assistant's.
+ let port_held = self.windows_wait_port().err();
if let Some(channel) = channel_slot.as_ref() {
if let Err(error) = channel.close() {
return Err(self.windows_refuse(format!("cannot replace unfinished Windows channel: {error}")));
@@ -726,6 +742,7 @@ impl Daemon {
}
drop(channel_slot.take());
drop(slot.take());
+ if let Some(reason) = port_held { return Err(self.windows_refuse(reason)); }
}
// Direct spawn has the same fallible admission, under the same guard.
self.windows_recovery_admission(&slot)?;
@@ -865,6 +882,14 @@ impl Daemon {
/// Returns this checked stop's outcome independently of the admission refusal latch.
#[cfg(windows)]
pub fn stop(&self) -> Result<(), String> {
+ self.windows_stop().and_then(|stop| match stop {
+ WindowsStop::Released => Ok(()),
+ WindowsStop::PortHeld(reason) => Err(reason),
+ })
+ }
+
+ #[cfg(windows)]
+ fn windows_stop(&self) -> Result<WindowsStop, String> {
let mut child_slot = self.child.lock().unwrap();
let mut channel_slot = self.channel.lock().unwrap();
// Only failed-launch cleanup may close first: its listener may not exist.
@@ -908,8 +933,8 @@ impl Daemon {
drop(child_slot.take());
drop(channel_slot.take());
match port_held {
- Some(reason) => self.windows_stop_failure(reason),
- None => Ok(()),
+ Some(reason) => { self.windows_refuse(reason.clone()); Ok(WindowsStop::PortHeld(reason)) }
+ None => Ok(WindowsStop::Released),
}
}
@@ -921,11 +946,7 @@ impl Daemon {
#[cfg(unix)]
self.stop();
#[cfg(windows)]
- if let Err(reason) = self.stop() {
- // Stop finished what we owned, and another assistant answers on the port.
- if reason == foreign_daemon_message(self.port) { return RestartOutcome::External; }
- return RestartOutcome::Failed(reason);
- }
+ if let Err(outcome) = windows_restart_stop(self.windows_stop()) { return outcome; }
// A second admission pass, deliberately: stop released both guards before returning,
// so another thread may have installed or failed an owner since. These are the states
// the windows_*_admission checks refuse, read again under the same child -> channel order.
@@ -1269,6 +1290,16 @@ mod windows_ownership_tests {
drop(peer);
}
+ #[test]
+ fn only_a_port_held_after_our_stop_maps_restart_to_external() {
+ // A latched recovered-admission failure cannot be built here (RecoveredLaunch is private
+ // to windows.rs), so this pins the mapping: the foreign text alone is not External.
+ let foreign = foreign_daemon_message(1);
+ assert!(matches!(windows_restart_stop(Ok(WindowsStop::PortHeld(foreign.clone()))), Err(RestartOutcome::External)));
+ assert!(matches!(windows_restart_stop(Err(foreign.clone())), Err(RestartOutcome::Failed(reason)) if reason == foreign));
+ assert!(matches!(windows_restart_stop(Ok(WindowsStop::Released)), Ok(())));
+ }
+
#[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));