Skip to content

Commit 2b457ad

Browse files
committed
Auto merge of #25784 - geofft:subprocess-signal-masks, r=alexcrichton
UNIX specifies that signal dispositions and masks get inherited to child processes, but in general, programs are not very robust to being started with non-default signal dispositions or to signals being blocked. For example, libstd sets `SIGPIPE` to be ignored, on the grounds that Rust code using libstd will get the `EPIPE` errno and handle it correctly. But shell pipelines are built around the assumption that `SIGPIPE` will have its default behavior of killing the process, so that things like `head` work: ``` geofft@titan:/tmp$ for i in `seq 1 20`; do echo "$i"; done | head -1 1 geofft@titan:/tmp$ cat bash.rs fn main() { std::process::Command::new("bash").status(); } geofft@titan:/tmp$ ./bash geofft@titan:/tmp$ for i in `seq 1 20`; do echo "$i"; done | head -1 1 bash: echo: write error: Broken pipe bash: echo: write error: Broken pipe bash: echo: write error: Broken pipe bash: echo: write error: Broken pipe bash: echo: write error: Broken pipe [...] ``` Here, `head` is supposed to terminate the input process quietly, but the bash subshell has inherited the ignored disposition of `SIGPIPE` from its Rust grandparent process. So it gets a bunch of `EPIPE`s that it doesn't know what to do with, and treats it as a generic, transient error. You can see similar behavior with `find / | head`, `yes | head`, etc. This PR resets Rust's `SIGPIPE` handler, as well as any signal mask that may have been set, before spawning a child. Setting a signal mask, and then using a dedicated thread or something like `signalfd` to dequeue signals, is one of two reasonable ways for a library to process signals. See tokio-rs/mio#16 for more discussion about this approach to signal handling and why it needs a change to `std::process`. The other approach is for the library to set a signal-handling function (`signal()` / `sigaction()`): in that case, dispositions are reset to the default behavior on exec (since the function pointer isn't valid across exec), so we don't have to care about that here. As part of this PR, I noticed that we had two somewhat-overlapping sets of bindings to signal functionality in `libstd`. One dated to old-IO and probably the old runtime, and was mostly unused. The other is currently used by `stack_overflow.rs`. I consolidated the two bindings into one set, and double-checked them by hand against all supported platforms' headers. This probably means it's safe to enable `stack_overflow.rs` on more targets, but I'm not including such a change in this PR. r? @alexcrichton cc @Zoxc for changes to `stack_overflow.rs`
2 parents 621a10e + 824a928 commit 2b457ad

File tree

5 files changed

+319
-362
lines changed

5 files changed

+319
-362
lines changed

Diff for: src/libstd/process.rs

-18
Original file line numberDiff line numberDiff line change
@@ -737,24 +737,6 @@ mod tests {
737737
}
738738
}
739739

740-
#[cfg(all(unix, not(target_os="android")))]
741-
pub fn pwd_cmd() -> Command {
742-
Command::new("pwd")
743-
}
744-
#[cfg(target_os="android")]
745-
pub fn pwd_cmd() -> Command {
746-
let mut cmd = Command::new("/system/bin/sh");
747-
cmd.arg("-c").arg("pwd");
748-
cmd
749-
}
750-
751-
#[cfg(windows)]
752-
pub fn pwd_cmd() -> Command {
753-
let mut cmd = Command::new("cmd");
754-
cmd.arg("/c").arg("cd");
755-
cmd
756-
}
757-
758740
#[cfg(all(unix, not(target_os="android")))]
759741
pub fn env_cmd() -> Command {
760742
Command::new("env")

0 commit comments

Comments
 (0)