Skip to content

FIX: Preserve failures in exception group handling (P311-12) - #3983

Merged
Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
WaqarAnwar:maint/p311-12-exception-group-handling
Nov 6, 2026
Merged

Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
WaqarAnwar:maint/p311-12-exception-group-handling

Conversation

@WaqarAnwar

Copy link
Copy Markdown
Contributor

Description

Part of #3166 (P311-12): three sites where exception-group failures were being lost. Exception types and single-failure behavior are unchanged.

Runtime shutdown and live apply

close_services_async raises a pure BaseExceptionGroup when two closures fail and at least one is a CancelledError. except (Exception, asyncio.CancelledError) does not catch that group, so in RuntimeLifecycle:

  • Shutdown: the group escaped _shutdown_runtime_async, which skipped memory disposal and dropped errors already collected.
  • Live apply: the group bypassed the failure handler, so the state was not updated to restart-required.

Both handlers now also catch BaseExceptionGroup, as github_copilot_target.py and llm_scoring.py already do. Apply re-raises any error that is not an Exception, which covers CancelledError and groups that can carry it. A plain ExceptionGroup is still absorbed into the state, as before.

Anthropic evals fetches

TaskGroup can collect several failures, but only group.exceptions[0] was raised. That same exception is still raised, so callers see no type change, and the other failures are added with add_note.

Persisted scenario errors

str(group) only gives a child count, so stored errors read Multiple failed (2 sub-exceptions). A new summarize_exception helper keeps child failures in the message, which is used by Scenario._mark_scenario_failed_async and ScenarioRunService:

Multiple failed (2 sub-exceptions): [ValueError: first boom; RuntimeError: second boom]

error_type stays ExceptionGroup, and messages for non-group errors are unchanged. This text is the only user-visible change.

Out of scope

This PR adds no except* and no new TaskGroup usage, because a broad migration would change behavior:

  • TaskGroup wraps even a single failure, which breaks except ValueError handlers and hides the message behind unhandled errors in a TaskGroup (1 sub-exception).
  • TaskGroup cancels siblings on the first failure, while the attack executor (partial results) and runtime shutdown (draining) let them finish.
  • gather_with_cleanup_async and the scenario supervisor deliberately defer caller cancellation until cleanup finishes.
  • A bare raise inside except* wraps a plain exception in a group, and a task re-raising a cancellation this way reports task.cancelled() == False.

A live apply cancelled through a mixed group still ends as a failed task rather than a cancelled one. That is unchanged, because converting it to a plain CancelledError would drop the other errors in the group.

Tests and Documentation

New regression tests:

  • test_shutdown_mixed_close_group_still_disposes_memory_and_reports_errors_async: with and without an earlier scenario shutdown failure.
  • test_apply_mixed_close_group_requires_restart_and_propagates_cancellation_async
  • test_apply_close_group_without_cancellation_requires_restart_without_raising_async: pins existing behavior, and passes before and after this change.
  • test_concurrent_fetch_failures_keep_first_error_and_note_the_others
  • test_execute_run_reports_every_failure_in_an_exception_group
  • test_summarize_exception: plain, empty, group and nested group cases.
  • test_concurrent_partial_failures_resume_without_duplicate_results and test_fetch_failure_cancels_pending_sibling_fetches: extended with new assertions.

The shutdown, apply (mixed group), fetch, run service and partial failure tests were confirmed failing before the fix.

Results:

  • tests/unit with -n 4: 33760 passed, 460 skipped, 4 xfailed, 2 failed. I ran this without --dist=loadfile, which make unit-test uses.
  • Both failures reproduce on a clean main and are unrelated: test_codeql_merge_group_scenarios needs node, which is not installed locally, and test_separate_research_target_receives_research_stages_async raises UnboundLocalError.
  • ruff check and ruff format: passed.
  • ty check on the changed source files: same diagnostics as main.
  • No documentation changes needed.

- Catch mixed BaseExceptionGroups from close_services_async in runtime
  shutdown and live apply, so memory is still disposed, earlier errors
  are kept, and apply records restart-required.
- Add other concurrent Anthropic fetch failures as notes on the raised
  error instead of discarding them.
- Include child failures in persisted scenario error messages.
Copilot AI balanced review requested due to automatic review settings November 5, 2026 15:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the changes preserving exception-group failure details.

No open issues 🎉

What changed in this PR

This PR addresses P311-12 in #3166 by preserving exception-group failure details across PyRIT’s runtime lifecycle, dataset fetching, and scenario error reporting, while retaining existing exception types and single-failure behavior.

Changes:

  • Handle mixed exception groups during shutdown and live apply so cleanup and failure-state updates still occur.
  • Attach concurrent dataset-fetch failures as notes to the original raised exception.
  • Summarize nested exception groups in scenario errors and add regression coverage.
File Description
tests/​unit/​scenario/​core/​test_scenario_partial_results.py Verify persisted errors include every partial failure.
tests/​unit/​exceptions/​test_exceptions_helpers.py Cover plain, empty-message, and nested exception summaries.
tests/​unit/​datasets/​test_anthropic_model_written_evals_dataset.py Verify concurrent fetch failures retain secondary error notes.
tests/​unit/​backend/​test_scenario_run_service.py Verify grouped failures reach run state and persistence.
tests/​unit/​backend/​test_runtime_lifecycle.py Cover cleanup and live-apply behavior for exception groups.
pyrit/​scenario/​core/​scenario.py Persist detailed exception-group summaries.
pyrit/​exceptions/​exceptions_helpers.py Add recursive exception summarization.
pyrit/​datasets/​seed_datasets/​remote/​anthropic_model_written_evals_dataset.py Preserve secondary fetch failures as exception notes.
pyrit/​backend/​services/​scenario_run_service.py Use detailed summaries in run failure reporting.
pyrit/​backend/​services/​runtime_lifecycle.py Catch mixed groups while preserving cancellation propagation.

Review effort: Balanced

@WaqarAnwar

Copy link
Copy Markdown
Contributor Author

The failure looks unrelated to this PR, and the most likely cause is a stalled macOS runner, though I couldn't reproduce it:

  • The test took 23.54s on that job against 0.09s locally, so about 13s of the delay fell outside the 10s wait.
  • Other tests also stalled on macOS runners in this run, including a 42s test setup in the dev_all job.
  • /api/health and the middleware don't use worker threads, and the harder one-worker variants did not fail.

Roman Lutz (@romanlutz) can you kindly re-run the failed job? If this keeps happening, the 10s limit may be worth revisiting in a separate change, since it is much tighter than the 300s TEST_WATCHDOG_SECONDS.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Nov 6, 2026
Merged via the queue into microsoft:main with commit 24bc8a6 Nov 6, 2026
83 of 84 checks passed
hannahwestra25 pushed a commit to Kaap10/PyRIT that referenced this pull request Nov 6, 2026
Resolve the import conflict in pyrit/scenario/core/scenario.py by keeping both
ScenarioRunInProgressException (this PR) and summarize_exception (microsoft#3983).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants