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.
This commit is contained in:
bchanot committed 2026-11-14 11:27:15 +01:00
1 parent cce1a1304c
commit 490ad8353d
2 files changed
+32 -3

No files matched your search

+15 -2
View File
@@ -37,13 +37,26 @@ elif [ -t 0 ]; then
fi fi
# ── Bounded runner: timeout, else gtimeout, else perl alarm (stock macOS) ── # ── 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=() _SS_TMO=()
for _ss_t in timeout gtimeout; do for _ss_t in timeout gtimeout; do
command -v "$_ss_t" >/dev/null 2>&1 && { _SS_TMO=("$_ss_t" 2); break; } command -v "$_ss_t" >/dev/null 2>&1 && { _SS_TMO=("$_ss_t" 2); break; }
done done
if [ ${#_SS_TMO[@]} -eq 0 ] && command -v perl >/dev/null 2>&1; then 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 fi
unset _ss_t unset _ss_t
+17 -1
View File
@@ -344,14 +344,30 @@ for c in bash git cat sed awk grep tail head cut dirname readlink wc tr \
done done
cat > "$WORK/sd9/lib/memory.sh" <<'EOF' cat > "$WORK/sd9/lib/memory.sh" <<'EOF'
#!/usr/bin/env bash #!/usr/bin/env bash
echo "memory: v1: x" >&2
( sleep 8 ) &
exec sleep 8 exec sleep 8
EOF EOF
p=$(mkproj sd19) p=$(mkproj sd19)
SECONDS=0 SECONDS=0
(cd "$p" && PATH="$bin" bash "$S9" </dev/null >/dev/null 2>&1) out=$(cd "$p" && PATH="$bin" bash "$S9" </dev/null 2>&1)
check SD19-no-timeout-bin "$(PATH="$bin" command -v timeout gtimeout \ check SD19-no-timeout-bin "$(PATH="$bin" command -v timeout gtimeout \
| wc -l | tr -d ' ')" 0 | wc -l | tr -d ' ')" 0
check SD19-perl-bounds-hang "$([ "$SECONDS" -lt 6 ] && echo yes || echo no)" yes 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" </dev/null 2>&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" printf 'session-digest: %d passed, %d failed\n' "$pass" "$fail"
[ "$fail" -eq 0 ] [ "$fail" -eq 0 ]