Repository navigation
Commit 2cc1dd5
fix(server): run after_handler_run when a handler call is cancelled (a2aproject#1427)
# Description
Follow-up to a2aproject#1402, as asked in the review there.
`server_plugin_hooks` handled cancellation differently for unary and
streaming methods, and neither told the plugin about it.
- The unary wrapper only called `after_handler_run` from `except
Exception`. A cancelled call (a client timeout, a disconnect,
`asyncio.wait_for` around the handler) got `before_handler_run` and then
nothing, so a plugin that opens a span or takes a slot in
`before_handler_run` never got to close it.
- The stream wrapper does call it from `finally`, but with `error=None`,
so a cancelled stream looked the same as one that finished normally.
Both wrappers now catch `asyncio.CancelledError` alongside `Exception`,
pass it to `after_handler_run` as `error`, and re-raise it.
`ServerResponse.error` is already typed `BaseException | None`. A stream
closed with `aclose()` still reports `error=None`, so
`test_stream_closed_early_still_gets_final_call` is unchanged.
The two new tests use a task store whose `get` blocks, so they cancel
inside `on_get_task` and `on_subscribe_to_task` without going through
`ActiveTask.subscribe` and don't depend on a2aproject#1402. Without the `src`
change the unary test gets no `after_handler_run` call at all and the
stream test gets `error=None`.
Full suite on 3.10: 2544 passed, 207 skipped, 4 xfailed, 1 xpassed.
`./scripts/lint.sh` passes (the three ty warnings it prints are the
existing unused `type: ignore` in the client transports).
I left the client side alone. `BaseClient._execute_with_interceptors`
has the same `except Exception` shape for `after_client_request`, and
`PluginAgentExecutor.execute` passes `None` to `after_execution` when
the execution is cancelled. I can send those separately if you want
them.
- [x] Follow the [`CONTRIBUTING`
Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md).
- [x] Make your Pull Request title in the
<https://www.conventionalcommits.org/> specification.
- [x] Ensure the tests and linter pass (Run `bash scripts/format.sh`
from the repository root to format)
- [x] Appropriate docs were updated (if necessary)
Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>1 parent 076e0ec commit 2cc1dd5
2 files changed
Lines changed: 63 additions & 2 deletions
File tree
- src/a2a/server/request_handlers
- tests/server/request_handlers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
85 | | - | |
| 86 | + | |
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
| |||
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
113 | | - | |
| 114 | + | |
| 115 | + | |
114 | 116 | | |
115 | 117 | | |
116 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
353 | 354 | | |
354 | 355 | | |
355 | 356 | | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
356 | 415 | | |
357 | 416 | | |
358 | 417 | | |
| |||
0 commit comments