fix: avoid 500s when a2a cleanup is cancelled#1268
Open
lithammer wants to merge 1 commit intokagent-dev:mainfrom
Open
fix: avoid 500s when a2a cleanup is cancelled#1268lithammer wants to merge 1 commit intokagent-dev:mainfrom
lithammer wants to merge 1 commit intokagent-dev:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR prevents duplicate side effects when clients retry on 5xx responses by treating CancelledError during A2A cleanup as non-fatal. Previously, if cleanup was cancelled after a request completed successfully, the server would return a 500 error, triggering client retries and causing duplicate operations (e.g., multiple Slack messages).
Changes:
- Introduced
SafeRequestHandlerthat catchesCancelledErrorduring cleanup and performs best-effort resource cleanup instead of propagating the error - Replaced
DefaultRequestHandlerwithSafeRequestHandlerin the A2A application - Added unit tests verifying that cleanup handles both cancelled and successful task scenarios
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/packages/kagent-adk/src/kagent/adk/_safe_request_handler.py | New handler class that catches CancelledError during cleanup and performs manual resource cleanup |
| python/packages/kagent-adk/src/kagent/adk/_a2a.py | Updated to use SafeRequestHandler instead of DefaultRequestHandler |
| python/packages/kagent-adk/tests/unittests/test_safe_request_handler.py | Added tests for both cancelled and normal cleanup scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
python/packages/kagent-adk/src/kagent/adk/_safe_request_handler.py
Outdated
Show resolved
Hide resolved
Swallow CancelledError during cleanup so successful requests don't turn into 5xx responses. This prevents clients retrying after side effects already happened. Signed-off-by: Peter Lithammer <peter.lithammer@embark-studios.com>
0d98064 to
abeed0d
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.
We hit a case where a client retried on
5xxresponses, and the agent's Slack tool had already executed before the server returned500. The request itself was effectively successful, but cleanup raisedCancelledError, which bubbled into a500and triggered the retry. That led to duplicate Slack messages.This change treats
CancelledErrorduring_cleanup_produceras non‑fatal so a completed request doesn't get turned into a500during cleanup. The result is that clients don't retry after work has already been done, avoiding duplicate side effects.