Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: treat SIGPIPE as success in Cmd::stream() (#1559)

Maximilian Roosdc586e1e0403
diff --git a/src/shell_exec.rs b/src/shell_exec.rs
index 21c3ec769..8c918b0b1 100644
--- a/src/shell_exec.rs
+++ b/src/shell_exec.rs
@@ -708,7 +708,7 @@ impl Cmd {
pub fn stream(mut self) -> anyhow::Result<()> {
#[cfg(unix)]
use {
- signal_hook::consts::{SIGINT, SIGTERM},
+ signal_hook::consts::{SIGINT, SIGPIPE, SIGTERM},
signal_hook::iterator::Signals,
std::os::unix::process::CommandExt,
};
@@ -875,6 +875,12 @@ impl Cmd {
#[cfg(unix)]
if let Some(sig) = std::os::unix::process::ExitStatusExt::signal(&status) {
+ // SIGPIPE (13) is expected when a pager (less, bat) exits before the
+ // child finishes writing — not an error from the user's perspective.
+ if sig == SIGPIPE {
+ log_external(Some(0));
+ return Ok(());
+ }
log_external(Some(128 + sig));
return Err(WorktrunkError::ChildProcessExited {
code: 128 + sig,
@@ -1214,6 +1220,38 @@ mod tests {
}
}
+ #[test]
+ #[cfg(unix)]
+ fn test_cmd_stream_sigpipe_is_not_an_error() {
+ // Simulates pager quit: the child is killed by SIGPIPE, same as when
+ // `git diff` writes to a pager and the user presses `q`.
+ // `sh -c 'kill -PIPE $$'` sends SIGPIPE to itself, terminating with signal 13.
+ let result = Cmd::new("sh").args(["-c", "kill -PIPE $$"]).stream();
+ assert!(
+ result.is_ok(),
+ "SIGPIPE should not be treated as an error: {result:?}"
+ );
+ }
+
+ #[test]
+ #[cfg(unix)]
+ fn test_cmd_stream_other_signals_are_errors() {
+ use crate::git::WorktrunkError;
+
+ // Non-SIGPIPE signals (like SIGTERM) should still be treated as errors.
+ let result = Cmd::new("sh").args(["-c", "kill -TERM $$"]).stream();
+ assert!(result.is_err());
+
+ let err = result.unwrap_err();
+ let wt_err = err.downcast_ref::<WorktrunkError>().unwrap();
+ match wt_err {
+ WorktrunkError::ChildProcessExited { code, .. } => {
+ assert_eq!(*code, 128 + 15); // SIGTERM = 15
+ }
+ _ => panic!("Expected ChildProcessExited error"),
+ }
+ }
+
#[test]
#[cfg(unix)]
fn test_cmd_shell_stream_with_stdin() {