Skip to content

Commit 88a4af2

Browse files
authored
pdg: use mutex around cargo runs to avoid file race conditions (#1380)
* Fixes #1365.
2 parents 67cfbc5 + 9a554cb commit 88a4af2

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pdg/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ mod tests {
203203
fmt::Display,
204204
path::{Path, PathBuf},
205205
process::Command,
206+
sync::Mutex,
206207
};
207208

208209
use c2rust_analysis_rt::runtime::backend::BackendKind;
@@ -316,6 +317,14 @@ mod tests {
316317
let metadata_path = exe_dir.join("metadata.bc");
317318
let event_log_path = exe_dir.join("event.log.bc");
318319

320+
/// There can be a race condition in the `cargo run` calls.
321+
/// The `cargo build`s synchronize already, but after building,
322+
/// a second `cargo build` can temporarily move/delete the binary
323+
/// while the first `cargo run` tries to run it.
324+
/// So just use a `Mutex` around the whole thing.
325+
static CARGO_RUN_C2RUST_INSTRUMENT: Mutex<()> = Mutex::new(());
326+
327+
let guard = CARGO_RUN_C2RUST_INSTRUMENT.lock().unwrap();
319328
let mut cmd = Command::new("cargo");
320329
cmd.current_dir(repo_dir()?)
321330
.args(&[
@@ -344,6 +353,7 @@ mod tests {
344353
.env("INSTRUMENT_OUTPUT_APPEND", "false");
345354
let status = cmd.status()?;
346355
ensure!(status.success(), eyre!("{cmd:?} failed: {status}"));
356+
drop(guard);
347357

348358
let pdg = Pdg::new(&metadata_path, &event_log_path)?;
349359
pdg.graphs.assert_all_tests();

0 commit comments

Comments
 (0)