Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Block SIGTTIN/SIGTTOU signals in PTY tests for Codex compatibility (#137)
Maximilian Roosd1e48a850e69
diff --git a/Cargo.toml b/Cargo.tomlindex 6a28abd00..deeeb5a87 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -89,6 +89,10 @@ skim = "0.20"nix = { version = "0.29", default-features = false, features = ["process", "signal"] }signal-hook = { version = "0.3.17", default-features = false, features = ["iterator"] }+[target.'cfg(unix)'.dev-dependencies]+# Match version used by portable-pty to avoid compiling multiple nix versions+nix = { version = "0.29", default-features = false, features = ["signal"] }+[build-dependencies]vergen-gitcl = { version = "1.0.8", features = ["build"] }diff --git a/tests/common/mod.rs b/tests/common/mod.rsindex 1c623023c..d19937b6f 100644--- a/tests/common/mod.rs+++ b/tests/common/mod.rs@@ -34,6 +34,82 @@ pub mod progressive_output;#[cfg(all(unix, feature = "shell-integration-tests"))]pub mod shell;+/// Block SIGTTIN and SIGTTOU signals to prevent test processes from being+/// stopped when PTY operations interact with terminal control in background+/// process groups.+///+/// This is needed when running tests in environments like Codex where the test+/// process may be in the background process group of a controlling terminal.+/// PTY operations (via `portable_pty`) can trigger these signals, causing the+/// process to be stopped rather than continuing execution.+///+/// Signal masks are per-thread, so this must be called on each thread that+/// performs PTY operations. It's idempotent within a thread (safe to call+/// multiple times on the same thread).+///+/// **Preferred usage**: Use the `pty_safe` rstest fixture instead of calling directly:+/// ```ignore+/// use rstest::rstest;+/// use crate::common::pty_safe;+///+/// #[rstest]+/// fn test_something(_pty_safe: ()) {+/// // PTY operations here won't cause SIGTTIN/SIGTTOU stops+/// }+/// ```+#[cfg(unix)]+pub fn ignore_tty_signals() {+ use std::cell::Cell;+ thread_local! {+ static TTY_SIGNALS_BLOCKED: Cell<bool> = const { Cell::new(false) };+ }+ TTY_SIGNALS_BLOCKED.with(|blocked| {+ if blocked.get() {+ return;+ }+ use nix::sys::signal::{SigSet, SigmaskHow, Signal, pthread_sigmask};+ let mut mask = SigSet::empty();+ mask.add(Signal::SIGTTIN);+ mask.add(Signal::SIGTTOU);+ // Block these signals in the current thread's signal mask.+ // Fail fast if this doesn't work - silent failure would cause flaky tests.+ pthread_sigmask(SigmaskHow::SIG_BLOCK, Some(&mask), None)+ .expect("failed to block SIGTTIN/SIGTTOU signals");+ blocked.set(true);+ });+}++/// Rstest fixture that blocks SIGTTIN/SIGTTOU signals before each test.+///+/// Use this for any test that performs PTY operations to prevent the test+/// from being stopped when running in background process groups (e.g., Codex).+///+/// # Example+/// ```ignore+/// use rstest::rstest;+/// use crate::common::pty_safe;+///+/// #[rstest]+/// fn test_pty_interaction(_pty_safe: ()) {+/// // PTY operations here are safe from SIGTTIN/SIGTTOU stops+/// }+/// ```+#[cfg(unix)]+#[rstest::fixture]+pub fn pty_safe() {+ ignore_tty_signals();+}++/// Returns a PTY system after ensuring SIGTTIN/SIGTTOU signals are blocked.+///+/// Use this instead of `portable_pty::native_pty_system()` directly to ensure+/// PTY tests work in background process groups (e.g., Codex).+#[cfg(unix)]+pub fn native_pty_system() -> Box<dyn portable_pty::PtySystem> {+ ignore_tty_signals();+ portable_pty::native_pty_system()+}+use insta_cmd::get_cargo_bin;use std::collections::HashMap;use std::path::{Path, PathBuf};diff --git a/tests/common/progressive_output.rs b/tests/common/progressive_output.rsindex 8b608586f..863292b0c 100644--- a/tests/common/progressive_output.rs+++ b/tests/common/progressive_output.rs@@ -100,7 +100,7 @@use crate::common::{ExponentialBackoff, TestRepo};use insta_cmd::get_cargo_bin;-use portable_pty::{CommandBuilder, PtySize, native_pty_system};+use portable_pty::{CommandBuilder, PtySize};use std::io::Read;use std::time::{Duration, Instant};@@ -331,8 +331,8 @@ pub fn capture_progressive_output() -> ProgressiveOutput {let start_time = Instant::now();- // Create PTY- let pty_system = native_pty_system();+ // Create PTY (handles signal blocking for background process groups)+ let pty_system = super::native_pty_system();let pair = pty_system.openpty(PtySize {rows: options.terminal_size.0,diff --git a/tests/integration_tests/approval_pty.rs b/tests/integration_tests/approval_pty.rsindex 9172da281..5df6063bd 100644--- a/tests/integration_tests/approval_pty.rs+++ b/tests/integration_tests/approval_pty.rs@@ -15,7 +15,7 @@use crate::common::TestRepo;use insta::assert_snapshot;use insta_cmd::get_cargo_bin;-use portable_pty::{CommandBuilder, PtySize, native_pty_system};+use portable_pty::{CommandBuilder, PtySize};use std::io::{Read, Write};use std::path::Path;@@ -29,7 +29,7 @@ fn exec_in_pty_with_input(env_vars: &[(String, String)],input: &str,) -> (String, i32) {- let pty_system = native_pty_system();+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: 48,diff --git a/tests/integration_tests/select.rs b/tests/integration_tests/select.rsindex 8ae4f9930..89aaa0530 100644--- a/tests/integration_tests/select.rs+++ b/tests/integration_tests/select.rs@@ -22,7 +22,7 @@use crate::common::TestRepo;use insta::assert_snapshot;use insta_cmd::get_cargo_bin;-use portable_pty::{CommandBuilder, PtySize, native_pty_system};+use portable_pty::{CommandBuilder, PtySize};use std::io::{Read, Write};use std::path::Path;use std::sync::mpsc;@@ -99,7 +99,7 @@ fn exec_in_pty_with_input_expectations(env_vars: &[(String, String)],inputs: &[(&str, Option<&str>)],) -> (Vec<u8>, i32) {- let pty_system = native_pty_system();+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: TERM_ROWS,diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rsindex 66f728bb1..dc15e80f0 100644--- a/tests/integration_tests/shell_wrapper.rs+++ b/tests/integration_tests/shell_wrapper.rs@@ -350,10 +350,11 @@ fn exec_in_pty_interactive(env_vars: &[(&str, &str)],inputs: &[&str],) -> (String, i32) {- use portable_pty::{CommandBuilder, PtySize, native_pty_system};+ use portable_pty::{CommandBuilder, PtySize};use std::io::{Read, Write};- let pty_system = native_pty_system();+ // Ignore SIGTTIN/SIGTTOU to prevent stopping in background process groups+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: 48,@@ -448,7 +449,7 @@ fn exec_bash_truly_interactive(working_dir: &std::path::Path,env_vars: &[(&str, &str)],) -> (String, i32) {- use portable_pty::{CommandBuilder, PtySize, native_pty_system};+ use portable_pty::{CommandBuilder, PtySize};use std::io::{Read, Write};use std::thread;use std::time::Duration;@@ -458,7 +459,7 @@ fn exec_bash_truly_interactive(let script_path = tmp_dir.path().join("setup.sh");fs::write(&script_path, setup_script).unwrap();- let pty_system = native_pty_system();+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: 48,@@ -2614,7 +2615,7 @@ fi/// The shell integration hint is truncated from the output.#[test]fn test_readme_example_approval_prompt() {- use portable_pty::{CommandBuilder, PtySize, native_pty_system};+ use portable_pty::{CommandBuilder, PtySize};use std::io::{Read, Write};let repo = TestRepo::new();@@ -2631,7 +2632,8 @@ test = "echo 'Running tests...'"repo.commit("Add config");// Direct PTY execution (not through shell wrapper) for interactive prompt- let pty_system = native_pty_system();+ // Ignore SIGTTIN/SIGTTOU to prevent stopping in background process groups+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: 48,@@ -2729,7 +2731,7 @@ test = "echo 'Running tests...'"/// - Completion output being executed as commands (the COMPLETE mode bug)#[test]fn test_bash_completion_produces_correct_output() {- use portable_pty::{CommandBuilder, PtySize, native_pty_system};+ use portable_pty::{CommandBuilder, PtySize};use std::io::Read;let repo = TestRepo::new();@@ -2810,7 +2812,8 @@ fi);// Execute in PTY- let pty_system = native_pty_system();+ // Ignore SIGTTIN/SIGTTOU to prevent stopping in background process groups+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: 48,@@ -2893,7 +2896,7 @@ fi/// wt command with COMPLETE=zsh produces completion candidates.#[test]fn test_zsh_completion_produces_correct_output() {- use portable_pty::{CommandBuilder, PtySize, native_pty_system};+ use portable_pty::{CommandBuilder, PtySize};use std::io::Read;let repo = TestRepo::new();@@ -2963,7 +2966,8 @@ fi);// Execute in PTY- let pty_system = native_pty_system();+ // Ignore SIGTTIN/SIGTTOU to prevent stopping in background process groups+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: 48,@@ -3164,7 +3168,7 @@ for c in "${{COMPREPLY[@]}}"; do echo "${{c%% *}}"; done#[case("fish")]fn test_wrapper_help_redirect_captures_all_output(#[case] shell: &str) {skip_if_shell_unavailable!(shell);- use portable_pty::{CommandBuilder, PtySize, native_pty_system};+ use portable_pty::{CommandBuilder, PtySize};use std::io::Read;let repo = TestRepo::new();@@ -3248,7 +3252,8 @@ echo "SCRIPT_COMPLETED"};// Execute in PTY- let pty_system = native_pty_system();+ // Ignore SIGTTIN/SIGTTOU to prevent stopping in background process groups+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: 48,@@ -3362,7 +3367,7 @@ echo "SCRIPT_COMPLETED"#[case("fish")]fn test_wrapper_help_interactive_uses_pager(#[case] shell: &str) {skip_if_shell_unavailable!(shell);- use portable_pty::{CommandBuilder, PtySize, native_pty_system};+ use portable_pty::{CommandBuilder, PtySize};use std::io::Read;let repo = TestRepo::new();@@ -3462,7 +3467,8 @@ echo "SCRIPT_COMPLETED"};// Execute in PTY (simulates interactive terminal)- let pty_system = native_pty_system();+ // Ignore SIGTTIN/SIGTTOU to prevent stopping in background process groups+ let pty_system = crate::common::native_pty_system();let pair = pty_system.openpty(PtySize {rows: 48,