Skip to content

Conversation

@ltawfik
Copy link
Collaborator

@ltawfik ltawfik commented Jul 1, 2025

Description

Fixes RuntimeError when using the SDK with FastAPI's Server-Sent Events streaming (#4).

Changes

  • Remove anyio.create_task_group() from receive_messages()
  • Read stderr sequentially after stdout completes
  • Add test to prevent regression

Technical Details

FastAPI can move async generators between different asyncio tasks during streaming, which conflicts with anyio's requirement that cancel scopes must be entered and exited in the same task.

The solution removes concurrent task creation and reads streams sequentially, eliminating the task boundary crossing issue.

Testing

  • All existing tests pass
  • Added test_fastapi_streaming_compatibility.py to verify no task groups are used
  • Manually verified with FastAPI SSE endpoint

ltawfik and others added 2 commits July 1, 2025 00:04
Remove anyio.create_task_group() from receive_messages() to fix the
RuntimeError "Attempted to exit cancel scope in a different task than
it was entered in" when using the SDK with FastAPI's SSE streaming.

The issue occurred because FastAPI can move async generators between
different asyncio tasks during the streaming lifecycle, which conflicts
with anyio's cancel scope tracking.

Changes:
- Remove task group usage from receive_messages()
- Read stderr sequentially after stdout completes
- Add test to ensure no task groups are used
- Fix existing test expectation for buffer overflow

This is a minimal fix that maintains compatibility while solving the
core issue. The trade-off is that stderr is read after stdout instead
of concurrently, but this is unlikely to cause issues in practice.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ltawfik
Copy link
Collaborator Author

ltawfik commented Jul 1, 2025

Fixed linting issues - removed trailing whitespace and added newline at end of file.

The test was checking source code for specific strings rather than
testing behavior. The existing subprocess tests already verify the
functionality works correctly.
ltawfik and others added 2 commits July 1, 2025 16:24
Add critical safety mechanisms to the sequential stderr reading approach:
- 10MB memory limit on stderr collection with truncation message
- 30 second timeout protection to prevent hanging
- Proper exit code based error detection instead of string matching
- Debug logging for stderr on successful runs
- Better exception handling for edge cases

This addresses potential production issues while maintaining FastAPI compatibility.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ltawfik ltawfik merged commit e4de22e into main Jul 2, 2025
6 checks passed
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.

3 participants