From ec45dca291e540f1d63a8d9f8c76296ffb465706 Mon Sep 17 00:00:00 2001
From: SU_G <2014221119200033@stu.hubu.edu.cn>
Date: Wed, 21 May 2025 04:30:38 +0800
Subject: [PATCH] fix:Prevent Async Event Loop from Running Indefinitely
(#6530)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
## Why are these changes needed?
## Prevent Async Event Loop from Running Indefinitely
### Description
This pull request addresses a bug in the
python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py
`async send_message` function where messages were being queued for
recipients that were not recognized. The current implementation sets an
exception on the future object when the recipient is not found, but
continues to enqueue the message, potentially leading to inconsistent
states.
### Changes Made
- Added a return statement immediately after setting the exception when
the recipient is not found. This ensures that the function exits early,
preventing further processing of the message and avoiding unnecessary
operations.
- This fix also addresses an issue where the asynchronous event loop
could potentially continue running indefinitely without terminating, due
to the future not being properly handled when an unknown recipient is
encountered.
### Impact
This fix prevents messages from being sent to unknown recipients. It
also ensures that the event loop can terminate correctly without being
stuck in an indefinite state.
### Testing
Ensure that the function correctly handles cases where the recipient is
not recognized by returning the exception without enqueuing the message,
and verify that the event loop terminates as expected.
## Related issue number
## Checks
- [ ] I've included any doc changes needed for
. See
to
build and test documentation locally.
- [ ] I've added tests (if relevant) corresponding to the changes
introduced in this PR.
- [ ] I've made sure all auto checks have passed.
Co-authored-by: Wanfeng Ge (葛万峰)
Co-authored-by: Jack Gerrits
---
.../src/autogen_core/_single_threaded_agent_runtime.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py b/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py
index 307cdd863e..5f56364416 100644
--- a/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py
+++ b/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py
@@ -362,6 +362,7 @@ class SingleThreadedAgentRuntime(AgentRuntime):
future = asyncio.get_event_loop().create_future()
if recipient.type not in self._known_agent_names:
future.set_exception(Exception("Recipient not found"))
+ return await future
content = message.__dict__ if hasattr(message, "__dict__") else message
logger.info(f"Sending message of type {type(message).__name__} to {recipient.type}: {content}")