Skip to content

Commit 30053ea

Browse files
authored
revert: Revert "fix(toolbox-core): Prevent ToolboxClient from closing externally ma…" (#271)
This reverts commit 7520adf.
1 parent 2638f66 commit 30053ea

File tree

3 files changed

+1
-55
lines changed

3 files changed

+1
-55
lines changed

packages/toolbox-core/src/toolbox_core/client.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,6 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
141141
"""
142142
await self.close()
143143

144-
def __del__(self):
145-
# This method is a "best-effort" safety net.
146-
# It should NOT be relied upon for guaranteed resource cleanup.
147-
# Explicitly using "async with" or calling "await client.close()" is the correct way.
148-
if self.__manage_session:
149-
try:
150-
loop = get_running_loop()
151-
except RuntimeError:
152-
loop = None
153-
154-
if loop and loop.is_running():
155-
# If a loop is running, try to schedule the close operation.
156-
# This is "fire-and-forget"; there's no guarantee it will complete
157-
# before the loop or interpreter shuts down.
158-
loop.create_task(self.__session.close())
159-
160144
async def close(self):
161145
"""
162146
Asynchronously closes the underlying client session. Doing so will cause

packages/toolbox-core/src/toolbox_core/sync_client.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ def close(self):
6868
any tools created by this Client to cease to function.
6969
"""
7070
coro = self.__async_client.close()
71-
run_coroutine_threadsafe(coro, self.__loop).result(timeout=5)
72-
73-
def __del__(self):
74-
self.close()
71+
run_coroutine_threadsafe(coro, self.__loop).result()
7572

7673
def load_tool(
7774
self,

packages/toolbox-core/tests/conftest.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@
2222
import subprocess
2323
import tempfile
2424
import time
25-
from asyncio import run_coroutine_threadsafe
2625
from typing import Generator
2726

2827
import google
2928
import pytest_asyncio
3029
from google.auth import compute_engine
3130
from google.cloud import secretmanager, storage
3231

33-
from toolbox_core import ToolboxSyncClient
34-
3532

3633
#### Define Utility Functions
3734
def get_env_var(key: str) -> str:
@@ -95,38 +92,6 @@ def get_auth_token(client_id: str) -> str:
9592

9693

9794
#### Define Fixtures
98-
@pytest_asyncio.fixture(autouse=True)
99-
def patch_sync_client_for_deadlock(monkeypatch):
100-
"""
101-
Automatically replaces the blocking `ToolboxSyncClient.close()` method
102-
with a non-blocking version for the entire test run.
103-
104-
The original `ToolboxSyncClient.close()` is a blocking method because it
105-
calls `.result()`. In the pytest environment, this blocking call creates a
106-
deadlock during the test teardown phase when it conflicts with other fixtures
107-
(like `sync_client_environment` or `toolbox_server`) that are also managing
108-
background processes and threads.
109-
110-
By replacing `close` with this safe, non-blocking version, we prevent the
111-
deadlock and allow the test suite's fixtures to tear down cleanly.
112-
This change is only active during the test run.
113-
"""
114-
115-
def non_blocking_close(self):
116-
"""A replacement for close() that doesn't block."""
117-
if hasattr(self.__class__, "_ToolboxSyncClient__loop") and hasattr(
118-
self, "_ToolboxSyncClient__async_client"
119-
):
120-
loop = self.__class__._ToolboxSyncClient__loop
121-
async_client = self._ToolboxSyncClient__async_client
122-
123-
if loop and loop.is_running():
124-
coro = async_client.close()
125-
run_coroutine_threadsafe(coro, loop)
126-
127-
monkeypatch.setattr(ToolboxSyncClient, "close", non_blocking_close)
128-
129-
13095
@pytest_asyncio.fixture(scope="session")
13196
def project_id() -> str:
13297
return get_env_var("GOOGLE_CLOUD_PROJECT")

0 commit comments

Comments
 (0)