Repository navigation
Commit 5e0d415
fix(server): close the event queues when ActiveTask.start() fails (a2aproject#1422)
# Description
Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make
sure it goes smoothly:
- [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.
- Important Prefixes for
[release-please](https://github.com/googleapis/release-please):
- `fix:` which represents bug fixes, and correlates to a
[SemVer](https://semver.org/) patch.
- `feat:` represents a new feature, and correlates to a SemVer minor.
- `feat!:`, or `fix!:`, `refactor!:`, etc., which represent a breaking
change (indicated by the `!`) and will result in a SemVer major.
- [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)
Fixes a2aproject#1421 🦕
## Problem
Each `EventQueueSource` starts its dispatcher task when it is created,
and `ActiveTask` creates two of them in `__init__`. When the setup in
`start()` fails, because the stored task is in a terminal state or is
missing, the `except` block re-raises without closing them:
```python
except Exception:
logger.debug('ActiveTask[%s]: Setup failed, cleaning up', self._task_id)
self._is_finished.set()
if self._reference_count == 0 and self._on_cleanup:
self._on_cleanup(self)
raise # _event_queue_agent and _event_queue_subscribers stay open
```
A `SendMessage` to a completed task goes through this path, so every
rejected request leaves two `_dispatch_loop` tasks pending until they
are garbage collected and logged as `Task was destroyed but it is
pending!`.
## Fix
The `except` block in `start()`
(`src/a2a/server/agent_execution/active_task.py`) now closes
`_event_queue_agent` and `_event_queue_subscribers` with
`close(immediate=True)` before calling `_on_cleanup`, the same way
`aclose()` closes them. `aclose()` itself can't be used here because it
takes `_lock`, which `start()` already holds.
The change only touches the failure path, so a successful `start()` is
not affected.
## Test
A test was added to the existing suite at
`tests/server/agent_execution/test_active_task.py`:
- `test_active_task_start_setup_failure_closes_event_queues`: after
`start()` fails on a completed task (`UnsupportedOperationError`) or a
missing one (`TaskNotFoundError`), both dispatcher tasks are done.
Both cases fail on `main` with the dispatcher tasks still pending and
pass with the fix. No existing test was modified or removed. Happy to
adapt the test approach based on maintainer feedback.
Co-authored-by: mykytanetipa <mykytanetipa@google.com>1 parent 05aaff6 commit 5e0d415
2 files changed
Lines changed: 46 additions & 1 deletion
File tree
- src/a2a/server/agent_execution
- tests/server/agent_execution
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
637 | 637 | | |
638 | 638 | | |
639 | 639 | | |
| 640 | + | |
| 641 | + | |
640 | 642 | | |
641 | 643 | | |
642 | 644 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
1503 | 1503 | | |
1504 | 1504 | | |
1505 | 1505 | | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
0 commit comments