Skip to content

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

Fix: Add SIGINT handler to wt_exec cleanup

Maximilian Roose869d40a1033
diff --git a/templates/posix_directives.sh b/templates/posix_directives.sh
index 2662671b2..de75fa830 100644
--- a/templates/posix_directives.sh
+++ b/templates/posix_directives.sh
@@ -17,6 +17,20 @@ wt_exec() {
local exec_cmd="" chunk="" exit_code=0 tmp_dir="" fifo_path="" runner_pid=""
+ # Cleanup handler for signals and normal exit
+ _wt_cleanup() {
+ # Kill background process if still running
+ if [[ -n "$runner_pid" ]] && kill -0 "$runner_pid" 2>/dev/null; then
+ kill "$runner_pid" 2>/dev/null || true
+ fi
+ # Remove temp files
+ /bin/rm -f "$fifo_path" 2>/dev/null || true
+ /bin/rmdir "$tmp_dir" 2>/dev/null || true
+ }
+
+ # On SIGINT: cleanup and exit immediately with 130
+ trap '_wt_cleanup; return 130' INT
+
# Create temp directory with FIFO for streaming output
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/wt.XXXXXX") || {
echo "Failed to create temp directory for worktrunk shim" >&2
@@ -54,8 +68,8 @@ wt_exec() {
wait "$runner_pid" >/dev/null 2>&1 || exit_code=$?
# Cleanup
- /bin/rm -f "$fifo_path"
- /bin/rmdir "$tmp_dir" 2>/dev/null || true
+ trap - INT
+ _wt_cleanup
# Execute deferred command if specified (its exit code takes precedence)
if [[ -n "$exec_cmd" ]]; then
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index d8f094032..45950d014 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -10,13 +10,16 @@ info:
COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_COUNT: "1"
GIT_CONFIG_GLOBAL: /dev/null
+ GIT_CONFIG_KEY_0: advice.mergeConflict
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_CONFIG_VALUE_0: "false"
GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmplvmMbJ/test-config.toml
---
success: true
exit_code: 0
@@ -48,6 +51,20 @@ wt_exec() {
local exec_cmd="" chunk="" exit_code=0 tmp_dir="" fifo_path="" runner_pid=""
+ # Cleanup handler for signals and normal exit
+ _wt_cleanup() {
+ # Kill background process if still running
+ if [[ -n "$runner_pid" ]] && kill -0 "$runner_pid" 2>/dev/null; then
+ kill "$runner_pid" 2>/dev/null || true
+ fi
+ # Remove temp files
+ /bin/rm -f "$fifo_path" 2>/dev/null || true
+ /bin/rmdir "$tmp_dir" 2>/dev/null || true
+ }
+
+ # On SIGINT: cleanup and exit immediately with 130
+ trap '_wt_cleanup; return 130' INT
+
# Create temp directory with FIFO for streaming output
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/wt.XXXXXX") || {
echo "Failed to create temp directory for worktrunk shim" >&2
@@ -85,8 +102,8 @@ wt_exec() {
wait "$runner_pid" >/dev/null 2>&1 || exit_code=$?
# Cleanup
- /bin/rm -f "$fifo_path"
- /bin/rmdir "$tmp_dir" 2>/dev/null || true
+ trap - INT
+ _wt_cleanup
# Execute deferred command if specified (its exit code takes precedence)
if [[ -n "$exec_cmd" ]]; then
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index a50e9cfba..d33d3c996 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -15,11 +15,10 @@ info:
GIT_CONFIG_KEY_0: advice.mergeConflict
GIT_CONFIG_SYSTEM: /dev/null
GIT_CONFIG_VALUE_0: "false"
- GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpOSL0P3/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp8OQXUV/test-config.toml
---
success: true
exit_code: 0
@@ -51,6 +50,20 @@ wt_exec() {
local exec_cmd="" chunk="" exit_code=0 tmp_dir="" fifo_path="" runner_pid=""
+ # Cleanup handler for signals and normal exit
+ _wt_cleanup() {
+ # Kill background process if still running
+ if [[ -n "$runner_pid" ]] && kill -0 "$runner_pid" 2>/dev/null; then
+ kill "$runner_pid" 2>/dev/null || true
+ fi
+ # Remove temp files
+ /bin/rm -f "$fifo_path" 2>/dev/null || true
+ /bin/rmdir "$tmp_dir" 2>/dev/null || true
+ }
+
+ # On SIGINT: cleanup and exit immediately with 130
+ trap '_wt_cleanup; return 130' INT
+
# Create temp directory with FIFO for streaming output
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/wt.XXXXXX") || {
echo "Failed to create temp directory for worktrunk shim" >&2
@@ -88,8 +101,8 @@ wt_exec() {
wait "$runner_pid" >/dev/null 2>&1 || exit_code=$?
# Cleanup
- /bin/rm -f "$fifo_path"
- /bin/rmdir "$tmp_dir" 2>/dev/null || true
+ trap - INT
+ _wt_cleanup
# Execute deferred command if specified (its exit code takes precedence)
if [[ -n "$exec_cmd" ]]; then