Skip to content

Commit ab7e36f

Browse files
authored
fix(server): fail fast when an executor produces no events (a2aproject#1345)
## Description With an `AgentExecutor` that returns normally without enqueueing any event, `message/send` hung forever on both the request coroutine and the ActiveTask producer/consumer tasks: `result` was `None`, and `ActiveTask.get_task()` awaited `_task_created` with no timeout — but `_task_created` is only set when a task event is processed, the task pre-existed at `start()`, or the producer failed. A *silent success* path sets none of them. Root-cause analysis and a runtime reproduction are in a2aproject#1330. Per the approach endorsed on the issue, this fail-fasts with `InternalError` when the subscribe loop finishes without a task event: - New `ActiveTask.task_created_is_set()` accessor (public wrapper over the internal `_task_created` event) so the handler doesn't reach into private state. - `DefaultRequestHandlerV2.on_message_send`: raise `InternalError` when `result is None` and `task_created_is_set()` is False, before awaiting `get_task()`. Follow-up (not in this PR): the legacy handler contains an equivalent `raise InternalError` guard (`default_request_handler.py:456`) that is empirically unreachable in this scenario — the flow blocks earlier in `consume_and_break_on_interrupt`. Making that reachable is a separate piece of work. - [x] Follow the CONTRIBUTING Guide - [x] PR title follows Conventional Commits (`fix:` → SemVer patch) - [x] Tests and linter pass - [x] Appropriate docs were updated (accessor docstring states the guarantee) Fixes a2aproject#1330 🦕 ## Validation - Claim: a zero-event executor now fails fast with `InternalError` instead of hanging; an event-producing executor is unaffected. - Exact head: 1ed3680 - BEFORE: `message/send` never returns (3s+ timeout in repro, issue a2aproject#1330). AFTER: raises `InternalError('Agent execution produced no events')` immediately. - Tests: new `test_on_message_send_raises_when_agent_produces_no_events`; handler + agent_execution suites green. - Environment: Python 3.10.20, macOS. - Limits / not tested: the optional timeout/completion-signaling complement discussed on the issue — the `is_set()` guard fully covers the unreachable-wait case; happy to add on request. Prepared with AI assistance (WorkBuddy); reviewed and verified by Linux2010 --------- Signed-off-by: andy <linux2011@qq.com>
1 parent 2cc1dd5 commit ab7e36f

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎src/a2a/server/agent_execution/active_task.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,11 @@ async def _mark_task_as_failed(self, exception: Exception) -> Task | None:
11201120
pass
11211121
return task
11221122

1123+
@property
1124+
def task_created(self) -> bool:
1125+
"""Whether a task exists, so ``get_task()`` will not block."""
1126+
return self._task_created.is_set()
1127+
11231128
async def get_task(self) -> Task:
11241129
"""Get task from db."""
11251130
await self._task_created.wait()

‎src/a2a/server/request_handlers/default_request_handler_v2.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from a2a.utils.errors import (
6464
ExtendedAgentCardNotConfiguredError,
6565
InternalError,
66+
InvalidAgentResponseError,
6667
InvalidParamsError,
6768
PushNotificationNotSupportedError,
6869
TaskNotCancelableError,
@@ -516,6 +517,11 @@ async def on_message_send( # noqa: D102
516517
# pushed after a Message.
517518

518519
if result is None:
520+
if not active_task.task_created:
521+
# A silent executor means get_task() would block forever.
522+
raise InvalidAgentResponseError(
523+
message='Agent execution produced no events'
524+
)
519525
logger.debug('Missing result for task %s', request_context.task_id)
520526
result = await active_task.get_task()
521527

‎tests/server/request_handlers/test_default_request_handler_v2.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,3 +3586,38 @@ async def test_failed_cancel_restarts_idle_release() -> None:
35863586

35873587
assert await _wait_for_release(handler, task.id)
35883588
await handler.aclose()
3589+
3590+
3591+
class SilentExecutor(AgentExecutor):
3592+
"""Returns normally without producing any event (issue #1330)."""
3593+
3594+
async def execute(self, context: RequestContext, event_queue: EventQueue):
3595+
return None
3596+
3597+
async def cancel(self, context: RequestContext, event_queue: EventQueue):
3598+
pass
3599+
3600+
3601+
@pytest.mark.asyncio
3602+
async def test_on_message_send_raises_when_agent_produces_no_events():
3603+
"""A silent executor must fail fast instead of hanging forever (#1330)."""
3604+
request_handler = DefaultRequestHandlerV2(
3605+
agent_executor=SilentExecutor(),
3606+
task_store=InMemoryTaskStore(),
3607+
agent_card=create_default_agent_card(),
3608+
)
3609+
params = SendMessageRequest(
3610+
message=Message(
3611+
role=Role.ROLE_USER,
3612+
message_id='msg_silent',
3613+
parts=[Part(text='Hi')],
3614+
),
3615+
configuration=SendMessageConfiguration(
3616+
accepted_output_modes=['text/plain']
3617+
),
3618+
)
3619+
3620+
with pytest.raises(InvalidAgentResponseError, match='no events'):
3621+
await request_handler.on_message_send(
3622+
params, create_server_call_context()
3623+
)

0 commit comments

Comments
 (0)