Skip to content

fix: Fix issue causing masking of empty loop or thread check while loading tool/toolset #227

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 6 commits into from
May 15, 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
13 changes: 6 additions & 7 deletions packages/toolbox-core/src/toolbox_core/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.


import asyncio
from threading import Thread
from typing import Any, Callable, Coroutine, Mapping, Optional, TypeVar, Union
Expand Down Expand Up @@ -59,7 +60,7 @@ async def create_client():

# Ignoring type since we're already checking the existence of a loop above.
self.__async_client = asyncio.run_coroutine_threadsafe(
create_client(), self.__class__.__loop # type: ignore
create_client(), self.__class__.__loop
).result()

def close(self):
Expand Down Expand Up @@ -101,11 +102,10 @@ def load_tool(
"""
coro = self.__async_client.load_tool(name, auth_token_getters, bound_params)

# We have already created a new loop in the init method in case it does not already exist
async_tool = asyncio.run_coroutine_threadsafe(coro, self.__loop).result() # type: ignore

if not self.__loop or not self.__thread:
raise ValueError("Background loop or thread cannot be None.")

async_tool = asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
return ToolboxSyncTool(async_tool, self.__loop, self.__thread)

def load_toolset(
Expand All @@ -130,11 +130,10 @@ def load_toolset(
"""
coro = self.__async_client.load_toolset(name, auth_token_getters, bound_params)

# We have already created a new loop in the init method in case it does not already exist
async_tools = asyncio.run_coroutine_threadsafe(coro, self.__loop).result() # type: ignore

if not self.__loop or not self.__thread:
raise ValueError("Background loop or thread cannot be None.")

async_tools = asyncio.run_coroutine_threadsafe(coro, self.__loop).result() # type: ignore
return [
ToolboxSyncTool(async_tool, self.__loop, self.__thread)
for async_tool in async_tools
Expand Down
6 changes: 2 additions & 4 deletions packages/toolbox-core/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,8 @@ async def client(self, aioresponses, test_tool_auth, tool_name, expected_header)

# mock tool INVOKE call
def require_headers(url, **kwargs):
if kwargs["headers"].get("my-auth-service_token") == expected_header:
return CallbackResult(status=200, body="{}")
else:
return CallbackResult(status=400, body="{}")
assert kwargs["headers"].get("my-auth-service_token") == expected_header
return CallbackResult(status=200, body="{}")

aioresponses.post(
f"{TEST_BASE_URL}/api/tool/{tool_name}/invoke",
Expand Down