Skip to content

Commit 719c4e7

Browse files
fix: avoid awaiting cleanup during coroutine closure (#5163)
1 parent 0438b09 commit 719c4e7

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎src/agents/util/_asyncio_tasks.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ async def gather_with_cancel(
106106
if on_child_failure is not None:
107107
on_child_failure()
108108
raise
109+
except GeneratorExit:
110+
# Coroutine closure cannot suspend; the owner must tear down child tasks.
111+
raise
109112
except BaseException:
110113
for task in tasks:
111114
if not task.done():
@@ -152,6 +155,9 @@ async def run_producer_consumer(
152155

153156
consumer_result = await consumer_task
154157
return producer_result, consumer_result
158+
except GeneratorExit:
159+
# Coroutine closure cannot suspend; the owner must tear down child tasks.
160+
raise
155161
except BaseException:
156162
for task in tasks:
157163
if not task.done():

‎tests/test_asyncio_tasks.py‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,77 @@ async def consumer() -> None:
160160
timeout=1,
161161
)
162162
assert consumer_cancelled.is_set()
163+
164+
165+
@pytest.mark.asyncio
166+
@pytest.mark.parametrize("producer_consumer", [False, True])
167+
async def test_closing_task_helper_leaves_child_cleanup_to_owner(producer_consumer: bool) -> None:
168+
children = [asyncio.create_task(asyncio.Event().wait()) for _ in range(2)]
169+
child_failure_reported = asyncio.Event()
170+
coro = (
171+
run_producer_consumer(*children, on_failure=child_failure_reported.set)
172+
if producer_consumer
173+
else gather_with_cancel(*children, on_child_failure=child_failure_reported.set)
174+
)
175+
try:
176+
# Drive the coroutine as its owner; do not close a live asyncio Task's coroutine.
177+
coro.send(None)
178+
coro.close()
179+
await asyncio.sleep(0)
180+
assert all(not child.done() for child in children)
181+
assert not child_failure_reported.is_set()
182+
finally:
183+
for child in children:
184+
child.cancel()
185+
await asyncio.gather(*children, return_exceptions=True)
186+
coro.close()
187+
188+
189+
@pytest.mark.asyncio
190+
async def test_closing_agent_tool_lookup_leaves_enabled_check_cleanup_to_owner() -> None:
191+
from agents import Agent, RunContextWrapper
192+
from agents.decorators import tool
193+
194+
started = asyncio.Event()
195+
finished = asyncio.Event()
196+
release = asyncio.Event()
197+
198+
async def is_enabled(context: RunContextWrapper[None], agent: Agent[None]) -> bool:
199+
started.set()
200+
try:
201+
await release.wait()
202+
finally:
203+
finished.set()
204+
return True
205+
206+
@tool(is_enabled=is_enabled)
207+
def example() -> str:
208+
return "example"
209+
210+
agent = Agent[None](name="test", tools=[example])
211+
coro = agent.get_all_tools(RunContextWrapper(context=None))
212+
try:
213+
coro.send(None)
214+
await started.wait()
215+
coro.close()
216+
await asyncio.sleep(0)
217+
assert not finished.is_set()
218+
finally:
219+
release.set()
220+
await finished.wait()
221+
coro.close()
222+
223+
224+
@pytest.mark.asyncio
225+
async def test_run_producer_consumer_drains_children_on_parent_cancellation() -> None:
226+
children = [asyncio.create_task(asyncio.Event().wait()) for _ in range(2)]
227+
parent = asyncio.create_task(run_producer_consumer(*children))
228+
try:
229+
await asyncio.sleep(0)
230+
parent.cancel()
231+
with pytest.raises(asyncio.CancelledError):
232+
await parent
233+
assert all(child.cancelled() for child in children)
234+
finally:
235+
parent.cancel()
236+
await asyncio.gather(parent, *children, return_exceptions=True)

0 commit comments

Comments
 (0)