Skip to content

Commit ce113a8

Browse files
RKestcopybara-github
authored andcommitted
fix: revert fix handle ContextVar detach errors during task cancellation
Co-authored-by: Max Ind <maxind@google.com> PiperOrigin-RevId: 903267607
1 parent 7744cfe commit ce113a8

5 files changed

Lines changed: 3 additions & 161 deletions

File tree

src/google/adk/agents/base_agent.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import inspect
1818
import logging
19-
import sys
2019
from typing import Any
2120
from typing import AsyncGenerator
2221
from typing import Awaitable
@@ -286,9 +285,7 @@ async def run_async(
286285
Event: the events generated by the agent.
287286
"""
288287

289-
cm = tracer.start_as_current_span(f'invoke_agent {self.name}')
290-
span = cm.__enter__()
291-
try:
288+
with tracer.start_as_current_span(f'invoke_agent {self.name}') as span:
292289
ctx = self._create_invocation_context(parent_context)
293290
tracing.trace_agent_invocation(span, self, ctx)
294291
if event := await self._handle_before_agent_callback(ctx):
@@ -305,23 +302,6 @@ async def run_async(
305302

306303
if event := await self._handle_after_agent_callback(ctx):
307304
yield event
308-
except BaseException:
309-
try:
310-
cm.__exit__(*sys.exc_info())
311-
except ValueError:
312-
logger.warning(
313-
'Failed to detach context during generator cleanup, likely due to'
314-
' cancellation.'
315-
)
316-
raise
317-
else:
318-
try:
319-
cm.__exit__(None, None, None)
320-
except ValueError:
321-
logger.warning(
322-
'Failed to detach context during generator cleanup, likely due to'
323-
' cancellation.'
324-
)
325305

326306
@final
327307
async def run_live(

src/google/adk/flows/llm_flows/base_llm_flow.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import asyncio
1919
import inspect
2020
import logging
21-
import sys
2221
from typing import AsyncGenerator
2322
from typing import Optional
2423
from typing import TYPE_CHECKING
@@ -1169,9 +1168,7 @@ async def _call_llm_async(
11691168
) -> AsyncGenerator[LlmResponse, None]:
11701169

11711170
async def _call_llm_with_tracing() -> AsyncGenerator[LlmResponse, None]:
1172-
cm = tracer.start_as_current_span('call_llm')
1173-
span = cm.__enter__()
1174-
try:
1171+
with tracer.start_as_current_span('call_llm') as span:
11751172
# Runs before_model_callback inside the call_llm span so
11761173
# plugins observe the same span as after/error callbacks.
11771174
if response := await self._handle_before_model_callback(
@@ -1264,23 +1261,6 @@ async def _call_llm_with_tracing() -> AsyncGenerator[LlmResponse, None]:
12641261
llm_response = altered
12651262

12661263
yield llm_response
1267-
except BaseException:
1268-
try:
1269-
cm.__exit__(*sys.exc_info())
1270-
except ValueError:
1271-
logger.warning(
1272-
'Failed to detach context during generator cleanup, likely due to'
1273-
' cancellation.'
1274-
)
1275-
raise
1276-
else:
1277-
try:
1278-
cm.__exit__(None, None, None)
1279-
except ValueError:
1280-
logger.warning(
1281-
'Failed to detach context during generator cleanup, likely due to'
1282-
' cancellation.'
1283-
)
12841264

12851265
async with Aclosing(_call_llm_with_tracing()) as agen:
12861266
async for event in agen:

src/google/adk/runners.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,7 @@ async def _run_with_trace(
543543
new_message: Optional[types.Content] = None,
544544
invocation_id: Optional[str] = None,
545545
) -> AsyncGenerator[Event, None]:
546-
cm = tracer.start_as_current_span('invocation')
547-
span = cm.__enter__()
548-
try:
546+
with tracer.start_as_current_span('invocation'):
549547
session = await self._get_or_create_session(
550548
user_id=user_id,
551549
session_id=session_id,
@@ -629,23 +627,6 @@ async def execute(ctx: InvocationContext) -> AsyncGenerator[Event]:
629627
self.session_service,
630628
skip_token_compaction=invocation_context.token_compaction_checked,
631629
)
632-
except BaseException:
633-
try:
634-
cm.__exit__(*sys.exc_info())
635-
except ValueError:
636-
logger.warning(
637-
'Failed to detach context during generator cleanup, likely due to'
638-
' cancellation.'
639-
)
640-
raise
641-
else:
642-
try:
643-
cm.__exit__(None, None, None)
644-
except ValueError:
645-
logger.warning(
646-
'Failed to detach context during generator cleanup, likely due to'
647-
' cancellation.'
648-
)
649630

650631
async with Aclosing(_run_with_trace(new_message, invocation_id)) as agen:
651632
async for event in agen:

tests/unittests/tools/BUILD

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,3 @@ pytest_test(
3131
"//third_party/py/pytest_asyncio",
3232
],
3333
)
34-
35-
pytest_test(
36-
name = "test_context_cancellation",
37-
srcs = ["test_context_cancellation.py"],
38-
args = [
39-
"-p",
40-
"pytest_asyncio.plugin",
41-
],
42-
deps = [
43-
"//third_party/py/google/adk",
44-
"//third_party/py/google/genai",
45-
"//third_party/py/pytest_asyncio",
46-
],
47-
)

tests/unittests/tools/test_context_cancellation.py

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)