Skip to content

fix: catch CancelledError in agent router streaming functions#7323

Open
metaphorics wants to merge 1 commit intoagno-agi:mainfrom
metaphorics:fix/agent-router-cancelled-error
Open

fix: catch CancelledError in agent router streaming functions#7323
metaphorics wants to merge 1 commit intoagno-agi:mainfrom
metaphorics:fix/agent-router-cancelled-error

Conversation

@metaphorics
Copy link
Copy Markdown
Contributor

Fixes #7320

Summary

  • Add except asyncio.CancelledError: return before except Exception in both agent_response_streamer and agent_continue_response_streamer
  • CancelledError (BaseException subclass) was propagating unhandled through agent router generators when Starlette cancels a disconnected client's SSE stream
  • Handled separately from Exception: clean return for disconnections (client is gone, no error event needed) vs error event for real failures
  • More correct than the team router's broad except BaseException which also swallows SystemExit/KeyboardInterrupt

Test plan

  • Existing tests pass
  • Manual: disconnect a client mid-stream — no unhandled exception in logs

The agent router streaming generators catch `except Exception` but not
`asyncio.CancelledError` (a BaseException subclass). When Starlette
cancels a disconnected client's SSE stream, CancelledError propagates
unhandled through the agent generator.

Handle CancelledError separately from Exception: clean return (client is
gone, no error event needed) vs error event for real failures. This is
more correct than the team router's broad `except BaseException` which
also swallows SystemExit/KeyboardInterrupt.

Fixes agno-agi#7320
@metaphorics metaphorics requested a review from a team as a code owner April 3, 2026 10:11
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

PR Triage

Missing tests: This PR modifies source code but does not include any test changes. Please add or update tests to cover your changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Agent router catches Exception, not BaseException — CancelledError propagates unhandled

1 participant