Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions pyrit/backend/services/runtime_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/backend/test_runtime_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Loading