Skip to content

fix(server): close cancellation streams on push failure - #554

Merged
muscariello merged 3 commits into
a2aproject:mainfrom
Linux2010:codex/fix-push-cancel-cleanup
Nov 25, 2026
Merged

muscariello merged 3 commits into
a2aproject:mainfrom
Linux2010:codex/fix-push-cancel-cleanup

Conversation

@Linux2010

@Linux2010 Linux2010 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When cancellation persists a task as CANCELED but sending its cancellation event fails, the cancel request could return before notifying attached subscribers or removing the active execution. A stream could remain blocked, and an earlier event delayed in push delivery could subsequently restore an older active snapshot.

  • On push delivery failure, publish the committed CANCELED status to attached subscribers and return the delivery error to the cancel caller.
  • Guard completion after begin_cancel returns, so errors applying cancel events and a dropped cancel future also finish the attached execution. The guard captures the runtime handle and resolves a pending registry lookup if necessary.
  • Preserve a terminal active snapshot and order event broadcasts with snapshot updates, so an in-flight older event cannot replace or follow it.
  • Add regression coverage for the cancel caller and attached subscriber, task-store failure, a deterministic stale-event race, cancellation while the execution registry is locked, and guard drop outside the runtime thread.

The guard schedules asynchronous cleanup when dropped; it cannot guarantee cleanup after the Tokio runtime has shut down or when a custom begin_cancel persists its transition but is dropped before returning. The public TaskStore contract does not provide conditional updates, so this change does not guarantee atomicity between task persistence and cancellation for every custom store. An already in-flight webhook can also still arrive after cancellation.

Rebased onto main after #556 merged, preserving its empty-cancellation notification and regression test.

Validation

  • cargo test -p a2a-server-lf --locked --offline: 293 unit tests, 8 integration tests, and 1 doc test passed.
  • cargo clippy -p a2a-server-lf --all-targets --locked --offline -- -D warnings.
  • cargo fmt --all -- --check and git diff --check.

Tests used the local cached registry mirror and the installed macOS Command Line Tools SDK.

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.64341% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
a2a-server/src/handler.rs 98.64% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

@muscariello muscariello left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closes a real gap: a cancel whose push delivery fails after CANCELED is committed returns early, and attached subscribers never see the end. Taking the write lock across the sequence assignment and the send, and dropping publishes once the snapshot is terminal, also stops a held-up older event from overwriting the cancellation. The gated push store test shows that race well.

  1. Subscribers get the push error, not the cancellation. On the failure path active.publish(Err(&error), …) sends the delivery error to everyone watching, although the cancel succeeded and the delivery problem belongs to the CancelTask caller. The error is whatever the sender returned, for example push URL targets blocked host as INVALID_PARAMS. Publishing the CANCELED status update (as #556 does) to subscribers and returning the push error only from cancel_task gives them the right final state.
  2. One ? is patched, not the class. apply_event_to_task(…)? just above also returns before finish. A guard that finishes the attached execution on drop, next to ReleaseOnDrop, would cover every early return.
  3. This conflicts with #556 in handler.rs; please rebase once that lands.

The cancel and push tests pass locally on this head.

@Linux2010
Linux2010 force-pushed the codex/fix-push-cancel-cleanup branch from cf3b3c9 to ec7f2bb Compare November 24, 2026 22:57

@muscariello muscariello left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three points are in, and the rebase is done. Subscribers now get the committed CANCELED update while the push error goes only to the caller. FinishCancelOnDrop covers the apply-error ?, the push failure and an aborted cancel call. After the rebase on #556, its empty-cancellation notification is folded into the guard (finish(!terminal_pushed)) and its regression test is kept. On this head a2a-server-lf passes (293 unit, 8 integration, 1 doc), the 30 cancel tests passed 25 runs in a row, and CI is green.

@muscariello
muscariello added this pull request to the merge queue Nov 25, 2026
Merged via the queue into a2aproject:main with commit f61f4a7 Nov 25, 2026
22 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 25, 2026
joulook added a commit to joulook/a2a-rs that referenced this pull request Nov 26, 2026
test_cancel_push_failure_ends_running_stream (a2aproject#554) read the stream's first
item as the working status. Since the stream now begins with the Task object,
the test takes the task id from that item and reads the working status second.

Signed-off-by: Mohammad Hossein Khoshechin Jorshari <[email protected]>
gulldan pushed a commit to gulldan/a2a-rs that referenced this pull request Nov 27, 2026
…#587)

* fix(a2a-server): begin a task stream with the task object

Spec 3.1.2: "If the agent returns a `Task`, the stream MUST begin with
the Task object". `send_streaming_message` relayed the executor's first
event, and the examples, ITK and ACTS agents open with a status update.

`start_execution` also returns the task the execution starts from (a
new SUBMITTED task, or the stored task with the turn's message), taken
before the executor starts. When the stream's first item is a status
or artifact update, `send_streaming_message` emits that task first; it
is the state before the update, so nothing is delivered twice. A first
Task, a Message, an error, `send_message`, subscriptions and push
notifications are unchanged.

Fixes: a2aproject#585
Signed-off-by: Mohammad Hossein Khoshechin Jorshari <[email protected]>

* docs(a2a-server): show the task first in the quickstart stream samples

The streaming send now begins with the task. The quickstart's
`SendStreamingMessage` sample has six events, the first a `task` in
`TASK_STATE_SUBMITTED`, and the a2acli page's `send --stream` sample
begins with a `State: SUBMITTED` block. Both match the greeter's
output on this branch.

Signed-off-by: Mohammad Hossein Khoshechin Jorshari <[email protected]>

* test(a2a-server): expect the task first in the push-failure stream test

test_cancel_push_failure_ends_running_stream (a2aproject#554) read the stream's first
item as the working status. Since the stream now begins with the Task object,
the test takes the task id from that item and reads the working status second.

Signed-off-by: Mohammad Hossein Khoshechin Jorshari <[email protected]>

* test(a2a-server): leave no unreachable line in the task-first tests

The shape checks of the task-first tests, and the first-item check of the
push-failure test, had a failure arm that never runs, which the patch coverage
counted as uncovered lines. The tests now assert the stream's shape and take
each item through one test macro, so the failure paths no longer occupy lines
of their own.

Signed-off-by: Mohammad Hossein Khoshechin Jorshari <[email protected]>

---------

Signed-off-by: Mohammad Hossein Khoshechin Jorshari <[email protected]>
Co-authored-by: Luca Muscariello <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants