Skip to content

revert(toolbox-core): Reverts "fix(toolbox-core): Prevent ToolboxClient from closing externally managed client sessions" #271

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 1 commit into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 0 additions & 16 deletions packages/toolbox-core/src/toolbox_core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,6 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
"""
await self.close()

def __del__(self):
# This method is a "best-effort" safety net.
# It should NOT be relied upon for guaranteed resource cleanup.
# Explicitly using "async with" or calling "await client.close()" is the correct way.
if self.__manage_session:
try:
loop = get_running_loop()
except RuntimeError:
loop = None

if loop and loop.is_running():
# If a loop is running, try to schedule the close operation.
# This is "fire-and-forget"; there's no guarantee it will complete
# before the loop or interpreter shuts down.
loop.create_task(self.__session.close())

async def close(self):
"""
Asynchronously closes the underlying client session. Doing so will cause
Expand Down
5 changes: 1 addition & 4 deletions packages/toolbox-core/src/toolbox_core/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ def close(self):
any tools created by this Client to cease to function.
"""
coro = self.__async_client.close()
run_coroutine_threadsafe(coro, self.__loop).result(timeout=5)

def __del__(self):
self.close()
run_coroutine_threadsafe(coro, self.__loop).result()

def load_tool(
self,
Expand Down
35 changes: 0 additions & 35 deletions packages/toolbox-core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@
import subprocess
import tempfile
import time
from asyncio import run_coroutine_threadsafe
from typing import Generator

import google
import pytest_asyncio
from google.auth import compute_engine
from google.cloud import secretmanager, storage

from toolbox_core import ToolboxSyncClient


#### Define Utility Functions
def get_env_var(key: str) -> str:
Expand Down Expand Up @@ -95,38 +92,6 @@ def get_auth_token(client_id: str) -> str:


#### Define Fixtures
@pytest_asyncio.fixture(autouse=True)
def patch_sync_client_for_deadlock(monkeypatch):
"""
Automatically replaces the blocking `ToolboxSyncClient.close()` method
with a non-blocking version for the entire test run.

The original `ToolboxSyncClient.close()` is a blocking method because it
calls `.result()`. In the pytest environment, this blocking call creates a
deadlock during the test teardown phase when it conflicts with other fixtures
(like `sync_client_environment` or `toolbox_server`) that are also managing
background processes and threads.

By replacing `close` with this safe, non-blocking version, we prevent the
deadlock and allow the test suite's fixtures to tear down cleanly.
This change is only active during the test run.
"""

def non_blocking_close(self):
"""A replacement for close() that doesn't block."""
if hasattr(self.__class__, "_ToolboxSyncClient__loop") and hasattr(
self, "_ToolboxSyncClient__async_client"
):
loop = self.__class__._ToolboxSyncClient__loop
async_client = self._ToolboxSyncClient__async_client

if loop and loop.is_running():
coro = async_client.close()
run_coroutine_threadsafe(coro, loop)

monkeypatch.setattr(ToolboxSyncClient, "close", non_blocking_close)


@pytest_asyncio.fixture(scope="session")
def project_id() -> str:
return get_env_var("GOOGLE_CLOUD_PROJECT")
Expand Down