← Back to Pull requests

Fail pending control requests promptly when Query closes #1364

Open
unknown opened this pull request with 341 lines diff across 4 files
View on GitHub
unknown unknown commented on Oct 06, 2026 13:59 UTC
Author

Fixes #1094.

Summary

Pending outgoing control requests already fail fast when the message reader exits with an error, but two shutdown paths could still leave callers waiting for the full control-request timeout:

  • caller-initiated Query.close() / ClaudeSDKClient.disconnect();

  • a clean reader EOF, where the transport ends normally and no exception path runs.
  • This change makes both paths fail pending control requests promptly while preserving any response or richer error that was already recorded.

    Prior art

    #1095 identified the same user-facing hang and correctly covered reader shutdown with a finally wakeup. That PR was opened against an older July base and is currently non-mergeable.

    This PR keeps that reader-exit coverage, preserves the newer typed error handling now on main, and also rejects control requests started after the query is already closed.

    Changes

  • factor unresolved-control wakeup into a shared helper;

  • fail all unresolved outgoing control requests as soon as Query.close() starts;

  • fail unresolved control requests when the message reader exits cleanly;

  • preserve already-resolved responses / richer reader errors;

  • reject new outgoing control requests after the query is closed;

  • cover asyncio and trio, including multiple simultaneous waiters.
  • Validation

    Regression proof against the unchanged base used for this branch:

  • 4/6 focused lifecycle cases fail by timeout: caller close and clean reader EOF fail under both asyncio and trio;

  • the two already-resolved-result preservation cases pass.
  • With this branch:

  • the committed focused shutdown regression class passes under asyncio and trio;

  • pytest -q tests/test_query.py — passed;

  • ruff check src/claude_agent_sdk/_internal/query.py tests/test_query.py — passed;

  • ruff format --check src/claude_agent_sdk/_internal/query.py tests/test_query.py — passed;

  • mypy src/claude_agent_sdk/_internal/query.py — passed.
  • Additional synthetic verification, kept out of the diff to avoid expanding the PR just for test volume:

  • multiple pending waiters fail promptly on close;

  • an already-recorded response is preserved when the reader exits;

  • a ProcessError is preserved rather than replaced by the generic shutdown error;

  • response-before-close and close-before-response ordering both terminate deterministically without a hang or overwrite;

  • public ClaudeSDKClient.disconnect() fails an in-flight control request promptly under both asyncio and trio;

  • the combined tests/test_query.py tests/test_client.py run passes;

  • mypy passes for both query.py and client.py.
  • The branch contains 2 commits. Upstream main has since advanced independently, and GitHub still reports this PR mergeable.

    Diff: 341 lines (+255 / -86) across 4 files Reviewer: tonydzi Review duration: 12828m
    All Vouch review confidence checks passed
    Residual risk 0.25 = Change Risk (0.47) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 341 lines diff across 4 files
    0.47
    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.349
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #1364"
    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: 47.3% (MEDIUM) · Higher = larger blast radius
    47.3%
    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: 25.2% · Safe to merge
    25.2%