Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/a2a/server/request_handlers/default_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@

except Exception:
logger.exception('Agent execution failed')
# If the consumer fails, we must cancel the producer to prevent it from hanging
# on queue operations (e.g., waiting for the queue to drain).
producer_task.cancel()
# Force the queue to close immediately, discarding any pending events.
# This ensures that any producers waiting on the queue are unblocked.
await queue.close(immediate=True)
raise
finally:
if interrupted_or_non_blocking:
Expand Down Expand Up @@ -392,6 +398,12 @@
bg_task.set_name(f'background_consume:{task_id}')
self._track_background_task(bg_task)
raise
except Exception:
# If the consumer fails (e.g. database error), we must cleanup.
logger.exception('Agent execution failed during streaming')
producer_task.cancel()
await queue.close(immediate=True)
raise
finally:
cleanup_task = asyncio.create_task(
self._cleanup_producer(producer_task, task_id)
Expand Down Expand Up @@ -435,7 +447,11 @@
task_id: str,
) -> None:
"""Cleans up the agent execution task and queue manager entry."""
await producer_task
try:
await producer_task
except (Exception, asyncio.CancelledError):

Check failure on line 452 in src/a2a/server/request_handlers/default_request_handler.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (BLE001)

src/a2a/server/request_handlers/default_request_handler.py:452:17: BLE001 Do not catch blind exception: `Exception`
# We don't want to stop cleanup if the producer task failed or was cancelled
pass

Check failure on line 454 in src/a2a/server/request_handlers/default_request_handler.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (S110)

src/a2a/server/request_handlers/default_request_handler.py:452:9: S110 `try`-`except`-`pass` detected, consider logging the exception

Check failure on line 454 in src/a2a/server/request_handlers/default_request_handler.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (SIM105)

src/a2a/server/request_handlers/default_request_handler.py:450:9: SIM105 Use `contextlib.suppress(Exception, asyncio.CancelledError)` instead of `try`-`except`-`pass`
Comment thread
lbobinski marked this conversation as resolved.
Outdated
await self._queue_manager.close(task_id)
async with self._running_agents_lock:
self._running_agents.pop(task_id, None)
Expand Down
1 change: 0 additions & 1 deletion tests/server/request_handlers/test_jsonrpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ async def streaming_coro():

self.assertIsInstance(response.root, JSONRPCErrorResponse)
assert response.root.error == UnsupportedOperationError() # type: ignore
mock_agent_executor.execute.assert_called_once()

@patch(
'a2a.server.agent_execution.simple_request_context_builder.SimpleRequestContextBuilder.build'
Expand Down
Loading