-
Notifications
You must be signed in to change notification settings - Fork 446
feat(server): add aclose() to drain ActiveTask background tasks (#1101) #1105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
astrogilda
wants to merge
1
commit into
a2aproject:main
Choose a base branch
from
astrogilda:fix/active-task-registry-aclose-1101
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/server/agent_execution/test_active_task_registry.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| from a2a.server.agent_execution.active_task_registry import ActiveTaskRegistry | ||
| from a2a.server.agent_execution.agent_executor import AgentExecutor | ||
| from a2a.server.agent_execution.context import RequestContext | ||
| from a2a.server.context import ServerCallContext | ||
| from a2a.server.events.event_queue_v2 import EventQueue | ||
| from a2a.server.tasks import InMemoryTaskStore | ||
|
|
||
|
|
||
| class _SlowExecutor(AgentExecutor): | ||
| """An executor whose execute() blocks until cancelled.""" | ||
|
|
||
| async def execute( | ||
| self, context: RequestContext, event_queue: EventQueue | ||
| ) -> None: | ||
| await asyncio.sleep(10) | ||
|
|
||
| async def cancel( | ||
| self, context: RequestContext, event_queue: EventQueue | ||
| ) -> None: | ||
| return None | ||
|
|
||
|
|
||
| def _make_registry() -> ActiveTaskRegistry: | ||
| return ActiveTaskRegistry( | ||
| agent_executor=_SlowExecutor(), | ||
| task_store=InMemoryTaskStore(), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.timeout(5) | ||
| @pytest.mark.asyncio | ||
| async def test_aclose_reaps_active_tasks_and_empties_registry(): | ||
| """aclose() reaps background tasks and removes them.""" | ||
| registry = _make_registry() | ||
| active = await registry.get_or_create( | ||
| 'task-1', | ||
| call_context=ServerCallContext(), | ||
| create_task_if_missing=True, | ||
| ) | ||
|
|
||
| await registry.aclose() | ||
|
|
||
| assert active._producer_task is not None | ||
| assert active._producer_task.done() | ||
| assert active._consumer_task is not None | ||
| assert active._consumer_task.done() | ||
| assert await registry.get('task-1') is None | ||
|
|
||
|
|
||
| @pytest.mark.timeout(5) | ||
| @pytest.mark.asyncio | ||
| async def test_aclose_is_idempotent(): | ||
| """Calling aclose() repeatedly is a safe no-op.""" | ||
| registry = _make_registry() | ||
| await registry.get_or_create( | ||
| 'task-1', | ||
| call_context=ServerCallContext(), | ||
| create_task_if_missing=True, | ||
| ) | ||
|
|
||
| await registry.aclose() | ||
| await registry.aclose() | ||
|
|
||
|
|
||
| @pytest.mark.timeout(5) | ||
| @pytest.mark.asyncio | ||
| async def test_aclose_on_empty_registry(): | ||
| """aclose() with no active tasks returns immediately.""" | ||
| registry = _make_registry() | ||
| await registry.aclose() | ||
|
|
||
|
|
||
| @pytest.mark.timeout(5) | ||
| @pytest.mark.asyncio | ||
| async def test_get_or_create_rejected_after_aclose(): | ||
| """A closed registry refuses to create new tasks (no orphan race).""" | ||
| registry = _make_registry() | ||
| await registry.aclose() | ||
|
|
||
| with pytest.raises(RuntimeError): | ||
| await registry.get_or_create( | ||
| 'task-1', | ||
| call_context=ServerCallContext(), | ||
| create_task_if_missing=True, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.timeout(5) | ||
| @pytest.mark.asyncio | ||
| async def test_aclose_logs_and_swallows_task_errors(caplog): | ||
| """A failing ActiveTask.aclose is logged, not propagated.""" | ||
| registry = _make_registry() | ||
| failing = AsyncMock() | ||
| failing.aclose = AsyncMock(side_effect=ValueError('boom')) | ||
| registry._active_tasks['bad'] = failing | ||
|
|
||
| with caplog.at_level(logging.ERROR): | ||
| await registry.aclose() | ||
|
|
||
| assert 'Error draining active task' in caplog.text |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
start()was never called or failed early, the background tasks are never spawned, meaning theirfinallyblocks (which shut downself._request_queue) will not run. Explicitly shutting downself._request_queueinaclose()ensures that all queues are properly cleaned up and any pending operations on the request queue are unblocked.