Stop hook infinite loop: hook execution creates new task files that re-trigger the hook
Bug Description
The Stop hook enters an infinite loop when a background agent has been launched during a session. Each time the Stop hook fires, it creates a new task output file in the session's tasks directory. The hook then detects this new file on the next turn and incorrectly treats it as a running background subagent, triggering itself again indefinitely.
Steps to Reproduce
- Launch a background agent using
run_in_background: truein the Agent tool - Wait for the agent to complete
- End a turn (respond to user)
- The Stop hook fires and creates a new task file (e.g.
bq6p1t19a.output) - On the next turn end, the hook detects the new file and fires again
- This repeats indefinitely — each hook execution creates a new file that triggers the next execution
Expected Behavior
The Stop hook should only block turn completion if there are genuinely running (not completed) background tasks. Completed task files or files created by the hook itself should not be treated as running subagents.
Actual Behavior
The Stop hook fires on every single turn end with the message:
Background subagents are still running. Use `TaskOutput task_id="..." block=true` to wait for their results before ending your turn.
Each new task ID (e.g. bq6p1t19a, blozydgr3, biphfsy02, etc.) returns No task found with ID: ... when queried with TaskOutput block=true, confirming these are phantom/stale files, not real running tasks.
Environment
- Platform: Windows 11 Pro (10.0.22621)
- Shell: bash
- Claude Code version: 2.1.84
- Session tasks directory:
C:/Users/<user>/AppData/Local/Temp/claude/<project>/<session-id>/tasks/
Impact
The session becomes stuck in an infinite loop. The user must start a new session to continue working. The loop cannot be broken from within the session.
Workaround
Start a new session. The issue does not persist across sessions.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