Skip to content

Commit

Permalink
Close non-stdio file handles when daemonizing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ntrrgc committed Jan 8, 2025
1 parent 9ca8beb commit 0fc0c5a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ rouille = { version = "3.6", optional = true, default-features = false, features
] }
syslog = { version = "6", optional = true }
version-compare = { version = "0.1.1", optional = true }
close_fds = "0.3.2"

[dev-dependencies]
assert_cmd = "2.0.13"
Expand Down
4 changes: 4 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,10 @@ pub fn daemonize() -> Result<()> {
Ok(ref val) if val == "1" => {}
_ => {
Daemonize::new().start().context("failed to daemonize")?;
// Be extra-zealous and clase all non-stdio file descriptors.
unsafe {
close_fds::close_open_fds(3, &[]);
}
}
}

Expand Down

0 comments on commit 0fc0c5a

Please sign in to comment.