Skip to content

fix(taskexec): report canceler panics during concurrent cancelation - #535

Merged
yarolegovich merged 3 commits into
a2aproject:mainfrom
ShivamSharma43:fix-taskexec-cancel-panic
Nov 6, 2026
Merged

yarolegovich merged 3 commits into
a2aproject:mainfrom
ShivamSharma43:fix-taskexec-cancel-panic

Conversation

@ShivamSharma43

Copy link
Copy Markdown
Contributor

When CancelTask runs while the task's execution is still active, localManager.handleCancelWithConcurrentRun writes the cancelation to the execution's pipe. That calls AgentExecutor.Cancel. The function recovers panics from that path, but the recovery had two bugs:

  • Defer order. The recover defer was registered before the defer that calls cancel.result.signalDone(). Defers run last-in, first-out, so signalDone ran first, and CancelTask waiters were unblocked before setError was called. When the waiter saw the result, the error usually wasn't set yet, and setError then raced with the waiter's read of p.err.
  • Shadowed error. Without a PanicHandler, err := fmt.Errorf(...) declared a new variable inside the else block. setError therefore always got nil.

As a result, a panicking AgentExecutor.Cancel resolved the cancelation to (nil, nil). convertToCancelationResult then reported bug: execution result was a message: task cannot be canceled instead of the panic.

The fix registers the recover defer after the signalDone defer, so it runs first and the error is set before waiters are unblocked. It also assigns the default panic error to the outer err. handleCancel and the execution path don't need this change: they go through runProducerConsumer, whose recovery is already correct.

Tests (written first):

  • TestManager_CancelerPanicFailsConcurrentCancelation makes testCanceler panic while an execution is active. It runs two cases: the default panic handling and a custom PanicHandler. It checks that Cancel returns the panic error. On main, the default case fails with bug: execution result was a message: task cannot be canceled. The custom-handler case passes only because of the race described above.

Validation (Go 1.26.9, Windows):

  • go test ./internal/taskexec/ ./a2asrv/... passes, and the new test passes with -count=20. I couldn't run -race locally because there is no 64-bit cgo toolchain.
  • go vet ./internal/taskexec/ is clean. golangci-lint run ./internal/taskexec/... (v2.15.0) reports nothing on the changed lines. Its remaining findings are CRLF goimports hits in untouched files, which come from the Windows checkout, and one existing errcheck hint at manager_test.go:531.

🤖 Generated with Claude Code

handleCancelWithConcurrentRun recovered canceler panics in a defer that
ran after the one calling signalDone, so CancelTask waiters were
unblocked before the error was set. Without a PanicHandler the error was
also assigned to a shadowed variable, so the cancelation resolved to
(nil, nil) and CancelTask failed with the misleading "bug: execution
result was a message: task cannot be canceled".

Register the recover defer last so it runs before signalDone, and assign
the default panic error to the outer variable.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 10:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Comment thread internal/taskexec/local_manager.go Outdated
m.mu.Unlock()
}()

// Deferred after signalDone so that it runs first and the error is set before waiters are unblocked.

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: I'd merge it into the defer above, then the order is explicit and we don't need the comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 18e8e0f. The recover now sits at the top of that defer, before signalDone, so the order is explicit and the comment is gone.

Comment thread internal/taskexec/local_manager.go Outdated
Comment on lines 372 to 383
defer func() {
if r := recover(); r != nil {
var err error
if m.panicHandler != nil {
err = m.panicHandler(r)
} else {
err := fmt.Errorf("task cancelation panicked: %v\n%s", r, debug.Stack())
err = fmt.Errorf("task cancelation panicked: %v\n%s", r, debug.Stack())
log.Warn(ctx, "cancelation panicked", "error", err)
}
cancel.result.setError(err)
}
}()

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.

cleaner.Cleanup is skipped when Cancel panics, so AgentExecutionCleaner doesn't get the error like it does in handleCancel. let's recover around canceler.Cancel and let the error branch below handle it. let's please also check it in the test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. In 18e8e0f, canceler.Cancel runs through a small cancelOnPipe helper that recovers and turns the panic into an error (using PanicHandler when set). The existing error branch then calls cleaner.Cleanup(ctx, nil, err) and sets the error, the same as handleCancel. The deferred recover stays for panics elsewhere, such as in CreateCanceler or Cleanup. The test now uses a recording cleaner and checks that Cleanup gets the error Cancel returns. Both cases fail on the previous commit with cleaner.Cleanup() error = <nil>.

…ation

Recover around canceler.Cancel so a panic goes through the error branch,
which calls cleaner.Cleanup with the error as handleCancel does. Merge the
recover into the defer that signals the cancelation result.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@yarolegovich
yarolegovich enabled auto-merge (squash) November 6, 2026 11:37
@yarolegovich
yarolegovich merged commit 7ffc680 into a2aproject:main Nov 6, 2026
5 checks passed
yarolegovich added a commit that referenced this pull request Nov 6, 2026
🤖 I have created a release *beep* *boop*
---


