a2aproject/a2a-python

[Bug]: Agent Execution Cancelled by Client-Side Cancellation During message/stream

yokonao opened this issue · 4 comments

What happened?

Currently, with the default DefaultRequestHandler implementation, if a client that sent a message/stream request cancels the request or unexpectedly loses connection, the agent's execution is cancelled.

I've confirmed this behavior using the following steps:

  1. Modified a2a-samples/samples/python/agents/helloworld to include await asyncio.sleep(1) to simulate a slightly longer agent execution time.

  2. Started the A2A server.

  3. Sent a message/stream request using curl and immediately cancelled it.

    curl -X POST \
        -i -s \
        --header "Content-Type: application/json" \
        --data "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"message/stream\",\"params\":{\"message\":{\"role\":\"user\",\"parts\":[{\"kind\":\"text\",\"text\":\"テストメッセージ\"}],\"messageId\":\"some-id\"}}}" \
        http://localhost:9999/
    

The server displayed the following error:

asyncio.exceptions.CancelledError: Cancelled by cancel scope 1074c75d0

Considering the existence of the tasks/resubscribe RPC method, it seems unintended for agent execution to be cancelled due to a disconnection mid-request. I've confirmed that this behavior does not occur with message/send requests.

Environment

❯ uv run python --version
Python 3.11.13

a2a-sample commit hash: 3ac8e0b44446edede8f7ea398d48c758285f9eeb

Relevant log output

Code of Conduct

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

The issue was fixed in the following commit: 2508a9b

I was able to recreate the bug with the example that you provided above. I upgraded the a2a-sdk package to 0.3.2 (from 0.3.0) and that resolved the issue.

The issue was fixed in the following commit: 2508a9b

I was able to recreate the bug with the example that you provided above. I upgraded the a2a-sdk package to 0.3.2 (from 0.3.0) and that resolved the issue.

Tried the solution, Still an issues, not working for me.
Can anyone suggests the fix

This specific issue was definitely resolved. If you have the same error, can you confirm whether the a2a-sdk package was updated in the uv.lock file? It took me some effort to update the uv.lock despite updating the pyproject.toml. Otherwise can you post the exact error trace?

I am a colleague of @yokonao. We have confirmed that the original issue has been fixed—thank you for the quick resolution.

However, after this fix we discovered a new issue: when the client disconnects, TaskStore.save is no longer called.

When the client-side connection is terminated, the EventConsumer stops processing, so even if the task state changes afterwards, those updates are not persisted to the TaskStore. As a result, the task continues running in the background, but its state is no longer reflected in the TaskStore.

Steps to Reproduce

Implement HelloWorldAgentExecuter.execute as follows:

from a2a.server.tasks import TaskUpdater
from a2a.utils import new_task

task = new_task(context.message)
updater = TaskUpdater(event_queue, task.id, task.context_id)
await updater.start_work()
await asyncio.sleep(5)
result = await self.agent.invoke()
await updater.complete(new_agent_text_message(result))

Add the following logger setup in __main__.py:

task_store_logger = logging.getLogger('a2a.server.tasks.inmemory_task_store')
task_store_logger.setLevel(logging.DEBUG)
task_store_logger.addHandler(logging.StreamHandler(sys.stdout))

With @yokonao’s reproduction steps, the normal (non‑disconnected) run produces logs like:

Attempting to get task with id: 42b2b321-37d9-4764-921c-2d2d0237be78
Task 42b2b321-37d9-4764-921c-2d2d0237be78 not found in store.
Task 42b2b321-37d9-4764-921c-2d2d0237be78 saved successfully.
Task 42b2b321-37d9-4764-921c-2d2d0237be78 saved successfully.
Task 42b2b321-37d9-4764-921c-2d2d0237be78 saved successfully.

If the client disconnects mid‑stream, the last “saved successfully” line is missing.

Fundamentally, the issue is that when the client disconnects the EventConsumer stops, and subsequent state updates are no longer persisted by the TaskManager. This means the task’s actual progress and the persisted state diverge after a disconnect. We would appreciate a fix.

Let me know if you’d like me to open a separate issue for this follow‑up. Thank you.

Additional Information (2025-09-08)

This issue was closed but my issue is not resolved. I opened a new issue: #464