Skip to content

fix(toolbox-core): Prevent ToolboxClient from closing externally managed client sessions #260

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

Merged
merged 11 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/toolbox-core/src/toolbox_core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ToolboxClient:

__base_url: str
__session: ClientSession
__manage_session: bool

def __init__(
self,
Expand All @@ -56,7 +57,9 @@ def __init__(
self.__base_url = url

# If no aiohttp.ClientSession is provided, make our own
self.__manage_session = False
if session is None:
self.__manage_session = True
session = ClientSession()
self.__session = session

Expand Down Expand Up @@ -142,10 +145,10 @@ async def close(self):
any tools created by this Client to cease to function.

If the session was provided externally during initialization, the caller
is responsible for its lifecycle, but calling close here will still
attempt to close it.
is responsible for its lifecycle.
"""
await self.__session.close()
if self.__manage_session:
await self.__session.close()

async def load_tool(
self,
Expand Down
4 changes: 0 additions & 4 deletions packages/toolbox-core/src/toolbox_core/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ def close(self):
"""
Synchronously closes the underlying client session. Doing so will cause
any tools created by this Client to cease to function.

If the session was provided externally during initialization, the caller
is responsible for its lifecycle, but calling close here will still
attempt to close it.
"""
coro = self.__async_client.close()
asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
Expand Down