Skip to content

fix: guard terminal state transitions and make cancelTask atomic - #636

Merged
JakubWorek merged 4 commits into
a2aproject:mainfrom
ez-lbz:fix/state-machine-cancel-race
Aug 26, 2026
Merged

JakubWorek merged 4 commits into
a2aproject:mainfrom
ez-lbz:fix/state-machine-cancel-race

Conversation

@ez-lbz

@ez-lbz ez-lbz commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

What changed

1. Terminal states can no longer be overwritten

Problem: ResultManager.applyStatusUpdate assigned task.status = structuredClone(updateEvent.status) unconditionally. A late status update could roll a COMPLETED/FAILED/CANCELED/REJECTED task back into a non-terminal state (or a different terminal state) — the same blind-overwrite gap as Python's task_manager.py:203-211.

Fix (src/server/result_manager.ts):

  • applyStatusUpdate now checks the persisted state: if it is terminal and the incoming update has a different state, the update is ignored (with a warning). Same-state terminal updates (e.g. a final COMPLETED message arriving after the COMPLETED status) are still applied.

2. Make cancelTask's check-then-act atomic

Problem: cancelTask loaded the task, checked its state, set CANCELED, and saved — with no lock between check and save. A COMPLETED status written by a racing executor in that window could be clobbered by the CANCELED write (or the cancel would win and corrupt the completed task), a TOCTOU race shared by most SDKs.

Fix (src/server/request_handler/default_request_handler.ts):

  • The no-bus cancel path now routes the CANCELED status update through a fresh ResultManager.processEvent(...) instead of mutating the loaded task and saving directly. That applies the per-(tenant, owner, taskId) write lock and the new terminal-state guard: if the executor already completed the task, the CANCELED update is rejected and cancelTask surfaces TaskNotCancelableError instead of corrupting the task.

Testing

  • npm test — 68 test files, 1510 tests passed.
  • npx tsc --noEmit (no errors in src/) and npx tsc --noEmit -p tsconfig.test.json pass.
  • New regression tests:
    • ResultManager: a WORKING status update on a COMPLETED task is ignored; a CANCELED update on a COMPLETED task is ignored; a same-state COMPLETED update with a final message is still applied.
    • cancelTask vs a racing executor that completes the task between the initial load and the locked write: TaskNotCancelableError is thrown and no CANCELED write reaches the store.
  • Test updates required by the behavior change: two mock stores (default_request_handler.spec.ts, auth_required.spec.ts) returned live references from load; they now deep-clone like the real InMemoryTaskStore, so the guard sees the true persisted state. Their assertions (FAILED persisted after a COMPLETED save failure) are unchanged.

Behavior change: status updates that would move a terminal task to a different state are dropped; canceling a task that a racing executor just completed fails with TaskNotCancelableError instead of overwriting COMPLETED with CANCELED.

@ez-lbz
ez-lbz requested a review from a team as a code owner August 10, 2026 16:27
@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

🧪 Code Coverage

⬇️ Download Full Report

Base PR Delta
src/server/request_handler/default_request_handler.ts 88.68% 91.72% 🟢 +3.04%
src/server/result_manager.ts 82.19% 83.26% 🟢 +1.07%
Total 91.27% 91.54% 🟢 +0.27%

Generated by coverage-comment.yml

@JakubWorek

Copy link
Copy Markdown
Member

Hi @ez-lbz, looks like this PR contains commit not related to the description, taken from #625. Could you please update the branch to contain only relevant changes and resolve merge conflicts?

@ez-lbz
ez-lbz force-pushed the fix/state-machine-cancel-race branch from 32415ab to bc3929e Compare August 25, 2026 14:37
@ez-lbz

ez-lbz commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Hi @JakubWorek, thanks for the review note. I've updated the branch:

All CI checks (test, test-edge, coverage, eslint, integration, ITK, tck-test, approvals, Validate PR Title) are green on the new head.

Thanks again!

@ez-lbz
ez-lbz force-pushed the fix/state-machine-cancel-race branch from 5ea9e75 to bc3929e Compare August 25, 2026 15:56
@JakubWorek
JakubWorek merged commit 7b87c94 into a2aproject:main Aug 26, 2026
15 checks passed
bartek-gralewicz added a commit that referenced this pull request Sep 18, 2026
🤖 I have created a release *beep* *boop*
---


## [1.2.0](v1.1.0...v1.2.0)
(2026-09-18)


### Features

* **server:** Introduce SettleByTaskId method
([#692](#692))
([e856bda](e856bda))


### Bug Fixes

* accept GetExtendedAgentCard JSON-RPC calls with omitted params
([#687](#687))
([047d970](047d970))
* avoid mutating caller message configuration
([#725](#725))
([e3a6428](e3a6428))
* **client:** cache the extended Agent Card only after signature
verification ([#711](#711))
([55b601c](55b601c))
* guard terminal state transitions and make cancelTask atomic
([#636](#636))
([7b87c94](7b87c94))
* preserve call context for JSON-RPC tenant requests
([#708](#708))
([ae20aca](ae20aca))
* preserve HeadersInit in authenticating fetch
([#721](#721))
([f8c33fa](f8c33fa))
* preserve historyLength=0 and reject invalid REST listTasks pageSize
([#685](#685))
([32c1865](32c1865))
* reject empty task IDs with RequestMalformedError in getTask/cancelTask
([#629](#629))
([71aae97](71aae97))
* reject invalid ListTasks status filters with RequestMalformedError
([#631](#631))
([a881fae](a881fae))
* reject missing required extensions before mutating task history
([#690](#690))
([3b4ef3f](3b4ef3f))
* reject whitespace-only taskId on sendMessage
([#689](#689))
([85227a0](85227a0))
* return 404/-32001 for missing push notification configs
([#630](#630))
([34f06e3](34f06e3))
* scope ExecutionEventBusManager by tenant and owner to match TaskStore
([#707](#707))
([69d8899](69d8899))
* stamp the resolved taskId onto sendMessage push configs
([#688](#688))
([cfb19a8](cfb19a8))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: Bartek Gralewicz <[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