feat(server): add async context manager support to EventQueue#743
feat(server): add async context manager support to EventQueue#743cchinchilla-dev wants to merge 2 commits intoa2aproject:mainfrom
Conversation
Summary of ChangesHello @cchinchilla-dev, 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 introduces asynchronous context manager support to the Highlights
Changelog
Activity
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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds async context manager support to the EventQueue class by implementing __aenter__ and __aexit__. This is a great improvement, as it allows for more idiomatic and safer resource management using async with. The implementation is clean, and the accompanying tests correctly verify the new functionality, including the happy path and exception handling. I've left a few minor stylistic suggestions in the test code to align with common Python practices for boolean assertions.
| """Test that EventQueue can be used as an async context manager.""" | ||
| async with event_queue as q: | ||
| assert q is event_queue | ||
| assert event_queue.is_closed() is False |
| async with event_queue as q: | ||
| assert q is event_queue | ||
| assert event_queue.is_closed() is False | ||
| assert event_queue.is_closed() is True |
There was a problem hiding this comment.
| with pytest.raises(RuntimeError, match='boom'): | ||
| async with event_queue: | ||
| raise RuntimeError('boom') | ||
| assert event_queue.is_closed() is True |
There was a problem hiding this comment.
There was a problem hiding this comment.
Thanks for the suggestion! I kept is True/is False for consistency with the existing assertions in this file (e.g., test_close_idempotent, test_is_closed_reflects_state, etc.), which all use the same style. Happy to change if maintainers prefer updating.
Description
Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
CONTRIBUTINGGuide.fix:which represents bug fixes, and correlates to a SemVer patch.feat:represents a new feature, and correlates to a SemVer minor.feat!:, orfix!:,refactor!:, etc., which represent a breaking change (indicated by the!) and will result in a SemVer major.bash scripts/format.shfrom the repository root to format)Fixes #720 🦕
Problem
EventQueuehas a sophisticatedclose()with graceful/immediate modes, child propagation, and cross-version handling — but doesn't supportasync with. Server-side code must use explicittry/finallyor risk leaking resources on exceptions:Fix
Add
__aenter__and__aexit__as concrete methods onEventQueue:__aenter__returnsSelf(viatyping_extensions).__aexit__delegates toclose()with defaultimmediate=False(graceful). Code needing immediate shutdown can still callawait queue.close(immediate=True)explicitly.This enables the idiomatic pattern:
Unlike the client-side hierarchy where
__aenter__/__aexit__were lifted to the abstractClient(#719),EventQueueis a concrete class with no abstract base above it —QueueManagermanages queue lifecycles by task ID but does not wrap or extendEventQueue. This is the correct and only place for these methods.Non-breaking, additive change. Manual
close()andtry/finallycontinue to work as before.Follows the pattern established in
ClientTransport(#682),BaseClient(#688), andClient(#719).Tests
Two tests added to
tests/server/events/test_event_queue.py, following the same approach asClientTransportandBaseClient.