Skip to content

fix: suppress ProcessError when result already received#790

Open
ya-luotao wants to merge 1 commit intoanthropics:mainfrom
ya-luotao:fix/suppress-process-error-after-result
Open

fix: suppress ProcessError when result already received#790
ya-luotao wants to merge 1 commit intoanthropics:mainfrom
ya-luotao:fix/suppress-process-error-after-result

Conversation

@ya-luotao
Copy link
Copy Markdown

@ya-luotao ya-luotao commented Apr 5, 2026

Summary

  • When query() uses StructuredOutput (or any tool_use stop reason), the CLI exits with code 1 because it called a tool but received no tool_result response
  • The transport raises ProcessError after the process exits, even though a valid ResultMessage was already streamed to the consumer
  • Query._read_messages catches this and enqueues it as an error, causing receive_messages to raise — so the caller sees a ProcessError despite having received the full result

Fix

In Query._read_messages, add a specific except ProcessError handler that checks _first_result_event.is_set(). If a result was already delivered, the non-zero exit code is expected and the error is suppressed with a warning log.

Test plan

  • Verify query() with output_schema (StructuredOutput) no longer raises ProcessError
  • Verify query() without a result still raises ProcessError on non-zero exit
  • Existing unit tests pass

🤖 Generated with Claude Code

The CLI can exit with non-zero code after delivering a valid result
(e.g., StructuredOutput tool_use triggers exit code 1 because the
CLI called a tool but received no tool_result response). When
_first_result_event is already set, the ProcessError is expected
and should not be propagated to the consumer.

Previously this caused query() callers to see a ProcessError even
though the full result message was already yielded successfully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant