Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

ci(devex): wrap pytest steps with signal-fanout for fast cancel (#60203)

Julian Bez7550381819bc
diff --git a/.github/scripts/signal-fanout b/.github/scripts/signal-fanout
new file mode 100755
index 000000000000..7013a76948a1
--- /dev/null
+++ b/.github/scripts/signal-fanout
@@ -0,0 +1,57 @@
+#!/bin/bash
+# Vendored from dimikot/signal-fanout @ 585118209dec (2024-04-11).
+# Source: https://github.com/dimikot/signal-fanout
+#
+# Used as a `shell:` wrapper in CI to propagate the runner's cancellation
+# signal to the entire process tree. GitHub Actions only signals the step's
+# top-level process; bash swallows that signal while waiting for a child, so
+# pytest never sees it and only dies when SIGKILL hits the tree ~10s later
+# (or sometimes not at all on hosted runners with detached descendants).
+# This wrapper traps the runner's signal and pkill -SIGTERMs the whole
+# session, so pytest exits immediately on cancel.
+set -e -o pipefail
+
+# The signals we're trapping.
+TRAP_SIGNALS=(SIGHUP SIGINT SIGQUIT SIGUSR1 SIGUSR2 SIGPIPE SIGTERM)
+
+# When receiving one of the above signals, send $KILL_SIGNAL to the entire
+# process tree. We must NOT use SIGINT here (and we also must not propagate
+# the original signal since it may be SIGINT) due to how shells (e.g. bash)
+# handle SIGINT. See https://www.cons.org/cracauer/sigint.html
+KILL_SIGNAL=SIGTERM
+
+if [[ "$1" == "--inner" ]]; then
+ shift
+
+ handler() {
+ signal="$1"
+ trap - "$KILL_SIGNAL"
+ echo "$(date): signal-fanout received $signal, killing processes of the current session with $KILL_SIGNAL:"
+ pids="$(pgrep -s 0 || true)"
+ if [[ -n "$pids" ]]; then ps -o pid,sess,pgid,command -p $pids || true; fi
+ pkill "-$KILL_SIGNAL" -s 0 || true
+ }
+ for signal in "${TRAP_SIGNALS[@]}"; do
+ # shellcheck disable=SC2064
+ trap "handler $signal" "$signal"
+ done
+
+ if [[ -x "$1" ]]; then
+ # Full executable name is passed, e.g. "signal-fanout /bin/zsh {0}".
+ "$@" & wait $!
+ elif [[ ! -f "$1" ]] && which "$1" &>/dev/null; then
+ # Shell name is passed, e.g. "signal-fanout zsh {0}".
+ "$@" & wait $!
+ else
+ # Just a script file name is passed, e.g. "signal-fanout {0}".
+ bash "$@" & wait $!
+ fi
+else
+ if [[ "$1" == "" ]]; then
+ echo "Usage examples:"
+ echo " - signal-fanout script-path"
+ echo " - signal-fanout zsh script-path"
+ exit 1
+ fi
+ exec setsid --wait "${BASH_SOURCE[0]}" --inner "$@"
+fi
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index f391a7b680ae..0fd77ef9d726 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -1707,6 +1707,15 @@ jobs:
ref: ${{ github.event.pull_request.head.ref }}
lfs: true
clean: false
+
+ # Put signal-fanout on PATH so the pytest steps below can use it as
+ # their `shell:` wrapper. GHA's `shell:` field doesn't allow
+ # ${{ github.workspace }} expansion, so a plain name on PATH is the
+ # only portable way to reference it.
+ - name: Install signal-fanout
+ shell: bash
+ run: sudo install -m 0755 .github/scripts/signal-fanout /usr/local/bin/signal-fanout
+
- name: Clean up data directories with container permissions
run: |
# Use docker to clean up files created by containers
@@ -1941,7 +1950,12 @@ jobs:
if: ${{ needs.changes.outputs.backend == 'true' && matrix.segment == 'Core' }}
env:
PERSON_ON_EVENTS_V2_ENABLED: ${{ matrix.person-on-events && 'true' || 'false' }}
- shell: bash
+ # Wrap bash with signal-fanout so the runner's cancel signal is
+ # propagated to the entire process tree (incl. pytest). Without
+ # this, GHA only signals the top-level bash and pytest survives
+ # until the runner's 10s SIGKILL — which sometimes misses
+ # detached descendants entirely. Installed earlier on PATH.
+ shell: 'signal-fanout bash --noprofile --norc -eo pipefail {0}'
run: | # async_migrations covered in ci-async-migrations.yml
set +e
pytest -v --tb=short --reuse-db -o junit_duration_report=call ${{
@@ -2005,7 +2019,8 @@ jobs:
- name: Run Temporal tests
id: run-temporal-tests
if: ${{ needs.changes.outputs.backend == 'true' && matrix.segment == 'Temporal' }}
- shell: bash
+ # See "Run Core tests" for why signal-fanout wraps bash here.
+ shell: 'signal-fanout bash --noprofile --norc -eo pipefail {0}'
env:
AWS_S3_ALLOW_UNSAFE_RENAME: 'true'
MODAL_TOKEN_ID: ${{ needs.changes.outputs.tasks_temporal == 'true' && secrets.MODAL_TOKEN_ID || '' }}