Skip to content

MAINT: Use TaskGroup for Anthropic model-written evals file fetches (P311-11) - #3959

Merged
Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
WaqarAnwar:maint/p311-11-anthropic-evals-taskgroup
Nov 5, 2026
Merged

Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
WaqarAnwar:maint/p311-11-anthropic-evals-taskgroup

Conversation

@WaqarAnwar

Copy link
Copy Markdown
Contributor

Description

Part of #3166 (P311-11): the parent-scoped JSONL fetches in _AnthropicModelWrittenEvalsDataset._fetch_dataset_async.

The loader fetched all 172 JSONL files with a bare asyncio.gather. When one fetch failed, the error propagated but the sibling fetches kept running unowned, and most of them were still queued for a worker thread (asyncio.to_thread, default executor of cpu_count + 4 threads). This PR:

  • runs the fetches in an asyncio.TaskGroup, so the first failure cancels the remaining fetches. Fetches still queued for a thread are dropped; fetches already running in a thread finish, since threads cannot be interrupted
  • re-raises the first fetch error unwrapped and outside the except block, so callers see the original exception (type, traceback, cause) rather than an ExceptionGroup. SeedDatasetProvider.fetch_datasets_async error handling and notes are unaffected
  • keeps seeds in manifest order

Scope is limited to this one site; pyrit/common/task_utils.py and other gather call sites are unchanged.

Tests and Documentation

  • test_fetch_failure_cancels_pending_sibling_fetches: a failing fetch cancels every sibling fetch, and the exact original exception (with its __cause__) reaches the caller. Fails against the previous gather implementation.
  • test_fetch_results_keep_manifest_order: fetches finish in reverse order and seeds still follow manifest order.
  • Verified separately that cancelling the task group drops queued to_thread work (2-worker executor: 3 of 20 jobs ran after the first failure).
  • tests/unit/datasets (anthropic + seed dataset provider): 260 passed
  • ruff check / ruff format --check: passed
  • ty check on both changed files: no new diagnostics
  • No documentation changes needed.

Happy to adjust anything in this PR

…P311-11)

Replace the bare asyncio.gather over the 172 JSONL fetches with an
asyncio.TaskGroup so a failed fetch cancels sibling fetches still queued
for a worker thread instead of leaving them running unowned. The first
fetch error is re-raised unwrapped, so callers still see the original
exception type, and seeds keep manifest order.

Part of microsoft#3166.
Copilot AI balanced review requested due to automatic review settings November 4, 2026 13:53

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 were identified for the change to cancel sibling dataset fetches when one fails.

No open issues 🎉

What changed in this PR

This PR addresses P311-11 in #3166 by giving Anthropic model-written eval file fetches a shared task lifetime, so a failed fetch cancels pending siblings.

Changes:

  • Replaces asyncio.gather with asyncio.TaskGroup in the dataset loader.
  • Re-raises the original fetch exception and preserves manifest ordering.
  • Adds tests for sibling cancellation, exception identity and cause, and result ordering.
File Description
tests/​unit/​datasets/​test_anthropic_model_written_evals_dataset.py Adds cancellation, exception-preservation, and ordering tests.
pyrit/​datasets/​seed_datasets/​remote/​anthropic_model_written_evals_dataset.py Uses a task group for file fetches while preserving errors and seed order.

Review effort: Balanced

Run file-fetch task groups in an owned task to preserve caller cancellation counts on Python 3.11 and 3.12. Cover repeated failures and external cancellation cleanup.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Nov 5, 2026
Merged via the queue into microsoft:main with commit fd62309 Nov 5, 2026
62 of 63 checks passed
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