Repository navigation
fix: guard terminal state transitions and make cancelTask atomic - #636
Merged
JakubWorek merged 4 commits intoAug 26, 2026
Merged
Conversation
🧪 Code Coverage
Generated by coverage-comment.yml |
Member
ez-lbz
force-pushed
the
fix/state-machine-cancel-race
branch
from
August 25, 2026 14:37
32415ab to
bc3929e
Compare
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
force-pushed
the
fix/state-machine-cancel-race
branch
from
August 25, 2026 15:56
5ea9e75 to
bc3929e
Compare
JakubWorek
approved these changes
Aug 26, 2026
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]>
1 task done
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changed
1. Terminal states can no longer be overwritten
Problem:
ResultManager.applyStatusUpdateassignedtask.status = structuredClone(updateEvent.status)unconditionally. A late status update could roll aCOMPLETED/FAILED/CANCELED/REJECTEDtask back into a non-terminal state (or a different terminal state) — the same blind-overwrite gap as Python'stask_manager.py:203-211.Fix (src/server/result_manager.ts):
applyStatusUpdatenow 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 finalCOMPLETEDmessage arriving after theCOMPLETEDstatus) are still applied.2. Make
cancelTask's check-then-act atomicProblem:
cancelTaskloaded the task, checked its state, setCANCELED, and saved — with no lock between check and save. ACOMPLETEDstatus written by a racing executor in that window could be clobbered by theCANCELEDwrite (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):
CANCELEDstatus update through a freshResultManager.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, theCANCELEDupdate is rejected andcancelTasksurfacesTaskNotCancelableErrorinstead of corrupting the task.Testing
npm test— 68 test files, 1510 tests passed.npx tsc --noEmit(no errors insrc/) andnpx tsc --noEmit -p tsconfig.test.jsonpass.ResultManager: aWORKINGstatus update on aCOMPLETEDtask is ignored; aCANCELEDupdate on aCOMPLETEDtask is ignored; a same-stateCOMPLETEDupdate with a final message is still applied.cancelTaskvs a racing executor that completes the task between the initial load and the locked write:TaskNotCancelableErroris thrown and noCANCELEDwrite reaches the store.default_request_handler.spec.ts,auth_required.spec.ts) returned live references fromload; they now deep-clone like the realInMemoryTaskStore, 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
TaskNotCancelableErrorinstead of overwritingCOMPLETEDwithCANCELED.