fix: prevent AssistantAgent deadlock on tool call cancellation - #8143
Closed
MOHAMMED WASIM KHAN (wasim-builds) wants to merge 3 commits into
Closed
MOHAMMED WASIM KHAN (wasim-builds) wants to merge 3 commits into
MOHAMMED WASIM KHAN (wasim-builds) wants to merge 3 commits into
Conversation
Previously, cancelling an in-flight tool call would cause asyncio.gather to raise without executing put_nowait(None), leaving the consumer loop blocking forever on stream.get(). Now return_exceptions=True ensures put_nowait(None) always executes, allowing the consumer loop to terminate properly. Fixes microsoft#7956
Copilot started reviewing on behalf of
MOHAMMED WASIM KHAN (wasim-builds)
September 2, 2026 04:38
View session
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The current return_exceptions=True change can return exception objects that break downstream tuple-unpacking and the end-of-stream sentinel still isn’t guaranteed via finally.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR targets a deadlock in AssistantAgent.on_messages_stream when an in-flight tool call is cancelled: the streaming consumer loop can block forever if the producer never enqueues the end-of-stream sentinel.
Changes:
- Modifies
_execute_tool_callsto avoid hanging the streaming consumer when a tool call raises duringasyncio.gather. - Updates inline comments around the end-of-stream sentinel behavior.
File summaries
| File | Description |
|---|---|
python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py |
Adjusts tool-call execution/stream termination logic to prevent deadlocks on cancellation/exception paths. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
1209
to
1213
| ) | ||
| for call in function_calls | ||
| ] | ||
| ], | ||
| return_exceptions=True, | ||
| ) |
added 2 commits
September 2, 2026 10:20
When return_exceptions=True and a tool call raises (e.g. CancelledError), the result is an exception object rather than a tuple. This update converts exceptions to error FunctionExecutionResult objects so downstream processing doesn't fail with a TypeError.
Verifies that AssistantAgent.on_messages_stream terminates cleanly when a tool call is cancelled mid-flight, preventing the deadlock described in issue microsoft#8092.
Author
|
Superseded by #8182 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7956 and adds regression test for #8092
Previously, cancelling an in-flight tool call would cause asyncio.gather to raise without executing put_nowait(None), leaving the consumer loop blocking forever on stream.get(). This violated the documented cancellation contract.
Changes
Test plan