fix: enforce terminal state transitions and serialize cancel check-then-act - #453
Open
ez-lbz wants to merge 2 commits into
Open
fix: enforce terminal state transitions and serialize cancel check-then-act#453ez-lbz wants to merge 2 commits into
ez-lbz wants to merge 2 commits into
Conversation
ez-lbz
force-pushed
the
fix/state-machine-cancel-race
branch
from
August 11, 2026 13:37
1c26cc0 to
3c8fd2c
Compare
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 state transitions are enforced in
TaskProjectionProblem:
TaskProjection.ApplyStatusunconditionally overwrotecurrent.Status, andApplyMessageunconditionally appended to history. A task in a terminal state (COMPLETED/CANCELED/FAILED/REJECTED) could be overwritten back toSUBMITTEDor accept further messages via direct state assignment, whenever events bypassed the request-level terminal checks (e.g. a misbehaving agent handler emitting events after a terminal status).Fix (src/A2A/Server/TaskProjection.cs):
GuardNotTerminal, called from bothApplyStatusandApplyMessage.A2AExceptionwithA2AErrorCode.UnsupportedOperation(-32004, the same codeA2AServeruses for terminal-state rejections) instead of being mutated.2.
CancelTaskAsynccheck-then-act is serialized under the per-task lockProblem:
CancelTaskAsyncread the task state and checked terminal status with no lock between the check and the state transition. Two concurrent cancel requests could both pass the check and both run the cancel handler (TOCTOU), and cancel could race withmessage/sendmutations.Fix (src/A2A/Server/A2AServer.cs):
_notifier.AcquireTaskLockAsync(request.Id), the same per-task lockApplyEventAsyncandSubscribeToTaskAsyncuse.TaskNotCancelableinstead of double-cancelling.ApplyEventAsyncwas refactored into a lock-acquiring wrapper plusApplyEventUnderLockAsync(the locked body), so the cancel path can apply events without re-acquiring the non-reentrant per-task semaphore (which would deadlock).Testing
dotnet test tests/A2A.UnitTests --framework net8.0— 434 passed, 0 failed (baseline 420, +14 new regression tests: status-overwrite and message-append rejection for all four terminal states, same-terminal-state re-emission rejection, artifact-update tolerance on terminal tasks, and a two-concurrent-cancel test asserting exactly one succeeds while the other getsTaskNotCancelable).dotnet test tests/A2A.AspNetCore.UnitTests --framework net8.0— 88 passed, 0 failed (unchanged).UnsupportedOperationerror (previously silently applied). Well-behaved handlers are unaffected; cancel still transitionsWORKING→CANCELEDas before.