fix(query): implement structured concurrency to prevent query() deadlocks (fixes #1136)#1139
Open
Tanush-Jain wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Resolves #1136 by replacing decoupled task lifecycles and superficial exception handling with structured concurrency powered by AnyIO task groups.
Problem Addressed
Previous PR #1109 introduced a local
try/exceptinsidestream_input()to convert prompt generator exceptions into error dictionaries. As audited in MCE Audit Tcas1109, this left several deadlock scenarios unaddressed:read_messages()hangs onstdoutbecause untracked child processes hold pipe file descriptors open.anyio.fail_after(5)to forcibly kill hanging calls, masking race conditions rather than enforcing true fail-fast behavior.Technical Solution
Structured Task Group Orchestration (
InternalClient._process_query_inner)_run_read_messages,_monitor_process, and input streaming (stream_input/wait_for_result_and_end_input) insideasync with anyio.create_task_group() as tg:.Native Exception Propagation (
Query.stream_input)try/exceptinsidestream_input()that converted exceptions into dictionaries. Exceptions fromAsyncIterableprompts bubble up natively to trigger immediate cross-task cancellation.Proactive Subprocess Lifecycle Monitoring (
SubprocessCLITransport.wait)wait()onTransportandSubprocessCLITransport._monitor_processinside the task group to await process termination. If the process exits non-zero while I/O streams are active,ProcessErroris raised immediately to unblock reading loops.Fail-Fast Test Suite (
tests/test_query.py)TestStructuredConcurrencytesting bothAsyncIterableprompt exceptions and subprocess crash scenarios.asyncioandtriobackends without using artificial timeouts (anyio.fail_after()).Verification
1236 passed, 5 skipped(pytest tests/)pytest tests/ -k TestStructuredConcurrency)ruff check src/ tests/)ruff format --check src/ tests/)mypy src/)Fixes #1136