-
Notifications
You must be signed in to change notification settings - Fork 362
Open
Description
Is your feature request related to a problem? Please describe.
EventQueue has a sophisticated close() with graceful/immediate modes, child propagation, cross-version handling, and idempotency — but it doesn't support async with.
Server-side code that creates and consumes queues must use explicit try/finally or risk leaking resources if an exception occurs between creation and cleanup:
queue = EventQueue()
try:
await queue.enqueue_event(event)
...
finally:
await queue.close()Describe the solution you'd like
Add __aenter__ and __aexit__ as concrete methods on EventQueue:
async def __aenter__(self) -> 'EventQueue':
return self
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
await self.close()This enables the idiomatic pattern:
async with EventQueue() as queue:
await queue.enqueue_event(event)
...
# close() called automatically, even on exceptions__aexit__ delegates to close() with default immediate=False (graceful), which is the safe default — it waits for the queue to drain and propagates to children. Code that needs immediate shutdown can still call await queue.close(immediate=True) explicitly.
Describe alternatives you've considered
- Do nothing:
try/finallyworks but is verbose and easy to forget. The pattern is already established on the client side (ClientTransportfeat: add async context manager support to ClientTransport #682,BaseClientfeat: add async context manager support to BaseClient #688,Clientfeat(client): expose close() and async context manager support on abstract Client #719). - Add an
immediateparameter to__aexit__: This would require storing state set at construction time. Overcomplicates the design for a rare use case — code that needsimmediate=Truecan callclose(immediate=True)directly.
Additional context
This follows the same pattern established across the client hierarchy:
ClientTransport— feat: add async context manager support to ClientTransport #682BaseClient— feat: add async context manager support to BaseClient #688Client— feat(client): expose close() and async context manager support on abstract Client #719
Code of Conduct
- I agree to follow this project's Code of Conduct
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels