Skip to content

fix(taskexec): ignore stale snapshots when cancelling input-required tasks - #505

Closed
ickeep wants to merge 2 commits into
a2aproject:mainfrom
HuashiCloud:codex/fix-cancel-upstream
Closed

ickeep wants to merge 2 commits into
a2aproject:mainfrom
HuashiCloud:codex/fix-cancel-upstream

Conversation

@ickeep

@ickeep ickeep commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

In cluster mode, CancelTask reads an old remoteSubscription snapshot before observing the cancellation worker's result. An INPUT_REQUIRED snapshot is treated as a final event for that Send execution, so cancellation immediately returns ErrTaskNotCancelable even though the worker can subsequently persist CANCELED.

This change only adjusts distributedManager.Cancel. It skips an old nonterminal snapshot, filters stale events using the task version read before enqueueing, and waits for the cancellation result. Using the pre-enqueue watermark preserves genuine refusal events from a fast worker. Subsequent INPUT_REQUIRED Task or Status events still return the existing refusal error. Send and Subscribe retain snapshot-first behavior. No polling, retries, service calls, API changes, or storage changes are introduced.

Validation on the implementation commit

  • The regression fails deterministically on upstream main 2692308d0926f05fa8e7892e2dacb099848200fe and passes with the fix.
  • Four test groups with ten subtests pass, including five repeated race runs.
  • Coverage includes INPUT_REQUIRED, WORKING, AUTH_REQUIRED, fast and slow Task/Status refusal, stale versions, deadline failure, and existing cancellation, subscription, and continuation behavior.
  • go test -race -mod=readonly -count=1 -shuffle=on ./..., go build -mod=readonly ./..., and go vet ./... pass.

The independent empty-slice fix is #504. This PR does not depend on it. Existing taskstore fallback error classification is retained, including its existing deadline-sentinel mapping limits.

@nahapetyan-serob

Copy link
Copy Markdown
Contributor

Please translate all the PR title, description, and comments into English for review.

@ickeep ickeep changed the title fix(taskexec): 取消等待输入任务时忽略旧快照 fix(taskexec): ignore stale snapshots when cancelling input-required tasks Nov 5, 2026
yarolegovich added a commit that referenced this pull request Nov 5, 2026
* `TestJSONRPC_DeletePushConfigResponse` ignores the ErrorInfo
`timestamp` when comparing responses, as the error conversion tests in
`internal/jsonrpc`, `internal/rest` and `internal/grpcutil` already do.
The expected error is built when the test cases are declared, and
`jsonrpc.ToJSONRPCError` stamps the current time with second precision,
so the test failed whenever the handler ran in the next second.

The test failed this way today on #505, #531 and #536. Sleeping until
the next second before calling the handler fails the old test in every
run and passes with this change.
return newRemoteSubscription(queue, m.taskStore, taskID), nil
}

// Cancel waits for the cancellation result without treating an old input-required snapshot as a refusal.

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.

let's remove, it describes the change, not the method

Comment on lines +199 to +200
// The old nonterminal snapshot provides an event watermark, not a cancellation result.
// Send and Subscribe retain remoteSubscription behavior. Cancel consumes subsequent worker events.

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.

same here, let's drop. the comment on the watermark below is the useful one

Comment on lines +203 to 231
snapshot, snapshotErr := m.taskStore.Get(ctx, req.ID)
if snapshotErr != nil && !errors.Is(snapshotErr, a2a.ErrTaskNotFound) {
subscriptionErr = fmt.Errorf("task snapshot loading failed: %w", snapshotErr)
} else {
// Use the version before enqueueing so a fast worker refusal is not hidden by the later snapshot.
snapshotVersion := storedTask.Version
if snapshot != nil {
if snapshot.Task.Status.State.Terminal() {
return convertToCancelationResult(ctx, snapshot.Task, nil)
}
}
if a2aevent.IsFinal(event) {
if result, ok := event.(*a2a.Task); ok {
return convertToCancelationResult(ctx, result, nil)
for {
msg, readErr := queue.Read(ctx)
if readErr != nil {
subscriptionErr = readErr
break
}
if msg.TaskVersion != taskstore.TaskVersionMissing && !msg.TaskVersion.After(snapshotVersion) {
continue
}
// A subsequent INPUT_REQUIRED event still indicates refusal and must remain observable.
if a2aevent.IsFinal(msg.Event) {
if result, ok := msg.Event.(*a2a.Task); ok {
return convertToCancelationResult(ctx, result, nil)
}
break
}
break
}
}

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 duplicates remoteSubscription.Events. let's add options to remoteSubscription instead (don't yield a nonterminal snapshot, use a given version as the watermark) or at least move the loop to a helper and return early instead of nesting under else

Comment on lines +209 to 213
if snapshot != nil {
if snapshot.Task.Status.State.Terminal() {
return convertToCancelationResult(ctx, snapshot.Task, nil)
}
}

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.

nit:

Suggested change
if snapshot != nil {
if snapshot.Task.Status.State.Terminal() {
return convertToCancelationResult(ctx, snapshot.Task, nil)
}
}
if snapshot != nil && snapshot.Task.Status.State.Terminal() {
return convertToCancelationResult(ctx, snapshot.Task, nil)
}

}

// newCancelSnapshotRuntime uses the real handler, in-memory store, and executor, with channels controlling worker ordering.
func newCancelSnapshotRuntime(t *testing.T, initial, final a2a.TaskState, fullTask, fast, replayOld bool) *cancelSnapshotRuntime {

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.

I'd rather add cases to TestClusterFrontend_Cancel in distributed_manager_test.go, it already sequences store results. a test queue returning given events and versions should cover the rest. three positional bools are also hard to read at call sites

Comment on lines +66 to +67
// Cancel uses the first read for admission and the second for the initial snapshot. The worker does not inherit the test marker.
if ctx.Value(cancelSnapshotContextKey{}) == true && reads.Add(1) == 2 {

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 breaks as soon as Cancel adds or removes a Get call

writes *atomic.Int32
}

// newCancelSnapshotRuntime uses the real handler, in-memory store, and executor, with channels controlling worker ordering.

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.

nit: no need to document test helpers and test funcs, here and in other places

t.Parallel()
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
runtime := newCancelSnapshotRuntime(t, state, a2a.TaskStateCanceled, false, false, false)

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.

nit: runtime -> rt, shadows the stdlib package name

@yarolegovich

Copy link
Copy Markdown
Member

Wanted to ship this in 2.9.0, so fixed in #560

yarolegovich added a commit that referenced this pull request Nov 6, 2026
#560)

Supersedes #505 with the review comments applied. Thanks @ickeep for the
original fix.

- Added `newRemoteCancelSubscription`, used by
`distributedManager.Cancel`. It emits the stored snapshot only when it
is terminal, because `IsFinal` treats `INPUT_REQUIRED` as final and an
input-required snapshot was returned as a cancelation refusal before the
worker could persist `CANCELED`.
- The cancel subscription skips events not newer than the task version
loaded before the cancel request is written, so a refusal persisted by a
fast worker is still observed.
- `Execute` and `Resubscribe` subscriptions still emit the snapshot
first.

Co-authored-by: ickeep <[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.

3 participants