Keep `test_many_concurrent_runs_fill_batches_of_the_default_size` within the per-test time budget
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
No issue to close: this fixes a flake on main. CI run 41310535560 attempt 1 failed test_many_concurrent_runs_fill_batches_of_the_default_size ("test on 3.13 (all-extras, shard 2/2)") at 8.25s against the 8s per-test budget. Attempt 2 passed.
Why we make these changes. The test runs near the cap all the time, not only on bad runs. Its call took 5.7-7.7s on the last 8 main runs, against ~1.4s locally. CPU speed explains only part of that gap: a CPU-bound sibling in the same file is ~2.5x slower on CI. The 1,050 concurrent runs allocate enough to trigger automatic full garbage collections. Each one walks every object the xdist worker has accumulated, and late in a CI run that is millions of objects.
The test now uses the existing young_gc fixture, which freezes the pre-existing heap for the test, so those collections walk only what the test allocates. The 1,050 runs and the assertions are unchanged, so the test still proves that runs fill batches of the default max_requests ([1_000, 50]). The budget is unchanged. The young_gc docstring now says that it covers automatic collections as well as gc.collect() calls.
Unfreezing at teardown hands the heap back to the collector, so a later test in the worker can hit its next full collection a little sooner. A local probe on 3.13 showed later tests pay about one extra heap walk in total. This test stops making three.
New public surface: none.
Verification. These are local runs of the test. A scratch pytest plugin pre-allocated a heap and timed GC during the call phase:
| simulated heap | before: call / gen-2 GC | after: call / gen-2 GC |
|---|---|---|
| none | 1.39s / 0.32s | 1.13s / 0.09s |
| 2.2M objects | 1.54s / 0.52s | 1.13s / 0.09s |
| 5.5M objects | 1.81s / 0.79s | 1.11s / 0.08s |
On CI (3.13 all-extras), the call took 5.7-8.25s before, across the last 8 main runs and the failed run. After, it took 4.82s and 2.48s on two attempts of this PR's run.
What changes for existing users: Nothing. This only changes tests.
PR decisions
2026-11-01 · pre-push review 1 of 3 at 174ae3d4298077c160860afd90ad815383f6e33a · iter -
- Decision: reserved; dispatching light-tier fresh reviewer
- Why: tree 324c09b0739a98e98e4310733a0d934af3f20dd5; full branch a0022cd08..174ae3d42
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-batch-runs-time-budget-flake
- Session: 96c1f83a-b18b-496e-9d0e-33e99e1b9d70
2026-11-01 · pre-push review 1 of 3 at 174ae3d4298077c160860afd90ad815383f6e33a · iter -
- Decision: 2 findings, no code change: (1) CI gap closure unproven -> PR body reports CI after-duration, no 'fixed' claim before it; (2) unfreeze defers one heap walk to a later test -> verified on 3.13 (test's 3 full walks removed, later tests pay ~1 extra) and stated in PR body
- Why: tree 324c09b0739a98e98e4310733a0d934af3f20dd5; scope a0022cd08..174ae3d42; first review; content unchanged after triage
- Source: https://github.com/pydantic/pydantic-ai/tree/fix-batch-runs-time-budget-flake
- Session: 96c1f83a-b18b-496e-9d0e-33e99e1b9d70
2026-11-01 · young_gc over fewer runs or a slow marker · iter -
- Decision: Freeze the pre-existing heap with the existing young_gc fixture; keep 1,050 runs, the [1_000, 50] assertion and the 8s budget
- Why: Locally the call drops 1.39-1.81s -> 1.11-1.13s, flat across simulated heap sizes; gen-2 GC 0.32-0.79s -> 0.08-0.09s. Removes real cost without weakening what the test proves.
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/41310535560
- Session: 96c1f83a-b18b-496e-9d0e-33e99e1b9d70
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.