From 3a5b76c1e25fb1a835a63283a3e8fb2bde6859de Mon Sep 17 00:00:00 2001 From: Fedor Sheremetyev Date: Tue, 5 Nov 2024 08:02:47 +0000 Subject: [PATCH] process: block SIGCHLD signal when spawning process --- tokio/src/process/unix/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tokio/src/process/unix/mod.rs b/tokio/src/process/unix/mod.rs index c9d1035f53d..832416067c6 100644 --- a/tokio/src/process/unix/mod.rs +++ b/tokio/src/process/unix/mod.rs @@ -116,6 +116,14 @@ impl fmt::Debug for Child { } pub(crate) fn spawn_child(cmd: &mut std::process::Command) -> io::Result { + // block SIGCHLD to avoid race condition with spawn and signal handler registration + unsafe { + let mut mask: libc::sigset_t = std::mem::zeroed(); + libc::sigemptyset(&mut mask); + libc::sigaddset(&mut mask, libc::SIGCHLD); + libc::sigprocmask(libc::SIG_BLOCK, &mask, std::ptr::null_mut()); + } + let mut child = cmd.spawn()?; let stdin = child.stdin.take().map(stdio).transpose()?; let stdout = child.stdout.take().map(stdio).transpose()?; @@ -137,6 +145,14 @@ pub(crate) fn spawn_child(cmd: &mut std::process::Command) -> io::Result