## [2.8.0](v2.7.0...v2.8.0)
(2026-11-06)


### Features

* **a2aacp:** add an A2A agent executor for Agent Client Protocol (ACP)
agents ([#491](#491),
[#492](#492),
[#493](#493),
[#494](#494),
[#530](#530),
[#545](#545),
[#548](#548),
[#552](#552),
[#562](#562))
* **a2agrpc:** add GRPCConnectionPool for gRPC client connection reuse
(fixes [#109](#109))
([#382](#382))
([a2411f7](a2411f7))
* **a2asrv:** add NewErrorSanitizer, an opt-in interceptor which logs
request errors in full and sends clients only A2A errors
([#552](#552))
([ece71bc](ece71bc))
* **a2asrv:** agent card HTTP caching (ETag/304, opt-in Cache-Control)
([#496](#496))
([313efd9](313efd9))
* implement structured input extension
([#490](#490))
([367f987](367f987))


### Bug Fixes

* **a2aclient:** honor API key security scheme Location
([#497](#497))
([c59beb5](c59beb5))
* **a2aclient:** honor configured HTTP authentication schemes
([#502](#502))
([413de0c](413de0c))
* **a2aclient:** normalize service param keys set by the client and
AuthInterceptor
([#479](#479))
([96f4df0](96f4df0))
* **a2aclient:** return JSON-RPC errors from non-200 responses
([#559](#559))
([97f1fac](97f1fac))
* **a2acrypto:** reject malformed Unicode before signing
([#450](#450))
([ec6cb14](ec6cb14))
* **a2aevent:** preserve input-required history order
([#501](#501))
([72f94a3](72f94a3))
* **a2aevent:** recover the whole artifact when it changes in place
([#485](#485))
([4d61739](4d61739))
* **a2a:** round-trip null data and nil raw parts through JSON
([#513](#513))
([186decc](186decc))
* **a2asrv:** make ServiceParams.With nil-safe
([#544](#544))
([c6042b8](c6042b8))
* **a2asrv:** preserve escaped tenant REST paths
([#514](#514),
[#562](#562))
([f86cf6c](f86cf6c))
* **a2asrv:** preserve numeric precision in raw card ETags
([#546](#546))
([18c2e72](18c2e72))
* **a2asrv:** reject HEAD before creating task subscriptions
([#481](#481))
([e641e3c](e641e3c))
* **a2asrv:** split comma-separated A2A-Extensions values
([#519](#519))
([db83820](db83820)),
closes [#516](#516)
* **a2av0:** keep security schemes and v0.3 interface in extended agent
cards ([#558](#558))
([196d4bb](196d4bb)),
closes [#556](#556)
* **client:** accept cached status and artifact stream events
([#508](#508))
([ea78132](ea78132))
* **client:** honor streaming interceptor error recovery
([#507](#507))
([737b626](737b626))
* **client:** keep propagated tenants local to each call
([#495](#495))
([0eaf017](0eaf017))
* **client:** preserve escaped REST path segments
([#488](#488))
([8880d8f](8880d8f))
* compare and send A2A-Version as Major.Minor
([#555](#555))
([b5ea753](b5ea753))
* don't fail CancelTask when the canceled execution ends first
([#521](#521))
([1613ae6](1613ae6)),
closes [#518](#518)
* don't move a task to FAILED when cancelation retries run out
([#522](#522))
([247933b](247933b))
* **jsonrpc:** return empty result for successful push config deletion
([#525](#525))
([42d0738](42d0738))
* pass CancelTask metadata over REST
([#520](#520))
([a104b2e](a104b2e)),
closes [#517](#517)
* **pbconv:** preserve JSON data part values
([#503](#503))
([7c040e0](7c040e0))
* **pbconv:** send pkceRequired for authorization code flows
([#542](#542))
([cb2c30b](cb2c30b))
* **push:** cap push notification configs per task
([#405](#405))
([6bb5a68](6bb5a68))
* report executions or cancelations in progress, and messages to
terminal tasks in cluster mode, as UnsupportedOperation
([#552](#552))
([ece71bc](ece71bc))
* **sse:** ignore a leading UTF-8 byte order mark
([#511](#511))
([248f628](248f628))
* **taskexec:** keep the error when broadcasting an event fails
([#536](#536))
([636fa9f](636fa9f))
* **taskexec:** report canceler panics during concurrent cancelation
([#535](#535))
([7ffc680](7ffc680))
* **taskexec:** skip heartbeats for non-positive intervals
([#543](#543))
([c4158ae](c4158ae))
* **taskexec:** wait for the cancelation result of input-required tasks
([#560](#560))
([3194abe](3194abe))

---
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: Yaroslav Shevchuk <[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