Repository navigation
Conversation
1c26cc0 to
3c8fd2c
Compare
|
We have rights to resolve the conflicts and resolve the noted issues. Depending on if ez-lbz would rather run with it instead. |
|
Thanks — please go ahead, the PR is yours to run with. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@ez-lbz @chopmob-cloud — could you both please consider the following detailed race-condition review before this is merged? I would not merge the current revision yet. I found one critical race introduced by the latest cleanup design, three additional high-confidence concurrency bugs, and several secondary issues.
1. Streaming ownership is released before persistence completes
A valid interleaving is:
This permits a new handler to start before the preceding operation's final state is persisted. The new The durable fix is a server-owned event pump that drains the agent queue, persists each event, forwards the persisted event to the client-facing channel, and releases the reservation only after that pump completes. 2. Stream cancellation loses the current eventThe streaming loop dequeues a response and then calls That conflicts with reconnect/resubscription semantics because 3. Concurrent subscriber creation can be orphaned
B can then wait forever because its channel is neither notified nor completed. The existing concurrency test checks only for exceptions/deadlocks, not delivery. Creation and last-channel removal need common per-task serialization or reference-counted subscriber-set retirement. 4. Blocking sends are outside the operation-reservation modelOnly return-immediately and streaming requests reserve an operation. Default blocking sends do not reserve the task, use only the request token, are absent from Consequences include:
Every task-associated send should use the same operation lease. Reservation, terminal recheck, and history append should occur atomically, and blocking handlers should receive a linked caller/per-task cancellation token. 5. Terminal tasks still accept artifact mutations
This conflicts with terminal-task immutability and the rule that no further stream updates follow a terminal state. Artifact application should use the same terminal guard, with regression tests for all terminal states. 6. Old streaming drains can remove newer trackingStreaming cleanup releases the reservation and then calls Removal needs to be identity/ownership-safe rather than key-only. A single operation object holding the CTS, handler task, persistence task, and drain completion would simplify this ownership considerably. 7. Locking does not extend across server instancesAll atomicity depends on a process-local If the SDK supports only one server instance per task partition, that restriction needs documentation. Otherwise the store contract needs optimistic concurrency or conditional writes. A2A specification comparisonThe current implementation can violate the latest lifecycle expectations because generated events may be lost after streaming disconnection, a continuation can begin before the preceding final state is durable, terminal tasks can receive artifact updates, cancellation does not stop blocking execution, and subscriber reconnection can silently lose live events. Relevant references: |

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.