Stop an `aws_lambda` retirement test from leaking pending tasks into later tests
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()undercaplogright after the leaking test and asserts no records. Onmainit 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 destroyedrecord 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.pypasses.
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 -
- Decision: reserved; dispatched (light tier: no hosted review yet)
- Why: tree 089b56622747aa7e1d43c9c4ce5c8558fe8a6c28; full branch 47b8c364c..a15405fc1; policy 47b8c364c; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/a15405fc131d31209814a1a9e2c243ecae82ea60
- Session: d605341b-a36e-475c-8881-c95461c74eba
2026-10-31 · pre-push review 1 of 3 at a15405fc131d31209814a1a9e2c243ecae82ea60 · iter -
- Decision: no findings (current at a15405fc1)
- Why: tree 089b56622747aa7e1d43c9c4ce5c8558fe8a6c28; full branch 47b8c364c..a15405fc1; fresh haiku claude -p from policy-base checkout, read-only tools, no fan-out
- Source: https://github.com/pydantic/pydantic-ai/tree/a15405fc131d31209814a1a9e2c243ecae82ea60
- Session: d605341b-a36e-475c-8881-c95461c74eba
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.