From 490ad8353db3d4646d7cb01c27f3691ca007c150 Mon Sep 17 00:00:00 2001 From: bchanot Date: Sat, 14 Nov 2026 11:27:15 +0100 Subject: [PATCH] fix(session-start): perl fallback runner kills the command's process group alarm then exec killed one process: a child holding the captured pipe kept the 2 s bound open. perl now leads its own group, kills it at the deadline and TERMs leftovers; SD19/SD19b assert the bound and that the command ran. --- hooks/session-start.sh | 17 +++++++++++++++-- lib/tests/session-digest.test.sh | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/hooks/session-start.sh b/hooks/session-start.sh index 1653902..dccc811 100644 --- a/hooks/session-start.sh +++ b/hooks/session-start.sh @@ -37,13 +37,26 @@ elif [ -t 0 ]; then fi # ── Bounded runner: timeout, else gtimeout, else perl alarm (stock macOS) ── -# perl execs the command, so the child is the same process alarm kills. +# Each runner puts the command in its own process group and kills the group +# at the deadline, so a child holding the pipe dies too. The perl runner also +# TERMs leftovers after a normal exit (timeout does not; a leftover that +# ignores TERM is unbounded, none of the callers starts one). Trade-off, as +# timeout without --foreground: in a terminal run the command is a background +# job of the tty (no stdin read; under `stty tostop` a tty write stops it +# until the deadline). _SS_TMO=() for _ss_t in timeout gtimeout; do command -v "$_ss_t" >/dev/null 2>&1 && { _SS_TMO=("$_ss_t" 2); break; } done if [ ${#_SS_TMO[@]} -eq 0 ] && command -v perl >/dev/null 2>&1; then - _SS_TMO=(perl -e "alarm shift; exec { \$ARGV[0] } @ARGV" 2) + _SS_TMO=(perl -e " + my \$t = shift; \$SIG{CHLD} = 'DEFAULT'; setpgrp(0, 0); + my \$pid = fork; defined \$pid or exit 125; + if (!\$pid) { alarm(\$t + 1); exec { \$ARGV[0] } @ARGV; exit 127 } + \$SIG{ALRM} = sub { kill 'KILL', -\$\$ }; + alarm \$t; my \$w = waitpid(\$pid, 0); my \$st = \$?; alarm 0; + \$SIG{TERM} = 'IGNORE'; kill 'TERM', -\$\$; + exit(\$w < 0 ? 125 : (\$st & 127) ? 128 + (\$st & 127) : \$st >> 8)" 2) fi unset _ss_t diff --git a/lib/tests/session-digest.test.sh b/lib/tests/session-digest.test.sh index 9702bd5..e105d85 100644 --- a/lib/tests/session-digest.test.sh +++ b/lib/tests/session-digest.test.sh @@ -344,14 +344,30 @@ for c in bash git cat sed awk grep tail head cut dirname readlink wc tr \ done cat > "$WORK/sd9/lib/memory.sh" <<'EOF' #!/usr/bin/env bash +echo "memory: v1: x" >&2 +( sleep 8 ) & exec sleep 8 EOF p=$(mkproj sd19) SECONDS=0 -(cd "$p" && PATH="$bin" bash "$S9" /dev/null 2>&1) +out=$(cd "$p" && PATH="$bin" bash "$S9" &1) check SD19-no-timeout-bin "$(PATH="$bin" command -v timeout gtimeout \ | wc -l | tr -d ' ')" 0 check SD19-perl-bounds-hang "$([ "$SECONDS" -lt 6 ] && echo yes || echo no)" yes +check SD19-command-ran "$(printf '%s' "$out" | grep -c 'registry v1: 1 file(s)')" 1 + +# SD19b: a background child outlives the command (straggler holding stderr) +cat > "$WORK/sd9/lib/memory.sh" <<'EOF' +#!/usr/bin/env bash +echo "memory: v1: x" >&2 +( sleep 8 ) & +exit 0 +EOF +p=$(mkproj sd19b) +SECONDS=0 +out=$(cd "$p" && PATH="$bin" bash "$S9" &1) +check SD19b-perl-bounds-straggler "$([ "$SECONDS" -lt 6 ] && echo yes || echo no)" yes +check SD19b-command-ran "$(printf '%s' "$out" | grep -c 'registry v1: 1 file(s)')" 1 printf 'session-digest: %d passed, %d failed\n' "$pass" "$fail" [ "$fail" -eq 0 ]