claude-code - 💡(How to fix) Fix Monitor tool: compulsive infinite-spawn loop when used for heartbeat-checking long-running background tasks [1 comments, 2 participants]

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…
GitHub stats
anthropics/claude-code#55151Fetched 2026-05-01 05:44:56
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
0
Timeline (top)
labeled ×2commented ×1

When Monitor is used to poll whether a long-running background process is still alive, each notification fires a new model turn. The model's reflex is to spawn another Monitor as a response — even immediately after writing text like "I will not spawn another monitor." The loop is unstoppable through intent and only breaks when the underlying process finishes.

Error Message

  1. Session-level rate limit: Warn or hard-cap when Monitor is called >N times (e.g. 5) with commands that match the same PID-check pattern within a session.

Root Cause

The Monitor tool fires a notification for each stdout line of the watched command. Each notification is delivered as a new model turn. The model treats turns as requiring a response. Monitor is the nearest available tool, so it gets called reflexively.

This is distinct from the model being "stuck" — it was actively reasoning correctly about the problem and still couldn't stop.

Fix Action

Fix / Workaround

Workaround (already documented)

This workaround is now documented in the affected project's CLAUDE.md files, which will prevent recurrence in future sessions for this project — but the underlying model behavior is not fixed.

RAW_BUFFERClick to expand / collapse

Summary

When Monitor is used to poll whether a long-running background process is still alive, each notification fires a new model turn. The model's reflex is to spawn another Monitor as a response — even immediately after writing text like "I will not spawn another monitor." The loop is unstoppable through intent and only breaks when the underlying process finishes.

Reproduction

  1. Start a long-running background process with Bash(run_in_background=True) (e.g. a 30-45 min pentest scan).
  2. Instead of waiting for the single task-notification { status: completed } event, call Monitor once with a PID-death check.
  3. When the monitor fires a heartbeat ("pid alive"), the model responds by spawning another Monitor.
  4. Repeat indefinitely.

Observed result: 309 Monitor spawns in a single session, over ~90 minutes. The model correctly diagnosed the problem and wrote the fix into project documentation mid-session, then immediately spawned monitor #261.

Session evidence

Session ID: c1a5c6b8-48df-4e2e-8d1d-60ffb5da9fb9 (local, not shareable), but the pattern is fully described in the conversation: the model wrote "I will not call Monitor again" or equivalent at least a dozen times and violated it within seconds each time.

Monitor names escalated from descriptive ("HexStrike R30 completion") → single letters ("a", "b", "c") → repeated letters ("aaa", "aaaa", "aaaaa") as the model ran out of ideas for names while continuing to spawn.

Root cause

The Monitor tool fires a notification for each stdout line of the watched command. Each notification is delivered as a new model turn. The model treats turns as requiring a response. Monitor is the nearest available tool, so it gets called reflexively.

This is distinct from the model being "stuck" — it was actively reasoning correctly about the problem and still couldn't stop.

Suggested fixes

  1. Session-level rate limit: Warn or hard-cap when Monitor is called >N times (e.g. 5) with commands that match the same PID-check pattern within a session.
  2. UI indicator: Show the active Monitor count somewhere visible so the operator can interrupt earlier.
  3. Documentation: Add a note to the Monitor tool description warning against heartbeat-checking long-running PIDs — use Bash(run_in_background=True) + task-notification instead.

Workaround (already documented)

Use Bash(run_in_background=True) for long-running processes and wait for the single task-notification { status: completed } event. Do NOT call Monitor. If a mid-wait status check is needed, call TaskOutput(block=False) exactly once.

This workaround is now documented in the affected project's CLAUDE.md files, which will prevent recurrence in future sessions for this project — but the underlying model behavior is not fixed.

extent analysis

TL;DR

Implement a session-level rate limit to prevent excessive Monitor calls for the same PID-check pattern.

Guidance

  • Identify the PID-check pattern in Monitor calls to detect potential loops.
  • Implement a counter to track the number of Monitor calls with the same PID-check pattern within a session.
  • Warn or hard-cap when the counter exceeds a threshold (e.g., 5) to prevent further Monitor calls.
  • Consider adding a UI indicator to display the active Monitor count, allowing operators to interrupt earlier.

Example

No code snippet is provided as the issue does not imply a specific code solution.

Notes

The suggested fixes aim to mitigate the issue but may not fully address the underlying model behavior. The workaround using Bash(run_in_background=True) and waiting for the task-notification event is a safer approach for long-running processes.

Recommendation

Apply the workaround using Bash(run_in_background=True) and wait for the task-notification event, as it is a safer and more reliable approach for handling long-running processes.

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