From 14e5bcc367aeaf48bd197744ebae9527609248c5 Mon Sep 17 00:00:00 2001 From: "Slai.life" <294879785+huong8373tt-beep@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:00:28 +0800 Subject: [PATCH] MAINT: Add runtime shutdown phase context --- pyrit/backend/services/runtime_lifecycle.py | 2 ++ tests/unit/backend/test_runtime_lifecycle.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/pyrit/backend/services/runtime_lifecycle.py b/pyrit/backend/services/runtime_lifecycle.py index d7ee31efc0..ef9944a64b 100644 --- a/pyrit/backend/services/runtime_lifecycle.py +++ b/pyrit/backend/services/runtime_lifecycle.py @@ -278,6 +278,7 @@ async def _shutdown_runtime_async(self) -> None: if service: await service.shutdown_async() except (Exception, asyncio.CancelledError) as error: + error.add_note("Runtime shutdown phase: scenario service shutdown.") errors.append(error) try: await close_services_async() @@ -289,6 +290,7 @@ async def _shutdown_runtime_async(self) -> None: if CentralMemory._memory_instance is not None: await CentralMemory.get_memory_instance().dispose_engine_async() except (Exception, asyncio.CancelledError) as error: + error.add_note("Runtime shutdown phase: memory disposal.") errors.append(error) if len(errors) == 1: raise errors[0] diff --git a/tests/unit/backend/test_runtime_lifecycle.py b/tests/unit/backend/test_runtime_lifecycle.py index 47549ddcfc..8021813597 100644 --- a/tests/unit/backend/test_runtime_lifecycle.py +++ b/tests/unit/backend/test_runtime_lifecycle.py @@ -169,6 +169,8 @@ async def test_shutdown_disposes_memory_after_services_even_on_failure( order.attach_mock(memory.dispose_engine_async, "memory") errors = tuple(asyncio.CancelledError(name) if cancelled else RuntimeError(name) for name in failures) for name, error in zip(failures, errors, strict=True): + if name in {"scenarios", "memory"}: + error.add_note(f"Existing {name} note.") getattr(order, name).side_effect = error if len(errors) == 1: with pytest.raises(type(errors[0])) as single_error: @@ -182,6 +184,13 @@ async def test_shutdown_disposes_memory_after_services_even_on_failure( await runtime.shutdown_async() assert order.mock_calls == [call.scenarios(), call.services(), call.memory()] + expected_notes = { + "scenarios": "Runtime shutdown phase: scenario service shutdown.", + "memory": "Runtime shutdown phase: memory disposal.", + } + for name, error in zip(failures, errors, strict=True): + expected = [f"Existing {name} note.", expected_notes[name]] if name in expected_notes else None + assert getattr(error, "__notes__", None) == expected async def test_shutdown_repeated_failure_preserves_original_error_async(runtime: RuntimeLifecycle) -> None: