fix: avoid 500s when a2a cleanup is cancelled#1268
fix: avoid 500s when a2a cleanup is cancelled#1268lithammer wants to merge 1 commit intokagent-dev:mainfrom
Conversation
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
| # Make a best-effort attempt to clean up resources. | ||
| await self._queue_manager.close(task_id) | ||
| async with self._running_agents_lock: | ||
| self._running_agents.pop(task_id, None) |
|
Or would you prefer I try to solve this upstream in https://github.com/a2aproject/a2a-python? |
|
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.