fix(query): stop a full message buffer from blocking control requests #1385
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
tonydzi
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_messagesdoes 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 examplereceive_response()returned at the first result while background subagents kept streaming) the loop blocked onsend(), and those requests went unanswered until something read again.Reproduced with a scripted transport that emits N
stream_eventmessages and then acontrol_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:
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 perQueryand does not re-arm;ClaudeSDKClient.receive_response()saying that messages arriving after theResultMessagestay 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
mainand 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
mainit 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:
stream_eventmessages from background subagents (parent_tool_use_idset) 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.ClaudeAgentOptions.max_buffer_sizealready 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").I kept this PR to the smallest change plus visibility; happy to rework it to whichever you prefer.
Test plan
TestReaderNotBlockedByIdleConsumer(asyncio and trio): a control request behind 250 unread messages is answered while nobody reads (fails onmainwith 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).