diff --git a/pyrit/backend/services/service_lifecycle.py b/pyrit/backend/services/service_lifecycle.py index ecedf13fee..6e48e2c9fc 100644 --- a/pyrit/backend/services/service_lifecycle.py +++ b/pyrit/backend/services/service_lifecycle.py @@ -37,10 +37,14 @@ def has_active_analytics_work() -> bool: async def close_services_async() -> None: """Close existing resources, then invalidate cached registry and memory references.""" errors: list[BaseException] = [] - for close in (_close_cached_services_async, close_analytics_service_async): + for operation_name, close in ( + ("cached backend services", _close_cached_services_async), + ("analytics service", close_analytics_service_async), + ): try: await close() except (Exception, asyncio.CancelledError) as error: + error.add_note(f"Failed to close {operation_name}.") errors.append(error) if len(errors) == 1: raise errors[0] diff --git a/tests/unit/backend/test_analytics_lifecycle.py b/tests/unit/backend/test_analytics_lifecycle.py index 66b66763be..f11e9f88b9 100644 --- a/tests/unit/backend/test_analytics_lifecycle.py +++ b/tests/unit/backend/test_analytics_lifecycle.py @@ -309,6 +309,7 @@ async def test_other_service_failure_still_closes_analytics_async( with pytest.raises(type(error)) as caught: await close_services_async() assert caught.value is error + assert error.__notes__ == ["Failed to close cached backend services."] assert analytics_service.is_closed assert get_analytics_service.cache_info().currsize == 0 @@ -326,6 +327,8 @@ async def test_service_and_analytics_close_failures_are_both_reported_async( with pytest.raises(BaseExceptionGroup, match="Backend service closure failed") as caught: await close_services_async() assert caught.value.exceptions == (services_error, analytics_error) + assert services_error.__notes__ == ["Failed to close cached backend services."] + assert analytics_error.__notes__ == ["Failed to close analytics service."] assert get_analytics_service() is analytics_service assert not analytics_service.is_closed