Skip to content

Commit ec8c28b

Browse files
committed
fix(agents): retry transient provider executable conflicts
Signed-off-by: Tee8z <[email protected]>
1 parent 90fb9dd commit ec8c28b

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ pub fn invoke_provider(
8888
cmd.current_dir(home);
8989
}
9090
crate::util::configure_no_window(&mut cmd);
91-
let mut child = cmd
92-
.stdin(std::process::Stdio::piped())
91+
cmd.stdin(std::process::Stdio::piped())
9392
.stdout(std::process::Stdio::piped())
94-
.stderr(std::process::Stdio::piped())
95-
.spawn()
93+
.stderr(std::process::Stdio::piped());
94+
let mut child = spawn_provider(&mut cmd)
9695
.map_err(|e| format!("failed to spawn {}: {e}", binary.display()))?;
9796

9897
// Write request and close stdin immediately so the provider sees EOF.
@@ -431,6 +430,21 @@ pub(crate) fn redact_env_values_in(
431430
redact_secrets_with(s, &values)
432431
}
433432

433+
fn spawn_provider(cmd: &mut std::process::Command) -> std::io::Result<std::process::Child> {
434+
// A concurrent fork can inherit the staging writer until that child execs,
435+
// even after this thread closes it. Retry only that failed exec: no provider
436+
// ran and no request was sent. Persistent writers still fail after 100 ms.
437+
for _ in 0..10 {
438+
match cmd.spawn() {
439+
Err(error) if error.kind() == std::io::ErrorKind::ExecutableFileBusy => {
440+
std::thread::sleep(Duration::from_millis(10));
441+
}
442+
result => return result,
443+
}
444+
}
445+
cmd.spawn()
446+
}
447+
434448
/// Copy a resolved provider into a private staging directory while hashing
435449
/// exactly the bytes copied. The staged file becomes non-writable before either
436450
/// invocation, closing the path replacement and in-place rewrite races.

‎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() {

0 commit comments

Comments
 (0)