Skip to content

Commit 67e4d4c

Browse files
committed
fix(agents): retry transient executable conflicts
A concurrent fork can briefly inherit an executable's writable descriptor before exec closes it. Provider launches and adapter version probes then fail with ExecutableFileBusy even after the staging writer closes. Share the bounded launch retry between provider execution and version probes. Retry only ExecutableFileBusy, at most ten times at 10 ms intervals. Keep probe retries within the existing five-second deadline. Exercise transient and persistent writers, other launch errors, and the real version-probe path with a script held open for writing. Signed-off-by: Tee8z <[email protected]>
1 parent 90fb9dd commit 67e4d4c

6 files changed

Lines changed: 131 additions & 9 deletions

File tree

‎desktop/src-tauri/src/managed_agents/backend.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::discovery::{command_search::command_discovery_dirs, resolve_command};
2+
use super::process_spawn::spawn_with_busy_retry as spawn_provider;
23
use sha2::{Digest, Sha256};
34
use std::io::{BufReader, Read, Write};
45
use std::path::{Path, PathBuf};
@@ -88,11 +89,10 @@ pub fn invoke_provider(
8889
cmd.current_dir(home);
8990
}
9091
crate::util::configure_no_window(&mut cmd);
91-
let mut child = cmd
92-
.stdin(std::process::Stdio::piped())
92+
cmd.stdin(std::process::Stdio::piped())
9393
.stdout(std::process::Stdio::piped())
94-
.stderr(std::process::Stdio::piped())
95-
.spawn()
94+
.stderr(std::process::Stdio::piped());
95+
let mut child = spawn_provider(&mut cmd)
9696
.map_err(|e| format!("failed to spawn {}: {e}", binary.display()))?;
9797

9898
// Write request and close stdin immediately so the provider sees EOF.

‎desktop/src-tauri/src/managed_agents/backend_tests.rs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,59 @@ fn write_test_provider(path: &Path, body: &str) {
142142
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700)).unwrap();
143143
}
144144

