RestApiTool and Google credential tools still redeem the OAuth2 auth code on the event loop
Describe the bug
f8ba96a2 moved the OAuth2 authorization-code exchange in CredentialManager off the event loop, because the blocking token request "stalled concurrent requests on the server for up to the 10-second token request timeout". Two other async paths still call the blocking ToolContext.get_auth_response directly, so they still stall the loop the same way:
ToolAuthHandler.prepare_auth_credentials(tools/openapi_tool/openapi_spec_parser/tool_auth_handler.py), via_get_auth_response(), used byRestApiTool/OpenAPIToolset.GoogleCredentialsManager._perform_oauth_flow(tools/_google_credentials.py), used by the BigQuery, Spanner and other Google tools. It already movescreds.refreshto a thread withasyncio.to_thread, but not this exchange.
To Reproduce
A ticker counts event-loop iterations (every 10 ms) while the token endpoint takes 0.5 s to answer. The token endpoint is mocked by patching OAuth2Session, the way test_tool_auth_handler.py does.
Script
import asyncio
import time
from unittest import mock
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.llm_agent import LlmAgent
from google.adk.auth.auth_credential import AuthCredential, AuthCredentialTypes, OAuth2Auth
from google.adk.auth.auth_handler import AuthHandler
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.adk.sessions.session import Session
from google.adk.tools.openapi_tool.auth.auth_helpers import openid_dict_to_scheme_credential
from google.adk.tools.openapi_tool.openapi_spec_parser.tool_auth_handler import ToolAuthHandler
from google.adk.tools.tool_context import ToolContext
def tool_context():
return ToolContext(
function_call_id="fc",
invocation_context=InvocationContext(
agent=LlmAgent(name="a"),
session=Session(app_name="app", user_id="u", id="s"),
invocation_id="i",
session_service=InMemorySessionService(),
),
)
def slow_fetch_token(*args, **kwargs):
time.sleep(0.5) # a slow token endpoint
return {"access_token": "tok", "token_type": "bearer"}
async def count_ticks(coro):
ticks = 0
done = False
async def ticker():
nonlocal ticks
while not done:
await asyncio.sleep(0.01)
ticks += 1
t = asyncio.create_task(ticker())
await asyncio.sleep(0)
start = time.perf_counter()
try:
await coro
finally:
done = True
await t
return ticks, time.perf_counter() - start
def setup():
scheme, _ = openid_dict_to_scheme_credential(
{"authorization_endpoint": "test.com", "token_endpoint": "test.com"},
["s"],
{"client_id": "123", "client_secret": "456", "redirect_uri": "https://app/cb"},
)
credential = AuthCredential(
auth_type=AuthCredentialTypes.OPEN_ID_CONNECT,
oauth2=OAuth2Auth(client_id="123", client_secret="456", redirect_uri="https://app/cb"),
)
ctx = tool_context()
handler = ToolAuthHandler(ctx, scheme, credential)
config = handler._build_auth_config()
stored = credential.model_copy(deep=True)
stored.oauth2.client_secret = None
stored.oauth2.auth_code = "code"
stored.oauth2.auth_response_uri = "https://app/cb?code=code"
ctx.state["temp:" + config.credential_key] = stored
return ctx, handler, config
async def main():
client = mock.MagicMock()
client.fetch_token.side_effect = slow_fetch_token
with mock.patch("google.adk.auth.oauth2_credential_util.OAuth2Session", lambda *a, **k: client):
ctx, handler, _ = setup()
ticks, secs = await count_ticks(handler.prepare_auth_credentials())
print(f"RestApiTool ToolAuthHandler.prepare_auth_credentials: {ticks} ticks in {secs:.2f}s")
ctx, _, config = setup()
ticks, secs = await count_ticks(AuthHandler(config)._get_auth_response_async(ctx.state))
print(f"CredentialManager path (AuthHandler._get_auth_response_async): {ticks} ticks in {secs:.2f}s")
asyncio.run(main())
Output on main (1cb926d5):
RestApiTool ToolAuthHandler.prepare_auth_credentials: 1 ticks in 0.51s
CredentialManager path (AuthHandler._get_auth_response_async): 45 ticks in 0.50s
The loop is frozen for the whole exchange on the RestApiTool path, and keeps running on the path f8ba96a2 fixed. I checked the Google credentials path by reading only: _perform_oauth_flow is async and calls tool_context.get_auth_response(...) directly.
Expected behavior
Redeeming the code doesn't block the event loop on any path, as with CredentialManager since f8ba96a2.
Suggested fix
Have both callers await the helper f8ba96a2 added, AuthHandler(config)._get_auth_response_async(tool_context.state), instead of the synchronous get_auth_response. In ToolAuthHandler that means making _get_auth_response async. A few tests in test_tool_auth_handler.py mock AuthHandler.get_auth_response, so they would need to mock the async helper instead.
I'm happy to send a PR for both paths if that's welcome.
Desktop (please complete the following information):
- OS: macOS
- Python version: 3.12
- ADK version: main (1cb926d5)
Hi @AkshayShah03, thanks for the detailed report. #8044 should fix this. It covers both the RestApiTool and Google credentials paths, and its regression tests fail on main and pass with the fix.
Could you test the PR in your setup and confirm whether it resolves the issue on your end?
Thanks @sanketpatil06. I tested #8044. It fixes the stall: the RestApiTool path now keeps the event loop running during the exchange (46 ticks in 0.5 s, against 1 on main). It does introduce one problem, though: two parallel tool calls can now redeem the same single-use code, and both end up without a credential. I left the details and a suggested fix (the same per-credential lock CredentialManager uses) on the PR.