diff --git a/crates/automation/src/agents/link.rs b/crates/automation/src/agents/link.rs index f43e37f78..c24072574 100644 --- a/crates/automation/src/agents/link.rs +++ b/crates/automation/src/agents/link.rs @@ -123,8 +123,8 @@ pub struct AgentLink { applied: Option>, retry_at: Option, retry: Duration, - /// The token a failed start already read and checked, for its retries; cleared once a start - /// succeeds. On Windows the check starts `whoami` and PowerShell (`owner_only`), about 300 ms + /// The token a failed start already read and checked, for its retries with the same settings; + /// cleared once a start succeeds or the settings change. On Windows the check starts `whoami` and PowerShell (`owner_only`), about 300 ms /// on the UI thread, which every retry of a taken port would otherwise pay again. checked_token: Option, status: AgentLinkStatus, @@ -184,9 +184,15 @@ impl AgentLink { } let now = motion::now(); let retry_due = self.retry_at.is_some_and(|at| now >= at); - if self.applied.as_ref().is_some_and(|applied| applied.as_ref() == want) && !retry_due { + let unchanged = self.applied.as_ref().is_some_and(|applied| applied.as_ref() == want); + if unchanged && !retry_due { return update; } + // Only a retry with the same settings reuses the token its failed start checked: after a + // change (agents off and on again, another port) the file may hold another token by now. + if !unchanged { + self.checked_token = None; + } let before = (self.status.clone(), self.applied.clone().flatten()); self.applied = Some(want.cloned()); self.retry_at = None; diff --git a/crates/automation/tests/it/agents.rs b/crates/automation/tests/it/agents.rs index 81b2fe5bc..6cca4325a 100644 --- a/crates/automation/tests/it/agents.rs +++ b/crates/automation/tests/it/agents.rs @@ -376,6 +376,28 @@ fn a_taken_port_retry_reuses_the_checked_token() { assert!(matches!(link.status().problem, Some(LinkProblem::TokenFile(_))), "{:?}", link.status()); } +/// The checked token is kept only for retries with the same settings: turning agents off and on again reads the token +/// file again, so a server never listens with a token the file no longer holds. +#[trial] +fn a_settings_change_reads_the_token_file_again() { + let dir = temp_dir("link-token-settings"); + let host = host(); + let mut link = AgentLink::new(Some(dir.path().to_path_buf()), "x").retry_after(Duration::ZERO); + let taken = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = u32::from(taken.local_addr().unwrap().port()); + assert!(link.sync(Some(&on(port)), &host).requests.is_none()); + assert!(matches!(link.status().problem, Some(LinkProblem::Listen { .. })), "{:?}", link.status()); + + assert!(!link.sync(None, &host).stopped, "nothing was running"); + // While agents are off the token file changes; here it can't be read at all, which the next start must report. + std::fs::remove_file(dir.path().join(TOKEN_FILE)).unwrap(); + std::fs::create_dir(dir.path().join(TOKEN_FILE)).unwrap(); + drop(taken); + let back = link.sync(Some(&on(port)), &host); + assert!(back.requests.is_none(), "started with the token the file no longer holds"); + assert!(matches!(link.status().problem, Some(LinkProblem::TokenFile(_))), "{:?}", link.status()); +} + /// Dropping the link stops its server and removes its connection file. #[trial] fn dropping_the_link_removes_its_connection_file() {