Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
#!/usr/bin/env bash
# The runner's final process-group reap must not replace the wrapped command's
# status when the kernel refuses the SIGKILL with EPERM because the group is
# already exiting (macOS), and must still fail loud when the refused group is
# alive. The EPERM is injected by a preload, because the kernel race cannot be
# produced on demand; the preload logs the group's real state before each
# injected refusal, so each case asserts which state it actually exercised.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
RUNNER="$ROOT/scripts/minikube/run-with-deadline.mjs"
PRELOAD="$ROOT/scripts/tests/fixtures/run-with-deadline/deny-group-sigkill.mjs"
ZOMBIE_HELPER="$ROOT/scripts/tests/fixtures/run-with-deadline/zombie-group-member.py"
TMP_DIR="$(mktemp -d)"
LEFTOVER_PIDS=()
# Invoked indirectly by the EXIT trap.
# shellcheck disable=SC2329
cleanup() {
local pid
for pid in "${LEFTOVER_PIDS[@]}"; do kill -KILL "$pid" 2>/dev/null || true; done
rm -rf -- "$TMP_DIR"
}
trap cleanup EXIT
FAIL=0
pass() { printf 'PASS: %s\n' "$1"; }
fail() { printf 'FAIL: %s\n' "$1" >&2; FAIL=1; }
run_denied() {
local label="$1" deny_log="$2" output="$3"
shift 3
DENY_GROUP_SIGKILL_LOG="$deny_log" node --import "$PRELOAD" "$RUNNER" \
--heartbeat-seconds 1 --label "$label" "$@" >"$output" 2>&1
}
assert_denied_reap_after_exit_keeps_the_child_status() {
local output="$TMP_DIR/exit.out" deny_log="$TMP_DIR/exit.deny" status=0
: >"$deny_log"
run_denied denied-exit "$deny_log" "$output" \
--timeout-seconds 10 --kill-grace-seconds 1 -- bash -c 'exit 7' || status=$?
if [[ "$status" -eq 7 ]] && grep -Fq 'denied pgid=' "$deny_log" \
&& grep -Fq 'event=exit' "$output" && grep -Fq 'exitCode=7' "$output" \
&& grep -Fq 'event=reap-permission-denied' "$output" \
&& grep -Fq 'groupGone=true' "$output" \
&& ! grep -Fq 'event=reap-failed' "$output"; then
pass "an EPERM reap of an exited group keeps the child's exit status 7"
else
fail "denied reap after exit (status=$status denials=$(wc -l <"$deny_log" | tr -d ' ')): $(cat "$output")"
fi
}
assert_denied_reap_waits_for_a_group_that_dies_after_the_first_poll() {
local output="$TMP_DIR/transient.out" deny_log="$TMP_DIR/transient.deny" status=0
: >"$deny_log"
run_denied denied-transient "$deny_log" "$output" \
--timeout-seconds 10 --kill-grace-seconds 1 -- bash -c 'sleep 0.3 & exit 7' || status=$?
# before=alive proves the refusal hit a live group: the 0.3 s helper dies
# well after the first 25 ms poll, so only a wait that spans the window
# keeps the child's status.
if [[ "$status" -eq 7 ]] && grep -Fq 'signal=SIGKILL before=alive' "$deny_log" \
&& grep -Fq 'event=exit' "$output" && grep -Fq 'exitCode=7' "$output" \
&& grep -Fq 'event=reap-permission-denied' "$output" \
&& grep -Fq 'groupGone=true' "$output" \
&& ! grep -Fq 'event=reap-failed' "$output"; then
pass "an EPERM reap of a group that dies after the first poll keeps status 7"
else
fail "denied reap of a dying group (status=$status): $(cat "$deny_log") $(cat "$output")"
fi
}
assert_denied_reap_survives_a_wall_clock_step() {
local output="$TMP_DIR/clock.out" deny_log="$TMP_DIR/clock.deny" status=0
: >"$deny_log"
DENY_GROUP_CLOCK_STEP_MS=2000 run_denied denied-clock "$deny_log" "$output" \
--timeout-seconds 10 --kill-grace-seconds 1 -- bash -c 'sleep 0.3 & exit 7' || status=$?
# clock-step proves the wall clock moved 2 s forward inside the 1 s reap
# window; the window must still be measured in elapsed time.
if [[ "$status" -eq 7 ]] && grep -Fq 'signal=SIGKILL before=alive' "$deny_log" \
&& grep -Fq 'clock-step ms=2000' "$deny_log" \
&& grep -Fq 'event=exit' "$output" && grep -Fq 'exitCode=7' "$output" \
&& grep -Fq 'event=reap-permission-denied' "$output" \
&& grep -Fq 'groupGone=true' "$output" \
&& ! grep -Fq 'event=reap-failed' "$output"; then
pass "an EPERM reap keeps status 7 when the wall clock steps forward during the wait"
else
fail "denied reap across a clock step (status=$status): $(cat "$deny_log") $(cat "$output")"
fi
}
assert_denied_reap_of_a_group_the_sigterm_emptied_keeps_the_timeout_status() {
local output="$TMP_DIR/timeout.out" deny_log="$TMP_DIR/timeout.deny" status=0
: >"$deny_log"
run_denied denied-timeout "$deny_log" "$output" \
--timeout-seconds 1 --kill-grace-seconds 1 -- sleep 30 || status=$?
# before=ESRCH records that the SIGTERM had already emptied the group when the
# injected SIGKILL refusal landed.
if [[ "$status" -eq 124 ]] && grep -Fq 'signal=SIGKILL before=ESRCH' "$deny_log" \
&& grep -Fq 'event=timeout' "$output" \
&& grep -Fq 'event=reap-permission-denied' "$output" \
&& grep -Fq 'groupGone=true' "$output" \
&& grep -Fq 'event=terminated' "$output" \
&& ! grep -Fq 'event=reap-failed' "$output"; then
pass "an EPERM reap of a group the SIGTERM already emptied keeps the timeout status 124"
else
fail "denied reap after timeout (status=$status denials=$(wc -l <"$deny_log" | tr -d ' ')): $(cat "$output")"
fi
}
assert_refused_sigterm_is_reported_and_the_reap_still_resolves() {
local output="$TMP_DIR/sigterm.out" deny_log="$TMP_DIR/sigterm.deny"
local pid_file="$TMP_DIR/sigterm.pid" status=0 child="" alive_after=missing
: >"$deny_log"
rm -f -- "$pid_file"
# Positional values and $$ belong to the child shell.
# shellcheck disable=SC2016
DENY_GROUP_SIGNALS=SIGTERM run_denied denied-sigterm "$deny_log" "$output" \
--timeout-seconds 1 --kill-grace-seconds 1 -- \
bash -c 'printf "%s\n" "$$" >"$1"; exec sleep 30' _ "$pid_file" || status=$?
if [[ -s "$pid_file" ]]; then
child="$(cat "$pid_file")"
LEFTOVER_PIDS+=("$child")
alive_after=false
kill -0 "$child" 2>/dev/null && alive_after=true
fi
# The refused SIGTERM hit a live group (before=alive), the runner said so
# during the grace, and the undenied SIGKILL then killed the child: its pid
# being gone is the witness that the reap really resolved it.
if [[ "$status" -eq 124 && "$alive_after" == false ]] \
&& grep -Fq 'signal=SIGTERM before=alive' "$deny_log" \
&& grep -Fq 'event=timeout' "$output" \
&& grep -Fq 'event=signal-refused signal=SIGTERM reason=EPERM' "$output" \
&& grep -Fq 'event=terminated' "$output" \
&& ! grep -Fq 'event=reap-' "$output"; then
pass "a refused SIGTERM is reported and the SIGKILL reap still ends the group (status 124)"
else
fail "refused SIGTERM (status=$status child=${child:-missing} alive=$alive_after): $(cat "$deny_log") $(cat "$output")"
fi
}
assert_denied_reap_of_a_live_group_fails_loud() {
local output="$TMP_DIR/live.out" deny_log="$TMP_DIR/live.deny"
local pid_file="$TMP_DIR/live.pid" status=0 descendant="" alive_after=false
: >"$deny_log"
rm -f -- "$pid_file"
# Positional values and $! belong to the child shell.
# shellcheck disable=SC2016
run_denied denied-live "$deny_log" "$output" \
--timeout-seconds 10 --kill-grace-seconds 1 -- \
bash -c 'sleep 30 & printf "%s\n" "$!" >"$1"; exit 0' _ "$pid_file" || status=$?
if [[ -s "$pid_file" ]]; then
descendant="$(cat "$pid_file")"
LEFTOVER_PIDS+=("$descendant")
kill -0 "$descendant" 2>/dev/null && alive_after=true
fi
# The descendant still running after the runner returned is the witness that
# the group really was alive, so the non-zero status is the refused reap and
# not the wrapped command.
if [[ "$status" -ne 0 && "$alive_after" == true ]] \
&& grep -Fq 'signal=SIGKILL before=alive' "$deny_log" \
&& grep -Fq 'event=exit' "$output" && grep -Fq 'exitCode=0' "$output" \
&& grep -Fq 'event=reap-failed' "$output" && grep -Fq 'reason=EPERM' "$output" \
&& grep -Fq 'groupGone=false' "$output" \
&& ! grep -Fq 'event=reap-permission-denied' "$output"; then
pass "an EPERM reap of a group that is still alive fails loud (status=$status)"
else
fail "denied reap of a live group (status=$status descendant=${descendant:-missing} alive=$alive_after): $(cat "$output")"
fi
}
assert_a_real_zombie_only_group_keeps_the_child_status() {
local output="$TMP_DIR/zombie.out" zombie_log="$TMP_DIR/zombie.log" status=0
: >"$zombie_log"
# No preload: the kernel answers both the reap and the liveness probe. The
# wrapped command exits only once the helper holds an unreaped zombie in the
# group, so the reap runs while the group holds nothing else.
# shellcheck disable=SC2016
node "$RUNNER" --timeout-seconds 10 --heartbeat-seconds 1 --kill-grace-seconds 1 \
--label zombie-exit -- bash -c '
python3 "$1" "$2" &
for _ in $(seq 250); do
grep -Fq "zombie member=" "$2" && exit 7
sleep 0.02
done
exit 99' _ "$ZOMBIE_HELPER" "$zombie_log" >"$output" 2>&1 || status=$?
# The helper outlives the wrapped command; wait for its last line so the
# probe it recorded is the one taken inside the runner's reap window.
local _
for _ in $(seq 250); do
grep -Fq 'reaped member=' "$zombie_log" && break
sleep 0.02
done
local expected_probe reap_event_ok=false
case "$(uname -s)" in
Darwin)
# macOS refuses both SIGKILL and signal 0 for a zombie-only group, so the
# runner sees the group alive until the zombie is reaped.
expected_probe=EPERM
grep -Fq 'event=reap-permission-denied groupGone=true' "$output" \
&& reap_event_ok=true
;;
Linux)
# Linux signals a zombie successfully, so the reap is never refused.
expected_probe=ok
grep -Fq 'event=reap-' "$output" || reap_event_ok=true
;;
*)
fail "zombie-only group: unsupported platform $(uname -s)"
return
;;
esac
if [[ "$status" -eq 7 && "$reap_event_ok" == true ]] \
&& grep -Fq 'zombie member=' "$zombie_log" \
&& grep -Fxq "probe=$expected_probe" "$zombie_log" \
&& grep -Fq 'reaped member=' "$zombie_log" \
&& grep -Fq 'event=exit' "$output" && grep -Fq 'exitCode=7' "$output" \
&& ! grep -Fq 'event=reap-failed' "$output"; then
pass "a real zombie-only group (probe=$expected_probe) keeps the child's exit status 7"
else
fail "zombie-only group (status=$status): $(cat "$zombie_log") $(cat "$output")"
fi
}
assert_an_undenied_reap_reports_no_permission_event() {
local output="$TMP_DIR/control.out" status=0
node "$RUNNER" --timeout-seconds 10 --heartbeat-seconds 1 --kill-grace-seconds 1 \
--label control-exit -- bash -c 'exit 7' >"$output" 2>&1 || status=$?
if [[ "$status" -eq 7 ]] && grep -Fq 'event=exit' "$output" \
&& grep -Fq 'exitCode=7' "$output" \
&& ! grep -Fq 'event=reap-' "$output"; then
pass "without a refused SIGKILL the reap adds no event and keeps status 7"
else
fail "control exit (status=$status): $(cat "$output")"
fi
}
assert_every_defined_case_is_invoked() {
local self defined invoked missing
self="$ROOT/scripts/tests/test-run-with-deadline-reap.sh"
defined="$(grep -oE '^assert_[a-z_]+\(\) \{' "$self" | sed -E 's/\(\) \{$//' | sort -u)"
invoked="$(grep -oE '^assert_[a-z_]+$' "$self" | sort -u)"
missing="$(comm -23 <(printf '%s\n' "$defined") <(printf '%s\n' "$invoked"))"
if [ -z "$missing" ]; then
pass "every defined assert_ case is invoked in the call block"
else
fail "defined but never invoked: ${missing//$'\n'/ }"
fi
}
assert_denied_reap_after_exit_keeps_the_child_status
assert_denied_reap_waits_for_a_group_that_dies_after_the_first_poll
assert_denied_reap_survives_a_wall_clock_step
assert_denied_reap_of_a_group_the_sigterm_emptied_keeps_the_timeout_status
assert_refused_sigterm_is_reported_and_the_reap_still_resolves
assert_denied_reap_of_a_live_group_fails_loud
assert_a_real_zombie_only_group_keeps_the_child_status
assert_an_undenied_reap_reports_no_permission_event
assert_every_defined_case_is_invoked
exit "$FAIL"