Skip to content

[Feat]: Add async context manager support to EventQueue #720

@cchinchilla-dev

Description

@cchinchilla-dev

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

Additional context

This follows the same pattern established across the client hierarchy:

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Labels

component: serverIssues related to frameworks for agent execution, HTTP/event handling, database persistence logic.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions