Stop an `aws_lambda` retirement test from leaking pending tasks into later tests

Merged
DS
1mo ago
+3-0

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

No issue: this fixes a CI flake (job log).

Why we make these changes

TestBridgeFailureModes::test_an_unwind_that_outlives_the_cancel_timeout_closes_the_retired_loop ends with gc.collect(). Its abandoned_run local still held the run task it abandons, so that collection missed the task. On 3.14 it also missed the retired loop's clean_up() task, reachable from the run through a reference cycle. Both tasks were destroyed in whichever test on the worker collected next, which logged two asyncio Task was destroyed but it is pending! records into that test's caplog. In the linked job that was TestLLMReminder::test_remembers_a_bounded_number_of_runs. #11882 made that test ignore other loggers; this PR removes the leak itself.

The fix is test-only: del abandoned_run after abandoned_runs.clear(), before the final gc.collect(). The test's assertions are unchanged.

New public surface: none.

Verification (local):

  • A throwaway victim test (not committed) runs gc.collect() under caplog right after the leaking test and asserts no records. On main it fails on 3.13.16 (1 record) and 3.14.10 (2 records). With this change it passes on both, 3 runs each.
  • A throwaway pytest plugin that attributes each Task was destroyed record to the test that logged it shows both records now come from the leaking test itself, and the collection after it logs none.
  • tests/harness/aws_lambda/test_aws_lambda.py passes.

What changes for existing users: nothing.

Known follow-up, separate PR: _AgentLoop.retire schedules its hard loop.stop at the same deadline as clean_up's budget, so clean_up's tail never runs.

PR decisions

2026-10-31 · pre-push review 1 of 3 at a15405fc131d31209814a1a9e2c243ecae82ea60 · iter -

2026-10-31 · pre-push review 1 of 3 at a15405fc131d31209814a1a9e2c243ecae82ea60 · iter -

2026-11-01 · no tracking issue for the _bridge.py retire race · iter -

  • Decision: Don't file an issue; the follow-up is its own PR, already in progress on branch let-retired-loop-cleanup-finish
  • Why: Chosen on David's behalf: his standing rule prefers our own follow-up PR over an issue
  • Source: https://github.com/pydantic/pydantic-ai/pull/12137
  • Session: d605341b-a36e-475c-8881-c95461c74eba
  • Supersedes: follow-up named in body, no tracking issue filed

2026-11-01 · no second-opinion review · iter -

  • Decision: David chose no second-opinion review
  • Why: Picked from the second-opinion options; the recommendation was none for a test-only 3-line diff that CI and douwebot had already cleared
  • Source: https://github.com/pydantic/pydantic-ai/pull/12137
  • Session: d605341b-a36e-475c-8881-c95461c74eba

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.