Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/cmdext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ pub trait CapStdExtCommandExt {

/// Use the given directory as the current working directory for the process.
fn cwd_dir(&mut self, dir: Dir) -> &mut Self;

/// On Linux, arrange for [`SIGTERM`] to be delivered to the child if the
/// parent *thread* exits. This helps avoid leaking child processes if
/// the parent crashes for example.
///
/// # IMPORTANT
///
/// Due to the semantics of <https://man7.org/linux/man-pages/man2/prctl.2.html> this
/// will cause the child to exit when the parent *thread* (not process) exits. In
/// particular this can become problematic when used with e.g. a threadpool such
/// as Tokio's <https://kobzol.github.io/rust/2025/02/23/tokio-plus-prctl-equals-nasty-bug.html>.
#[cfg(any(target_os = "linux", target_os = "android"))]
fn lifecycle_bind_to_parent_thread(&mut self) -> &mut Self;
}

#[allow(unsafe_code)]
Expand Down Expand Up @@ -58,6 +71,20 @@ impl CapStdExtCommandExt for std::process::Command {
}
self
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn lifecycle_bind_to_parent_thread(&mut self) -> &mut Self {
// SAFETY: This API is safe to call in a forked child.
unsafe {
self.pre_exec(|| {
rustix::process::set_parent_process_death_signal(Some(
rustix::process::Signal::TERM,
))
.map_err(Into::into)
});
}
self
}
}

#[cfg(test)]
Expand Down
11 changes: 11 additions & 0 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,14 @@ fn test_big_xattr() -> Result<()> {

Ok(())
}

#[test]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn test_lifecycle_bind_to_parent_thread() -> Result<()> {
let status = Command::new("true")
.lifecycle_bind_to_parent_thread()
.status()?;
assert!(status.success());

Ok(())
}
Loading