Skip to content

Commit 73c847c

Browse files
committed
guard process creation with a mutex
Fixes an fd leak on all unix systems that don't have pipe2. As of today, that's all unix systems that are not dragonfly bsd, freebsd, linux, netbsd, openbsd, or redox -- in particular, macOS. Fixes evmar#14.
1 parent 871c660 commit 73c847c

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition = "2018"
99
getopts = "0.2"
1010
anyhow = "1.0"
1111
libc = "0.2"
12+
lazy_static = "1.4.0"
1213

1314
[target.'cfg(windows)'.dependencies]
1415
kernel32-sys = "0.2.2"

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ mod task;
1313
pub mod trace;
1414
pub mod work;
1515

16+
#[macro_use]
17+
extern crate lazy_static;
18+
1619
#[cfg(not(target_env = "msvc"))]
1720
use jemallocator::Jemalloc;
1821

src/task.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::graph::{BuildId, RspFile};
1010
use crate::scanner::Scanner;
1111
use anyhow::{anyhow, bail};
1212
use std::sync::mpsc;
13+
use std::sync::Mutex;
1314
use std::time::{Duration, Instant};
1415

1516
#[cfg(unix)]
@@ -83,12 +84,21 @@ fn run_task(
8384
Ok(result)
8485
}
8586

87+
lazy_static! {
88+
static ref TASK_MUTEX: Mutex<i32> = Mutex::new(0);
89+
}
90+
8691
#[cfg(unix)]
8792
fn run_command(cmdline: &str) -> anyhow::Result<TaskResult> {
88-
let mut cmd = std::process::Command::new("/bin/sh")
93+
// Command::spawn() can leak FSs when run concurrently, see #14.
94+
let just_one = TASK_MUTEX.lock().unwrap();
95+
let p = std::process::Command::new("/bin/sh")
8996
.arg("-c")
9097
.arg(cmdline)
91-
.output()?;
98+
.spawn()?;
99+
drop(just_one);
100+
101+
let mut cmd = p.wait_with_output()?;
92102
let mut output = Vec::new();
93103
output.append(&mut cmd.stdout);
94104
output.append(&mut cmd.stderr);

0 commit comments

Comments
 (0)