Skip to content

Fix/broad exception handling #6280#6647

Merged
ekzhu merged 18 commits into
microsoft:mainfrom
tejas-dharani:fix/broad-exception-handling-6280
Jun 27, 2025
Merged

Fix/broad exception handling #6280#6647
ekzhu merged 18 commits into
microsoft:mainfrom
tejas-dharani:fix/broad-exception-handling-6280

Conversation

@tejas-dharani
Copy link
Copy Markdown
Contributor

Why are these changes needed?

This fix addresses proper WebSocket disconnect handling in the team chat application. The current implementation catches WebSocketDisconnect exceptions with broad except Exception: blocks and re-raises them as RuntimeError, preventing graceful handling of client disconnections. This causes server errors when clients disconnect normally and breaks the expected flow when UserProxyAgent is waiting for input.

Related issue number

Closes #6280

What changes are included in this PR?

Changes Made

  1. Added specific WebSocketDisconnect handling in _user_input function:

    • Catches WebSocketDisconnect specifically and allows it to propagate instead of masking it with broad exception handling
    • Adds informative logging for disconnect events
  2. Added specific WebSocketDisconnect handling in main message processing loop:

    • Gracefully exits the loop when client disconnects during message processing
    • Prevents attempts to send messages to closed connections
  3. Improved error message handling with disconnect detection:

    • Handles disconnections that occur while sending error messages
    • Prevents server errors when client disconnects unexpectedly

Checks

…on guide- Fix version format from 0.4.0-dev-1 to 0.4.0-dev.1 for all packages- Remove reference to non-existent Microsoft.AutoGen.Extensions package- Add correct extension packages: Aspire, MEAI, and SemanticKernel- Fix typo: RuntimeGatewway -> RuntimeGateway- Improve documentation structure with clear section headersFixes #6244
Fix issue #6277 where TextMessage was used but not imported in three code cells
of the custom agents documentation, causing NameError when users run the examples.

Changes:
- Add TextMessage to imports in ArithmeticAgent section
- Add TextMessage to imports in GeminiAssistantAgent section
- Add TextMessage to imports in Declarative GeminiAssistantAgent section

The CountDownAgent section already had the correct import.

Fixes #6277
…_team.py

- Add specific WebSocketDisconnect exception handling in _user_input function
- Handle client disconnects during message processing without RuntimeError
- Prevent attempts to send messages to closed WebSocket connections
- Add graceful cleanup when clients disconnect while waiting for input

Fixes #6280: Heavy use of broad Exceptions leads to incorrect error handling

The previous implementation caught WebSocketDisconnect with broad Exception
handlers and re-raised as RuntimeError, preventing graceful disconnect handling.
This fix allows proper exception propagation and clean connection termination.
…stallation guide- Fix version format from 0.4.0-dev-1 to 0.4.0-dev.1 for all packages- Remove reference to non-existent Microsoft.AutoGen.Extensions package- Add correct extension packages: Aspire, MEAI, and SemanticKernel- Fix typo: RuntimeGatewway -> RuntimeGateway- Improve documentation structure with clear section headersFixes #6244"

This reverts commit 6d9fb2e.
Comment thread python/samples/agentchat_fastapi/test_websocket_disconnect.py Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.79%. Comparing base (11b7743) to head (e47a1cf).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6647      +/-   ##
==========================================
- Coverage   79.74%   73.79%   -5.96%     
==========================================
  Files         232      122     -110     
  Lines       17403    10368    -7035     
==========================================
- Hits        13878     7651    -6227     
+ Misses       3525     2717     -808     
Flag Coverage Δ
unittests 73.79% <ø> (-5.96%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tejas-dharani tejas-dharani requested a review from ekzhu June 27, 2025 08:30
@ekzhu ekzhu enabled auto-merge (squash) June 27, 2025 11:06
@ekzhu ekzhu merged commit b571e3f into microsoft:main Jun 27, 2025
65 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.

Heavy use of broad Exceptions leads to incorrect error handling

2 participants