145+
#[cfg(target_os = "linux")]
146+
#[test]
147+
fn provider_spawn_waits_for_a_transient_writer() {
148+
let directory = tempfile::tempdir().unwrap();
149+
let provider = directory.path().join("provider");
150+
write_test_provider(&provider, "exit 0");
151+
let writer = std::fs::OpenOptions::new()
152+
.write(true)
153+
.open(&provider)
154+
.unwrap();
155+
assert_eq!(
156+
std::process::Command::new(&provider)
157+
.spawn()
158+
.unwrap_err()
159+
.kind(),
160+
std::io::ErrorKind::ExecutableFileBusy,
161+
);
162+
let closer = std::thread::spawn(move || {
163+
std::thread::sleep(Duration::from_millis(20));
164+
drop(writer);
165+
});
166+
167+
let mut child = spawn_provider(&mut std::process::Command::new(&provider))
168+
.expect("launch after the inherited writer closes");
169+
assert!(child.wait().unwrap().success());
170+
closer.join().unwrap();
171+
}
172+
173+
#[cfg(target_os = "linux")]
174+
#[test]
175+
fn provider_spawn_returns_persistent_write_conflicts() {
176+
let directory = tempfile::tempdir().unwrap();
177+
let provider = directory.path().join("provider");
178+
write_test_provider(&provider, "exit 0");
179+
let _writer = std::fs::OpenOptions::new()
180+
.write(true)
181+
.open(&provider)
182+
.unwrap();
183+
184+
let error = spawn_provider(&mut std::process::Command::new(&provider)).unwrap_err();
185+
assert_eq!(error.kind(), std::io::ErrorKind::ExecutableFileBusy);
186+
}
187+
188+
#[test]
189+
fn provider_spawn_preserves_other_launch_errors() {
190+
let directory = tempfile::tempdir().unwrap();
191+
let error = spawn_provider(&mut std::process::Command::new(
192+
directory.path().join("missing"),
193+
))
194+
.unwrap_err();
195+
assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
196+
}
197+
145198
#[cfg(unix)]
146199
#[test]
147200
fn provider_deploy_negotiates_and_deploys_the_same_staged_bytes() {

‎desktop/src-tauri/src/managed_agents/discovery.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -840,14 +840,13 @@ pub(crate) fn probe_codex_acp_version_with_path(
840840
command.env("PATH", path);
841841
}
842842
crate::util::configure_no_window(&mut command);
843-
let mut child = command
843+
let deadline = Instant::now() + VERSION_PROBE_TIMEOUT;
844+
command
844845
.stdout(tmp.try_clone().ok()?)
845-
.stderr(std::process::Stdio::null())
846-
.spawn()
847-
.ok()?;
846+
.stderr(std::process::Stdio::null());
847+
let mut child = super::process_spawn::spawn_with_busy_retry(&mut command).ok()?;
848848

849849
// Poll until the deadline rather than blocking on stdout EOF.
850-
let deadline = Instant::now() + VERSION_PROBE_TIMEOUT;
851850
let exit_status = loop {
852851
match child.try_wait() {
853852
Ok(Some(status)) => break status,

‎desktop/src-tauri/src/managed_agents/discovery/tests/codex_version.rs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,56 @@ fn codex_adapter_availability_outdated_for_older_1x_binary() {
7373
assert!(codex_adapter_is_outdated(&bin));
7474
}
7575
}
76+
77+
#[cfg(target_os = "linux")]
78+
#[test]
79+
fn probe_codex_acp_version_waits_for_a_transient_executable_writer() {
80+
use std::os::unix::fs::PermissionsExt;
81+
let temp = tempfile::tempdir().expect("temp dir");
82+
let binary = temp.path().join("codex-acp");
83+
std::fs::write(
84+
&binary,
85+
"#!/bin/sh\necho '@agentclientprotocol/codex-acp 1.1.2'\n",
86+
)
87+
.expect("write adapter");
88+
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755))
89+
.expect("chmod adapter");
90+
let writer = std::fs::OpenOptions::new()
91+
.write(true)
92+
.open(&binary)
93+
.unwrap();
94+
assert_eq!(
95+
std::process::Command::new(&binary)
96+
.spawn()
97+
.unwrap_err()
98+
.kind(),
99+
std::io::ErrorKind::ExecutableFileBusy
100+
);
101+
let closer = std::thread::spawn(move || {
102+
std::thread::sleep(std::time::Duration::from_millis(20));
103+
drop(writer);
104+
});
105+
let version = probe_codex_acp_version_with_path(&binary, None);
106+
closer.join().unwrap();
107+
assert_eq!(version, Some((1, 1, 2)));
108+
}
109+
110+
#[cfg(target_os = "linux")]
111+
#[test]
112+
fn probe_codex_acp_version_rejects_a_persistent_executable_writer() {
113+
use std::os::unix::fs::PermissionsExt;
114+
let temp = tempfile::tempdir().expect("temp dir");
115+
let binary = temp.path().join("codex-acp");
116+
std::fs::write(
117+
&binary,
118+
"#!/bin/sh\necho '@agentclientprotocol/codex-acp 1.1.2'\n",
119+
)
120+
.expect("write adapter");
121+
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o755))
122+
.expect("chmod adapter");
123+
let _writer = std::fs::OpenOptions::new()
124+
.write(true)
125+
.open(&binary)
126+
.unwrap();
127+
assert_eq!(probe_codex_acp_version_with_path(&binary, None), None);
128+
}

‎desktop/src-tauri/src/managed_agents/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub(crate) mod persona_events;
3232
mod personas;
3333
#[cfg(windows)]
3434
mod process_lifecycle;
35+
mod process_spawn;
3536
pub(crate) mod readiness;
3637
pub(crate) mod reconcile;
3738
mod relay_mesh;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub(super) fn spawn_with_busy_retry(
2+
cmd: &mut std::process::Command,
3+
) -> std::io::Result<std::process::Child> {
4+
// A concurrent fork can inherit the staging writer until that child execs,
5+
// even after this thread closes it. Retry only that failed exec: the command
6+
// never started. Persistent writers still fail after 100 ms.
7+
for _ in 0..10 {
8+
match cmd.spawn() {
9+
Err(error) if error.kind() == std::io::ErrorKind::ExecutableFileBusy => {
10+
std::thread::sleep(std::time::Duration::from_millis(10));
11+
}
12+
result => return result,
13+
}
14+
}
15+
cmd.spawn()
16+
}

0 commit comments

Comments
 (0)