Skip to content

Commit 9aeb460

Browse files
committed
fix: resolve race condition in RunResultStreaming.stream_events() causing premature session cleanup
1 parent c583dfc commit 9aeb460

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/agents/result.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ async def stream_events(self) -> AsyncIterator[StreamEvent]:
213213
yield item
214214
self._event_queue.task_done()
215215

216+
# Ensure main execution completes before cleanup to avoid race conditions with session operations
217+
await self._await_task_safely(self._run_impl_task)
218+
# Safely terminate all background tasks after main execution has finished
216219
self._cleanup_tasks()
217220

218221
if self._stored_exception:

0 commit comments

Comments
 (0)