A Claude Code SessionStart hook that prevents subagents from freezing indefinitely by monitoring child processes and terminating any that exceed a configurable timeout.
Subagents can freeze during execution with no timeout, no error, and no recovery path, requiring manual Ctrl+C to recover the entire session (#37521).
- A
SessionStarthook launchesagent-timeout.shwhen a Claude Code session begins. - The script spawns a background monitor that tracks direct child processes of the session.
- Each child's first-seen time is recorded in a temp directory (
$TMPDIR/agent-timeout-<pid>/). - If any child process exceeds
AGENT_TIMEOUTseconds, the monitor sendsSIGTERM. - The monitor exits automatically when the parent session ends.
# Requires: jq
bash install.shThis adds a SessionStart hook to ~/.claude/settings.json. The hook is idempotent — running install.sh again is a no-op if already installed.
Set environment variables in your shell profile (.bashrc, .zshrc, etc.):
| Variable | Default | Description |
|---|---|---|
AGENT_TIMEOUT |
300 |
Seconds before a child process is killed |
POLL_INTERVAL |
10 |
Seconds between monitoring checks |
Example:
export AGENT_TIMEOUT=600 # 10-minute timeout
export POLL_INTERVAL=5 # check every 5 secondsRemove the SessionStart entry referencing agent-timeout from ~/.claude/settings.json.
make test # syntax check
make lint # shellcheck