Skip to content

Commit

Permalink
Use wait() instead of wait_with_output()
Browse files Browse the repository at this point in the history
The latter consumes the `Child`, which is normally a good thing, but it
will interfere with the drop guard, which needs to own the child. Hence
a reference is better in this case.
  • Loading branch information
jfrimmel committed Sep 20, 2024
1 parent f9cdee4 commit 5d6d39f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/valgrind/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where
valgrind.args(additional_args.split(' '));
}

let cargo = valgrind
let mut cargo = valgrind
.arg("--xml=yes")
.arg(format!("--xml-socket={}:{}", address.ip(), address.port()))
.args(command)
Expand Down Expand Up @@ -110,17 +110,24 @@ where
Ok(xml)
});

let output = cargo.wait_with_output().map_err(|_| Error::ProcessFailed)?;
if output.status.success() {
let status = cargo.wait().map_err(|_| Error::ProcessFailed)?;
if status.success() {
let xml = xml.join().expect("Reader-thread panicked")?;
Ok(xml)
} else {
// this does not really terminalte the thread, but detaches it. Despite
// that, the thread will be killed, if the main thread exits.
drop(xml);
Err(Error::ValgrindFailure(
String::from_utf8_lossy(&output.stderr).to_string(),
))

let mut buffer = Vec::default();
cargo
.stderr
.as_mut()
.expect("stderr is piped and thus has to be available")
.read_to_end(&mut buffer)
.ok();
let stderr = String::from_utf8_lossy(&buffer);
Err(Error::ValgrindFailure(stderr.to_string()))
}

// TODO: use drop guard, that waits on child in order to prevent printing to stdout of the child
Expand Down

0 comments on commit 5d6d39f

Please sign in to comment.