Skip to content

Commit 5c415b6

Browse files
committed
fix: move stream_queue.put_nowait(None) to a finally block to avoid deadlock on tool cancellation (Fixes #7956)
1 parent e8e3652 commit 5c415b6

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,22 +1197,24 @@ async def _execute_tool_calls(
11971197
function_calls: List[FunctionCall],
11981198
stream_queue: asyncio.Queue[BaseAgentEvent | BaseChatMessage | None],
11991199
) -> List[Tuple[FunctionCall, FunctionExecutionResult]]:
1200-
results = await asyncio.gather(
1201-
*[
1202-
cls._execute_tool_call(
1203-
tool_call=call,
1204-
workbench=workbench,
1205-
handoff_tools=handoff_tools,
1206-
agent_name=agent_name,
1207-
cancellation_token=cancellation_token,
1208-
stream=stream_queue,
1209-
)
1210-
for call in function_calls
1211-
]
1212-
)
1213-
# Signal the end of streaming by putting None in the queue.
1214-
stream_queue.put_nowait(None)
1215-
return results
1200+
try:
1201+
results = await asyncio.gather(
1202+
*[
1203+
cls._execute_tool_call(
1204+
tool_call=call,
1205+
workbench=workbench,
1206+
handoff_tools=handoff_tools,
1207+
agent_name=agent_name,
1208+
cancellation_token=cancellation_token,
1209+
stream=stream_queue,
1210+
)
1211+
for call in function_calls
1212+
]
1213+
)
1214+
return results
1215+
finally:
1216+
# Signal the end of streaming by putting None in the queue.
1217+
stream_queue.put_nowait(None)
12161218

12171219
task = asyncio.create_task(_execute_tool_calls(current_model_result.content, stream))
12181220

0 commit comments

Comments
 (0)