fix: fix python 3.10 workflow asyncio.timeout usage#4877
fix: fix python 3.10 workflow asyncio.timeout usage#4877sasha-gitg wants to merge 6 commits intogoogle:v2from
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request primarily addresses an incompatibility issue with Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Ignored Files
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
Response from ADK Triaging Agent Hello @sasha-gitg, thank you for creating this PR! This PR is a bug fix, could you please associate the github issue with this PR? If there is no existing issue, could you please create one? In addition, could you please provide logs or screenshot after the fix is applied? This information will help reviewers to review your PR more efficiently. Thanks! |
There was a problem hiding this comment.
Code Review
This pull request introduces a compatibility layer for asyncio.timeout to support Python versions older than 3.11, which is a good improvement. My review focuses on the correctness of this fallback implementation. I've identified a significant issue where the fallback incorrectly handles task cancellations, potentially masking the true cause of errors. I've provided a detailed comment with a suggested fix to make the implementation more robust and align it with the behavior of the standard library's asyncio.timeout.
| @contextlib.asynccontextmanager | ||
| async def _timeout_context(delay): | ||
| """Fallback for Python <3.11 without async_timeout.""" | ||
| if delay is None: | ||
| yield | ||
| return | ||
| task = asyncio.current_task() | ||
| loop = asyncio.get_running_loop() | ||
| handle = loop.call_later(delay, task.cancel) | ||
| try: | ||
| yield | ||
| except asyncio.CancelledError: | ||
| raise TimeoutError(f'Node timed out after {delay}s') from None | ||
| finally: | ||
| handle.cancel() |
There was a problem hiding this comment.
The current fallback implementation for _timeout_context incorrectly treats all asyncio.CancelledError exceptions as timeouts. This can mask the true cause of cancellation if the task is cancelled externally, leading to incorrect error handling.
The standard asyncio.timeout context manager correctly distinguishes between a timeout and an external cancellation. This fallback should mimic that behavior by re-raising asyncio.CancelledError if it was not caused by the timeout.
I suggest using a flag to track whether the timeout was triggered, and only raise TimeoutError in that case.
@contextlib.asynccontextmanager
async def _timeout_context(delay):
"""Fallback for Python <3.11 without async_timeout."""
if delay is None:
yield
return
task = asyncio.current_task()
loop = asyncio.get_running_loop()
timed_out = False
def on_timeout():
nonlocal timed_out
timed_out = True
task.cancel()
handle = loop.call_later(delay, on_timeout)
try:
yield
except asyncio.CancelledError:
if timed_out:
raise TimeoutError(f'Node timed out after {delay}s') from None
raise # Re-raise external cancellations
finally:
handle.cancel()
No description provided.