fix: fix flock/binding issues with claude_run_with_watchdog
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/pr/ci Pipeline was successful

This commit is contained in:
Claude 2026-04-10 17:41:39 +00:00
parent ec5eb48224
commit d6f93bb8f5
2 changed files with 12 additions and 3 deletions

View file

@ -104,6 +104,14 @@ claude_run_with_watchdog() {
kill "$grace_pid" 2>/dev/null || true kill "$grace_pid" 2>/dev/null || true
wait "$grace_pid" 2>/dev/null || true wait "$grace_pid" 2>/dev/null || true
# When timeout fires (rc=124), explicitly kill the orphaned claude process
# tail --pid is a passive waiter, not a supervisor
if [ "$rc" -eq 124 ]; then
kill "$pid" 2>/dev/null || true
sleep 1
kill -KILL "$pid" 2>/dev/null || true
fi
# Output the captured stdout # Output the captured stdout
cat "$out_file" cat "$out_file"
return "$rc" return "$rc"
@ -132,7 +140,8 @@ agent_run() {
mkdir -p "$(dirname "$lock_file")" mkdir -p "$(dirname "$lock_file")"
local output rc local output rc
log "agent_run: starting (resume=${resume_id:-(new)}, dir=${run_dir})" log "agent_run: starting (resume=${resume_id:-(new)}, dir=${run_dir})"
output=$(cd "$run_dir" && flock -w 600 "$lock_file" claude_run_with_watchdog claude "${args[@]}" 2>>"$LOGFILE") && rc=0 || rc=$? # Acquire lock separately (flock cannot exec bash functions)
output=$(cd "$run_dir" && ( flock -w 600 "$lock_file" || exit 1; claude_run_with_watchdog claude "${args[@]}" ) 2>>"$LOGFILE") && rc=0 || rc=$?
if [ "$rc" -eq 124 ]; then if [ "$rc" -eq 124 ]; then
log "agent_run: timeout after ${CLAUDE_TIMEOUT:-7200}s (exit code $rc)" log "agent_run: timeout after ${CLAUDE_TIMEOUT:-7200}s (exit code $rc)"
elif [ "$rc" -ne 0 ]; then elif [ "$rc" -ne 0 ]; then
@ -173,7 +182,7 @@ agent_run() {
local nudge="You stopped but did not push any code. You have uncommitted changes. Commit them and push." local nudge="You stopped but did not push any code. You have uncommitted changes. Commit them and push."
log "agent_run: nudging (uncommitted changes)" log "agent_run: nudging (uncommitted changes)"
local nudge_rc local nudge_rc
output=$(cd "$run_dir" && flock -w 600 "$lock_file" claude_run_with_watchdog claude -p "$nudge" --resume "$_AGENT_SESSION_ID" --output-format json --dangerously-skip-permissions --max-turns 50 ${CLAUDE_MODEL:+--model "$CLAUDE_MODEL"} 2>>"$LOGFILE") && nudge_rc=0 || nudge_rc=$? output=$(cd "$run_dir" && ( flock -w 600 "$lock_file" || exit 1; claude_run_with_watchdog claude -p "$nudge" --resume "$_AGENT_SESSION_ID" --output-format json --dangerously-skip-permissions --max-turns 50 ${CLAUDE_MODEL:+--model "$CLAUDE_MODEL"} ) 2>>"$LOGFILE") && nudge_rc=0 || nudge_rc=$?
if [ "$nudge_rc" -eq 124 ]; then if [ "$nudge_rc" -eq 124 ]; then
log "agent_run: nudge timeout after ${CLAUDE_TIMEOUT:-7200}s (exit code $nudge_rc)" log "agent_run: nudge timeout after ${CLAUDE_TIMEOUT:-7200}s (exit code $nudge_rc)"
elif [ "$nudge_rc" -ne 0 ]; then elif [ "$nudge_rc" -ne 0 ]; then

View file

@ -31,7 +31,7 @@
# Requires: lib/env.sh, lib/worktree.sh, lib/agent-sdk.sh sourced first for shared helpers. # Requires: lib/env.sh, lib/worktree.sh, lib/agent-sdk.sh sourced first for shared helpers.
# Source agent-sdk for claude_run_with_watchdog watchdog helper # Source agent-sdk for claude_run_with_watchdog watchdog helper
source "$(dirname "$0")/agent-sdk.sh" source "$(dirname "${BASH_SOURCE[0]}")/agent-sdk.sh"
# ── Run guards ─────────────────────────────────────────────────────────── # ── Run guards ───────────────────────────────────────────────────────────