Skip to content

Commit 0fc0c5a

Browse files
committed
Close non-stdio file handles when daemonizing
Otherwise, when the compiler wrapper spawns the sccache server, the server may end up with unintended file descriptors, which can lead to unexpected problems. This is particularly problematic if any of those file descriptors that accidentally end up in the server process is a pipe, as the pipe will only be closed when all the processes with that file descriptor close it or exit. This was causing sccache to hang ninja, as ninja uses the EOF of a pipe given to the subprocess to detect when that subprocess has exited: ninja-build/ninja#2444 (comment) This patch adds a dependency on the [close_fds](https://crates.io/crates/close_fds) crate, which automatically chooses an appropriate mechanism to close a range of file descriptors. On Linux 5.9+ that mechanism will be libc::close_range(). Fixes mozilla#2313
1 parent 9ca8beb commit 0fc0c5a

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Cargo.lock

+11
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
@@ -121,6 +121,7 @@ rouille = { version = "3.6", optional = true, default-features = false, features
121121
] }
122122
syslog = { version = "6", optional = true }
123123
version-compare = { version = "0.1.1", optional = true }
124+
close_fds = "0.3.2"
124125

125126
[dev-dependencies]
126127
assert_cmd = "2.0.13"

src/util.rs

+4
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,10 @@ pub fn daemonize() -> Result<()> {
851851
Ok(ref val) if val == "1" => {}
852852
_ => {
853853
Daemonize::new().start().context("failed to daemonize")?;
854+
// Be extra-zealous and clase all non-stdio file descriptors.
855+
unsafe {
856+
close_fds::close_open_fds(3, &[]);
857+
}
854858
}
855859
}
856860

0 commit comments

Comments
 (0)