-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Python: fix(python): add release_session API to prevent BackgroundAgentsProvider memory leaks #7450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -312,6 +312,41 @@ def _get_runtime(self, session: AgentSession) -> _RuntimeState: | |
| self._runtime[session_id] = _RuntimeState() | ||
| return self._runtime[session_id] | ||
|
|
||
| async def release_session(self, session_id: str, *, cancel_running: bool = True) -> None: | ||
| """Release all runtime state for a session to prevent runtime leaks. | ||
|
|
||
| Args: | ||
| session_id: The session ID to release. | ||
| cancel_running: If True, cancel pending asyncio.Tasks safely. | ||
| """ | ||
|
|
||
|
|
||
| runtime = self._runtime.get(session_id) | ||
| if runtime is None: | ||
| return | ||
|
|
||
| tasks = list(runtime.in_flight_tasks.values()) | ||
| pending = [t for t in tasks if not t.done()] | ||
|
|
||
| if pending and not cancel_running: | ||
| raise RuntimeError( | ||
| f"Cannot release session {session_id}: {len(pending)} tasks still running." | ||
| ) | ||
|
|
||
| if cancel_running and pending: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What bounds teardown when an agent suppresses |
||
| for task in pending: | ||
| task.cancel() | ||
|
|
||
| await asyncio.wait(pending, return_when=asyncio.ALL_COMPLETED) | ||
|
|
||
| if tasks: | ||
| await asyncio.gather(*tasks, return_exceptions=True) | ||
|
|
||
| self._runtime.pop(session_id, None) | ||
|
|
||
| runtime.in_flight_tasks.clear() | ||
| runtime.background_sessions.clear() | ||
|
|
||
| async def before_run( | ||
| self, | ||
| *, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could teardown race with an active parent run? While this method awaits the snapshotted tasks, an existing tool closure can add work to the same
runtime; that task is never cancelled or awaited, and clearing its tracking reference leaves it executing arbitrary agent side effects after the session has been released. Concurrent releases can also let a stale call pop a replacement runtime created for the same session, so could we mark the old runtime closed and remove it only if the mapping still points to that instance?