Skip to content

Commit 69a310c

Browse files
authored
fix: propagate CLI errors to pending control requests (#388)
## Summary When the CLI exits with an error (e.g., invalid session ID passed to `--resume`), signal all pending control requests immediately instead of waiting for the 60-second timeout. **The fix adds 4 lines** to the exception handler in `_read_messages`: ```python # Signal all pending control requests so they fail fast instead of timing out for request_id, event in list(self.pending_control_responses.items()): if request_id not in self.pending_control_results: self.pending_control_results[request_id] = e event.set() ``` ## Problem When the CLI exits with an error, the SDK's message reader catches it but doesn't notify pending control requests. This causes `initialize()` to wait for the full 60-second timeout even though the error is known within seconds. Example scenario: 1. User passes invalid session ID via `ClaudeAgentOptions(resume="invalid-id")` 2. CLI prints `No conversation found with session ID: xxx` and exits with code 1 3. SDK message reader catches the error after ~3 seconds 4. But `initialize()` keeps waiting for 60 seconds before timing out ## Solution The existing code at `_send_control_request` lines 361-362 already handles exceptions in results: ```python if isinstance(result, Exception): raise result ``` The fix simply signals all pending control events when an error occurs, allowing them to fail fast with the actual error instead of timing out. ## Test Plan - [ ] Test with invalid session ID - should fail fast (~3s) instead of timing out (60s) - [ ] Test normal flow still works - [ ] Test multiple pending requests all get signaled Fixes #387
1 parent 6791efe commit 69a310c

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/claude_agent_sdk/_internal/query.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ async def _read_messages(self) -> None:
214214
raise # Re-raise to properly handle cancellation
215215
except Exception as e:
216216
logger.error(f"Fatal error in message reader: {e}")
217+
# Signal all pending control requests so they fail fast instead of timing out
218+
for request_id, event in list(self.pending_control_responses.items()):
219+
if request_id not in self.pending_control_results:
220+
self.pending_control_results[request_id] = e
221+
event.set()
217222
# Put error in stream so iterators can handle it
218223
await self._message_send.send({"type": "error", "error": str(e)})
219224
finally:

0 commit comments

Comments
 (0)