refactor: add execution retries on windows

This commit is contained in:
zhom
2025-08-10 00:47:52 +04:00
parent 6cc27ec5b2
commit 999eb165ec
2 changed files with 69 additions and 17 deletions
+42 -14
View File
@@ -292,20 +292,48 @@ fn run_app(app_dir: &Path, args: &[String]) -> Result<()> {
let mut cmd_args = vec![main_script.clone()];
cmd_args.extend(args.iter().cloned());
// Execute Node.js application
let status = Command::new(&node_executable)
.args(&cmd_args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()
.with_context(|| format!(
"Failed to execute Node.js application\nExecutable: {}\nMain script: {}\nArgs: {:?}\nWorking directory: {}",
node_executable.display(),
main_script,
cmd_args,
app_path.display()
))?;
// Execute Node.js application with a few retries to tolerate transient Windows issues
let mut status = None;
let mut last_err: Option<anyhow::Error> = None;
let max_attempts: u32 = 8;
for attempt in 1..=max_attempts {
match Command::new(&node_executable)
.args(&cmd_args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()
{
Ok(s) => {
status = Some(s);
break;
}
Err(e) => {
last_err = Some(anyhow::anyhow!(e).context(format!(
"Failed to execute Node.js application (attempt {attempt}/{max_attempts})\nExecutable: {}\nMain script: {}\nArgs: {:?}\nWorking directory: {}",
node_executable.display(),
main_script,
cmd_args,
app_path.display()
)));
#[cfg(windows)]
{
use std::time::Duration;
std::thread::sleep(Duration::from_millis(50 * attempt as u64));
}
#[cfg(not(windows))]
{
if attempt >= 2 {
break;
}
}
}
}
}
let status = status.ok_or_else(|| last_err.unwrap_or_else(|| anyhow::anyhow!(
"Failed to execute Node.js application after {} attempts",
max_attempts
)))?;
std::process::exit(status.code().unwrap_or(1));
}
+27 -3
View File
@@ -675,7 +675,31 @@ impl BundlerTestHelper {
}
// Build command to run the executable.
let mut cmd = Command::new(executable_path);
// On Windows CI, concurrent CreateProcess on the same path may fail sporadically.
// Copy the binary to a unique temp dir to shorten the path and avoid races.
#[cfg(windows)]
let (exec_path_owned, _temp_guard) = {
let tmp = TempDir::new().context("Failed to create temp dir for executable copy")?;
let file_name = executable_path
.file_name()
.ok_or_else(|| anyhow::anyhow!(
"Executable path missing file name: {}",
executable_path.display()
))?;
let dest = tmp.path().join(file_name);
std::fs::copy(executable_path, &dest).with_context(|| {
format!(
"Failed to copy executable to temporary directory: {} -> {}",
executable_path.display(),
dest.display()
)
})?;
(dest, tmp)
};
#[cfg(not(windows))]
let exec_path_owned = executable_path.to_path_buf();
let mut cmd = Command::new(&exec_path_owned);
cmd.args(args);
@@ -685,14 +709,14 @@ impl BundlerTestHelper {
println!(
"Executing: {} with args: {:?}",
executable_path.display(),
exec_path_owned.display(),
args
);
let output = cmd.output().with_context(|| {
format!(
"Failed to execute command: {}\nArgs: {:?}\nEnv vars: {:?}\nWorking directory: {:?}",
executable_path.display(),
exec_path_owned.display(),
args,
env_vars,
std::env::current_dir().unwrap_or_else(|_| "<unknown>".into())