Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Serialize signal scope retirement with signal receipt (#4609)
Maximilian Roosd697e941e683
diff --git a/src/shell_exec.rs b/src/shell_exec.rsindex 2d7b87ec7..fb537ba6b 100644--- a/src/shell_exec.rs+++ b/src/shell_exec.rs@@ -170,6 +170,14 @@ pub(crate) fn with_process_owner<T: Send + 'static>(Ok(create(command))}+/// Retire a signal scope after the signal owner's pending receipt completes.+#[cfg(unix)]+pub(crate) fn on_process_owner<T: Send + 'static>(+ operation: impl FnOnce() -> T + Send + 'static,+) -> std::io::Result<T> {+ process_owner::run(operation)+}+/// Create wakeup endpoints under the same guard as process creation.#[cfg(unix)]pub fn socket_pair() -> std::io::Result<(diff --git a/src/shell_exec/process_owner.rs b/src/shell_exec/process_owner.rsindex 2aedaa1d6..d5c54c32f 100644--- a/src/shell_exec/process_owner.rs+++ b/src/shell_exec/process_owner.rs@@ -7,9 +7,10 @@//! admits a child. If spawn temporarily blocks signals, restoring its mask runs//! the pending handler before the admission check.//!-//! Requests cover creation and admission, never child waits. Commands return to-//! their callers for reuse. The descriptor-creation lock remains inside each raw-//! spawn: callers must not hold it while waiting for an owner request.+//! Requests cover creation, admission, and signal-scope retirement, never child+//! waits. Commands return to their callers for reuse. The descriptor-creation+//! lock remains inside each raw spawn: callers must not hold it while waiting+//! for an owner request.//! Library users that do not initialize process startup keep direct spawning;//! establishing inherited masks after arbitrary threads exist would be unsafe.@@ -22,19 +23,24 @@ use nix::sys::signal::{SigSet, Signal};use once_cell::sync::OnceCell;type Request = Box<dyn FnOnce() + Send>;-static OWNER: OnceCell<mpsc::Sender<Request>> = OnceCell::new();+struct Owner {+ sender: mpsc::Sender<Request>,+ thread: std::thread::ThreadId,+}++static OWNER: OnceCell<Owner> = OnceCell::new();pub(super) fn init() -> io::Result<()> {OWNER.get_or_try_init(start).map(|_| ())}-fn start() -> io::Result<mpsc::Sender<Request>> {+fn start() -> io::Result<Owner> {let mut blocked = SigSet::thread_get_mask()?;blocked.add(Signal::SIGINT);blocked.add(Signal::SIGTERM);let (sender, receiver) = mpsc::channel::<Request>();- std::thread::Builder::new()+ let owner = std::thread::Builder::new().name("wt-process-owner".into()).spawn(move || {for request in receiver {@@ -45,29 +51,48 @@ fn start() -> io::Result<mpsc::Sender<Request>> {// Spawn failure leaves our mask intact; mask failure drops the sender and// lets the unpublished owner exit.blocked.thread_set_mask()?;- Ok(sender)+ Ok(Owner {+ sender,+ thread: owner.thread().id(),+ })}-pub(super) fn with_command<T: Send + 'static>(- command: &mut Command,- create: impl FnOnce(&mut Command) -> T + Send + 'static,+pub(super) fn run<T: Send + 'static>(+ operation: impl FnOnce() -> T + Send + 'static,) -> io::Result<T> {let Some(owner) = OWNER.get() else {- return Ok(create(command));+ return Ok(operation());};- let mut owned = std::mem::replace(command, Command::new(""));+ if std::thread::current().id() == owner.thread {+ return Ok(operation());+ }let (sender, receiver) = mpsc::channel();owner+ .sender.send(Box::new(move || {- // Keep the signal owner alive if a request panics, and put the- // reusable command back before propagating that panic to its caller.- let result = catch_unwind(AssertUnwindSafe(|| create(&mut owned)));- let _ = sender.send((owned, result));+ // Keep the signal owner alive if a request panics.+ let _ = sender.send(catch_unwind(AssertUnwindSafe(operation)));})).map_err(|error| io::Error::other(error.to_string()))?;- let (returned, result) = receiver+ match receiver.recv()- .map_err(|error| io::Error::other(error.to_string()))?;+ .map_err(|error| io::Error::other(error.to_string()))?+ {+ Ok(result) => Ok(result),+ Err(panic) => resume_unwind(panic),+ }+}++pub(super) fn with_command<T: Send + 'static>(+ command: &mut Command,+ create: impl FnOnce(&mut Command) -> T + Send + 'static,+) -> io::Result<T> {+ let mut owned = std::mem::replace(command, Command::new(""));+ let (returned, result) = run(move || {+ // Return the reusable command even if its operation panics.+ let result = catch_unwind(AssertUnwindSafe(|| create(&mut owned)));+ (owned, result)+ })?;*command = returned;match result {Ok(result) => Ok(result),@@ -129,6 +154,17 @@ mod tests {.unwrap_err();let owner = *panic.downcast::<std::thread::ThreadId>().unwrap();assert_ne!(owner, std::thread::current().id());+ // Generic requests propagate panic payloads too, and nested requests+ // execute on the owner instead of waiting on that same thread.+ let panic = catch_unwind(|| {+ run(|| std::panic::panic_any(std::thread::current().id())).unwrap();+ })+ .unwrap_err();+ assert_eq!(*panic.downcast::<std::thread::ThreadId>().unwrap(), owner);+ assert_eq!(+ run(|| run(|| std::thread::current().id()).unwrap()).unwrap(),+ owner+ );assert_eq!(with_command(&mut command, |_| std::thread::current().id()).unwrap(),ownerdiff --git a/src/signal_forwarder.rs b/src/signal_forwarder.rsindex 2600ce5f3..625c584c4 100644--- a/src/signal_forwarder.rs+++ b/src/signal_forwarder.rs@@ -14,8 +14,11 @@//! SharedChild serializes those deliveries with reaping. No caller group is killed.//! A group-wide TERM may also reach a child through this PID forwarding;//! repeated TERM deliveries never trigger escalation here.-//! The process owner receives signals and performs foreground admission on the-//! same thread, so a handler cannot lag behind a spawn on a different thread.+//! The process owner receives signals and performs foreground admission and+//! retirement on the same thread, so a handler cannot lag behind a spawn on a+//! different thread.+//! Retirement restores idle defaults only after pending receipt completes;+//! a child that already handled the key cannot turn that key into an idle signal.//! Spawn return commits admission. Startup cancellation retires an uncommitted//! child with TERM; its cleanup is best effort, while admitted children finish//! native-key cleanup. TERM is cooperative, with no escalation.@@ -216,12 +219,16 @@ impl Lease {impl Drop for Lease {fn drop(&mut self) {- // Cleanup must restore defaults even if another owner panicked.- let mut activity = self.0.lock().unwrap_or_else(|error| error.into_inner());- activity.count -= 1;- if activity.count == 0 {- activity.idle.store(true, Ordering::SeqCst);- }+ let state = self.0;+ let _ = crate::shell_exec::on_process_owner(move || {+ // Receipt must finish before restoring idle defaults, even when+ // the direct child handled the key and has already exited.+ let mut activity = state.lock().unwrap_or_else(|error| error.into_inner());+ activity.count -= 1;+ if activity.count == 0 {+ activity.idle.store(true, Ordering::SeqCst);+ }+ });}}@@ -385,6 +392,9 @@ impl ForegroundSignals {}let listener = thread::spawn(move || {for signal in signals.forever() {+ if tracing::enabled!(target: crate::trace::WT_TRACE_TARGET, tracing::Level::DEBUG) {+ crate::trace::instant(&format!("Foreground signal received:{signal}"));+ }if signal == SIGTERM {cancel_foreground(signal);for child in &children {@@ -537,6 +547,123 @@ mod tests {use crate::git::ErrorExt;use crate::shell_exec::Cmd;+ #[test]+ #[allow(unsafe_code)]+ fn terminal_handoff_keeps_status_after_delayed_signal_receipt() {+ use nix::sys::signal::{SigSet, Signal, killpg};+ use nix::unistd::Pid;+ use std::os::unix::process::CommandExt;+ use std::time::{Duration, Instant};++ const CHILD_ENV: &str = "WORKTRUNK_TEST_DELAYED_HANDOFF_CHILD";+ let mut signals = SigSet::empty();+ signals.add(Signal::SIGINT);+ signals.add(Signal::SIGTERM);+ if std::env::var_os(CHILD_ENV).is_none() {+ let mut command = std::process::Command::new(std::env::current_exe().unwrap());+ command+ .args([+ "--exact",+ "signal_forwarder::tests::terminal_handoff_keeps_status_after_delayed_signal_receipt",+ "--nocapture",+ ])+ .env(CHILD_ENV, "1")+ .process_group(0)+ .stdout(std::process::Stdio::piped())+ .stderr(std::process::Stdio::piped());+ // SAFETY: pthread_sigmask is async-signal-safe and accesses a set+ // constructed before fork. The libtest harness stays blocked.+ unsafe {+ command.pre_exec(move || signals.thread_block().map_err(Into::into));+ }+ let output = crate::shell_exec::spawn(&mut command)+ .unwrap()+ .wait_with_output()+ .unwrap();+ let stdout = String::from_utf8_lossy(&output.stdout);+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ output.status.success(),+ "{:?}\n{stdout}\n{stderr}",+ output.status+ );+ assert!(stdout.contains(" 1 passed;"));+ return;+ }++ signals.thread_unblock().unwrap();+ crate::shell_exec::init_startup().unwrap();+ let entered = Arc::new(AtomicBool::new(false));+ let release = Arc::new(AtomicBool::new(false));+ struct Release(Arc<AtomicBool>);+ impl Drop for Release {+ fn drop(&mut self) {+ self.0.store(true, Ordering::SeqCst);+ }+ }+ let release_on_panic = Release(release.clone());+ let received = entered.clone();+ let released = release.clone();+ // Register before the idle default action so receipt can pause before+ // its decision, as when the signal-owning thread is descheduled.+ // SAFETY: the callback performs only lock-free atomic operations.+ let delayed = unsafe {+ signal_hook::low_level::register(SIGINT, move || {+ received.store(true, Ordering::SeqCst);+ while !released.load(Ordering::SeqCst) {+ std::hint::spin_loop();+ }+ })+ }+ .unwrap();+ let operation = CommandOperation::start().unwrap();+ let foreground = ForegroundSignals::install(TerminalInterrupt::ChildInput).unwrap();+ let temp = tempfile::tempdir().unwrap();+ let marker = temp.path().join("ready");+ let mut command = std::process::Command::new("sh");+ command+ .args([+ "-c",+ "trap 'echo child-handled >&2; exit 0' INT; echo ready > \"$1\"; while :; do sleep 0.01; done",+ "sh",+ ])+ .arg(&marker);+ let child = Arc::new(foreground.spawn(&mut command).unwrap());+ let deadline = Instant::now() + Duration::from_secs(20);+ while !marker.exists() {+ assert!(Instant::now() < deadline, "child did not become ready");+ thread::sleep(Duration::from_millis(1));+ }+ let idle = Lease::state().unwrap().lock().unwrap().idle.clone();+ let waiting_child = child.clone();+ let (waiting, started) = std::sync::mpsc::channel();+ let waiter = thread::spawn(move || {+ waiting.send(()).unwrap();+ foreground.wait(&waiting_child).unwrap()+ });+ started.recv_timeout(Duration::from_secs(20)).unwrap();+ killpg(Pid::this(), Signal::SIGINT).unwrap();+ while !entered.load(Ordering::SeqCst) {+ assert!(Instant::now() < deadline, "signal handler did not run");+ thread::yield_now();+ }+ // The child has finished successfully while wt's own receipt is still+ // paused. Retirement must not restore idle defaults during that receipt.+ assert!(child.wait().unwrap().success());+ thread::sleep(crate::testing::SLEEP_FOR_ABSENCE_CHECK);+ let retired_during_receipt = idle.load(Ordering::SeqCst);+ drop(release_on_panic);+ let outcome = waiter.join().unwrap();+ signal_hook::low_level::unregister(delayed);+ assert!(+ !retired_during_receipt,+ "idle defaults restored during receipt"+ );+ assert!(outcome.status.success());+ assert_eq!(outcome.cancellation, None);+ assert_eq!(operation.interrupt_signal(), None);+ }+/// Delay receipt before the flag callbacks, as when the kernel dequeues/// SIGINT onto a thread that is then descheduled. Admission must wait for/// that receipt instead of starting a child that missed the terminal key.diff --git a/tests/integration_tests/step_alias.rs b/tests/integration_tests/step_alias.rsindex 7fe3321ab..5fa9d5b02 100644--- a/tests/integration_tests/step_alias.rs+++ b/tests/integration_tests/step_alias.rs@@ -1436,11 +1436,34 @@ fn test_alias_group_sigint_does_not_escalate(#[cfg(unix)]fn test_alias_last_step_keeps_status_after_caught_ctrl_c(repo: TestRepo) {repo.write_test_config(- "[aliases]\nsignal-test = \"trap 'exit 0' INT; echo $$ > pid; while :; do sleep 0.1; done\"\n",+ "[aliases]\nsignal-test = \"trap 'echo handled > int; while [ ! -e release ]; do sleep 0.01; done; exit 0' INT; echo $$ > pid; while :; do sleep 0.1; done\"\n",);- let mut job = SignalJob::spawn(&repo, &["-vv", "step", "signal-test"], Stdio::null());+ let stderr_path = repo.root_path().join("stderr");+ let stderr = std::fs::File::create(&stderr_path).unwrap();+ let mut job = SignalJob::spawn(&repo, &["-vv", "step", "signal-test"], stderr.into());job.interrupt_admitted_child(&repo, &repo.root_path().join("pid"));- assert!(job.wait().success());+ crate::common::wait_for_file_content(&repo.root_path().join("int"));+ // Keep the child alive until wt has received the group signal. Otherwise+ // the child can exit before SIGINT reaches wt, turning it into an idle key.+ let trace = crate::common::resolve_git_common_dir(repo.root_path()).join("wt/logs/trace.jsonl");+ let receipt = format!(+ "Foreground signal received:{}",+ nix::sys::signal::Signal::SIGINT as i32+ );+ crate::common::wait_for("wt to receive SIGINT", || {+ std::fs::read_to_string(&trace).is_ok_and(|records| {+ worktrunk::trace::parse_lines(&records).iter().any(|entry| {+ matches!(&entry.kind, worktrunk::trace::TraceEntryKind::Instant { name } if name == &receipt)+ })+ })+ });+ job.release();+ let status = job.wait();+ assert!(+ status.success(),+ "the final child handled Ctrl-C successfully: {status:?}\n{}",+ std::fs::read_to_string(stderr_path).unwrap()+ );}/// Non-UTF-8 bytes and CRLF line endings in child output must not stall the