claude-code - 💡(How to fix) Fix Stop hook infinite loop: hook execution creates new task files that re-trigger the hook

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Fix Action

Workaround

Start a new session. The issue does not persist across sessions.

Code Example

Background subagents are still running. Use `TaskOutput task_id="..." block=true` to wait for their results before ending your turn.
RAW_BUFFERClick to expand / collapse

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

  1. Launch a background agent using run_in_background: true in the Agent tool
  2. Wait for the agent to complete
  3. End a turn (respond to user)
  4. The Stop hook fires and creates a new task file (e.g. bq6p1t19a.output)
  5. On the next turn end, the hook detects the new file and fires again
  6. 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.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Stop hook infinite loop: hook execution creates new task files that re-trigger the hook