|
16 | 16 | EventConsumer, |
17 | 17 | EventQueue, |
18 | 18 | InMemoryQueueManager, |
| 19 | + NoTaskQueue, |
19 | 20 | QueueManager, |
20 | 21 | ) |
21 | 22 | from a2a.server.request_handlers.request_handler import RequestHandler |
|
50 | 51 |
|
51 | 52 | logger = logging.getLogger(__name__) |
52 | 53 |
|
53 | | -TERMINAL_TASK_STATES = { |
| 54 | +TERMINAL_TASK_STATES = ( |
54 | 55 | TaskState.completed, |
55 | 56 | TaskState.canceled, |
56 | 57 | TaskState.failed, |
57 | 58 | TaskState.rejected, |
58 | | -} |
| 59 | +) |
59 | 60 |
|
60 | 61 |
|
61 | 62 | @trace_class(kind=SpanKind.SERVER) |
@@ -236,7 +237,8 @@ async def _setup_message_execution( |
236 | 237 | request_context = await self._request_context_builder.build( |
237 | 238 | params=params, |
238 | 239 | task_id=task.id if task else None, |
239 | | - context_id=params.message.context_id, |
| 240 | + context_id=params.message.context_id |
| 241 | + or (task.context_id if task else None), |
240 | 242 | task=task, |
241 | 243 | context=context, |
242 | 244 | ) |
@@ -342,7 +344,11 @@ async def push_notification_callback() -> None: |
342 | 344 | await self._cleanup_producer(producer_task, task_id) |
343 | 345 |
|
344 | 346 | if not result: |
345 | | - raise ServerError(error=InternalError()) |
| 347 | + raise ServerError( |
| 348 | + error=InternalError( |
| 349 | + message='Agent execution completed without producing a result.' |
| 350 | + ) |
| 351 | + ) |
346 | 352 |
|
347 | 353 | if isinstance(result, Task): |
348 | 354 | self._validate_task_id_match(task_id, result.id) |
@@ -435,8 +441,18 @@ async def _cleanup_producer( |
435 | 441 | task_id: str, |
436 | 442 | ) -> None: |
437 | 443 | """Cleans up the agent execution task and queue manager entry.""" |
438 | | - await producer_task |
439 | | - await self._queue_manager.close(task_id) |
| 444 | + try: |
| 445 | + await producer_task |
| 446 | + except Exception: |
| 447 | + # Task exceptions are already handled via logger and _track_background_task |
| 448 | + pass |
| 449 | + |
| 450 | + try: |
| 451 | + await self._queue_manager.close(task_id) |
| 452 | + except NoTaskQueue: |
| 453 | + # Already closed by another request handler for the same task. |
| 454 | + pass |
| 455 | + |
440 | 456 | async with self._running_agents_lock: |
441 | 457 | self._running_agents.pop(task_id, None) |
442 | 458 |
|
|
0 commit comments