#!/usr/bin/env bash # Run a command with a deadline; past it, SAMPLE what every process in its tree # is doing, print that into the log, kill the tree and exit 124. # # scripts/ci/with-deadline.sh [args...] # # WHY. On 29 Sep 2026 an `xctest` in the `swift` job sat for 25 minutes with no # CPU — its main thread in XCTWaiter waiting, without a timeout, on an async # test whose server restart had deadlocked inside SQLite — while holding the # machine-wide build lock, so every swift and ios check queued behind it. The # step's own ceiling (`BUILD_LOCK_MAX_MIN`, 50) would have ended it eventually, # but blind: a killed run that says nothing about where it hung is a hang that # comes back. `swift test` takes about 10 min of test time on a runner (run 5403, # 11 Oct 2026: 578 s, and the 10 min ceiling cut a healthy run), so the # workflows give it 20; that still bounds it in minutes and # leaves the stacks of the hang in the job log. # # The command runs in its own process group so the kill reaches everything it # started. `sample` (macOS) gives every thread's stack; elsewhere `ps` is all # that is printed. Exit status is the command's own when it finishes in time. set -uo pipefail MINUTES="${1-}" case "$MINUTES" in ''|*[!0-9]*) echo "with-deadline: the deadline wants a whole number of minutes, got '${MINUTES}'" >&2 echo "usage: with-deadline.sh [args...]" >&2 exit 2 ;; esac shift if [ "$#" -eq 0 ]; then echo "with-deadline: no command given" >&2 exit 2 fi # Seconds, overridable for the tests only. LIMIT="${WITH_DEADLINE_SECONDS:-$(( MINUTES * 60 ))}" POLICY="$(cd "$(dirname "${BASH_SOURCE[0]}")/../lib" && pwd)/mac_disk_policy.py" set -m "$@" & child=$! set +m descendants() { local root="$1" kids echo "$root" kids="$(pgrep -P "$root" 2>/dev/null || true)" for kid in $kids; do descendants "$kid"; done } # A stop from outside reaches the command too. It runs in a process group of # its own, so a build lock's yield, a runner's cancel or an outer deadline # (the Mac steps put this inside both locks, around a build that runs its own # `with-deadline.sh 20 swift test`) would otherwise stop only this script and # leave the compile running, unowned, after the locks were let go. stop() { kill -TERM -- "-$child" 2>/dev/null || kill -TERM "$child" 2>/dev/null sleep 5 kill -KILL -- "-$child" 2>/dev/null || true wait "$child" 2>/dev/null exit "$1" } trap 'stop 143' TERM trap 'stop 130' INT trap 'stop 129' HUP waited=0 while kill -0 "$child" 2>/dev/null; do if [ "$waited" -ge "$LIMIT" ]; then echo "::error::with-deadline: '$*' ran past ${MINUTES} min — sampling its processes, then stopping it (exit 124)" >&2 for pid in $(descendants "$child"); do # Read in-process, never through the setuid `ps` a lane's seatbelt refuses. echo "----- pid $pid: $(python3 "$POLICY" --process-info args "$pid" 2>/dev/null | cut -c1-200)" >&2 if command -v sample >/dev/null 2>&1; then # The call-graph section only: every thread's stack, heaviest first. sample "$pid" 3 2>/dev/null | sed -n '/^Call graph:/,/^Total number in stack/p' | head -400 >&2 || true fi done trap - TERM INT HUP stop 124 fi sleep 1 waited=$(( waited + 1 )) done wait "$child"