← Back to Pull requests

fix(query): stop a full message buffer from blocking control requests #1385

Open
unknown opened this pull request with 449 lines diff across 5 files
View on GitHub
unknown unknown commented on Oct 14, 2026 13:35 UTC
Author

Draft because this changes a trade-off rather than fixing a plain bug, so it needs a maintainer's call on the design (see below).

Summary


Query._read_messages does two jobs in one loop: it buffers messages for the consumer, and it routes the control frames the CLI blocks on (hook callbacks, permission requests, SDK MCP calls). The buffer was capped at 100, so once a consumer stopped reading (for example receive_response() returned at the first result while background subagents kept streaming) the loop blocked on send(), and those requests went unanswered until something read again.

Reproduced with a scripted transport that emits N stream_event messages and then a control_request, with no consumer reading: with N = 50 or 99 the request is answered; with N = 150 it is not, until a consumer starts draining.

This PR:

  • makes the buffer unbounded so the loop never waits on the consumer, and updates the comments that described the buffer-full case;

  • logs one warning when 10,000 unread messages are buffered, using the stream's own statistics().current_buffer_used (about 0.2 microseconds per message until it fires), so a consumer that stopped reading shows up in the log instead of as a silent memory leak. It fires once per Query and does not re-arm;

  • adds a note to ClaudeSDKClient.receive_response() saying that messages arriving after the ResultMessage stay buffered in the process until they are read or the client disconnects.
  • The warning and the docs note came out of the review below.

    Refs #425 (same cause; that report ties it to SDK MCP tools going missing under background subagents, which this PR does not reproduce end to end, only the stalled control channel). Related: #558 and the older #572, which makes the same one-line change; this PR is rebased on current main and adds a test that a control request is answered while the consumer is idle.

    The trade-off (why this is a draft)


    A consumer that never reads now lets the backlog grow in memory instead of pushing back on the CLI's stdout. With an idle consumer and a synthetic transport streaming flat out, I measured about 0.6 KB per buffered message (roughly 126 MB for about 206,000 messages in 3 seconds); on main it stops at 101 messages. Real CLI output is far slower than that. The warning above makes the growth visible, but it does not bound it.

    Still open for a maintainer to decide:

  • A cap. One option is to drop only stream_event messages from background subagents (parent_tool_use_id set) once the backlog passes a threshold. I have not measured the real message mix from a live CLI with background subagents, so I did not add it.

  • A configurable size. ClaudeAgentOptions.max_buffer_size already means bytes of CLI stdout for the JSON framer, so a new option would need a different name (for example one that says "pending messages").

  • Draining into a separate queue instead of buffering in the stream.
  • I kept this PR to the smallest change plus visibility; happy to rework it to whichever you prefer.

    Test plan


  • Added TestReaderNotBlockedByIdleConsumer (asyncio and trio): a control request behind 250 unread messages is answered while nobody reads (fails on main with a timeout); the backlog is delivered in order once the consumer resumes (guard); the warning fires once when the backlog reaches the threshold, not below it, and exactly at the boundary (checked by mutating the comparison and the once-flag; each mutant fails a test).

  • ruff check, ruff format --check, mypy src/ scripts/, pytest tests/ (1615 passed, 6 skipped).
  • Diff: 449 lines (+336 / -113) across 5 files Reviewer: tonydzi Review duration: 2817m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 449 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.366
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1385"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.4% (MEDIUM) · Higher = larger blast radius
    45.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.2% · Safe to merge
    24.2%