fix: claude subprocess not killed on INT/TERM in cleanup trap (#530)

Track CLAUDE_PID before launching the claude subprocess so cleanup()
can kill it before reverting Anvil state. Running claude via `&` +
`wait` lets the trap fire immediately on INT/TERM, killing the
subprocess and preventing it from making calls against an
already-reverted chain.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
openhands 2026-03-13 09:48:34 +00:00
parent 8b1d1ce146
commit 659044e2d1

View file

@ -180,8 +180,12 @@ SNAP=$("$CAST" rpc anvil_snapshot --rpc-url "$RPC_URL" | tr -d '"')
log " Snapshot ID: $SNAP"
# Revert to the baseline snapshot on exit so subsequent runs start clean.
CLAUDE_PID=""
cleanup() {
local rc=$?
if [[ -n "${CLAUDE_PID:-}" ]]; then
kill "$CLAUDE_PID" 2>/dev/null || true
fi
if [[ -n "${SNAP:-}" ]]; then
"$CAST" rpc anvil_revert "$SNAP" --rpc-url "$RPC_URL" >/dev/null 2>&1 || true
fi
@ -661,8 +665,11 @@ set +e
# omitting it causes the CLI to exit with an error, producing an empty stream log.
timeout "$CLAUDE_TIMEOUT" claude -p --dangerously-skip-permissions \
--verbose --output-format stream-json \
"$PROMPT" >"$STREAM_LOG" 2>&1
"$PROMPT" >"$STREAM_LOG" 2>&1 &
CLAUDE_PID=$!
wait "$CLAUDE_PID"
AGENT_EXIT=$?
CLAUDE_PID=""
set -e
if [[ $AGENT_EXIT -ne 0 ]]; then