Fix flaky `test_remembered_approval_holds_on_replay_and_in_the_next_run` by closing the event loop `run_sync` leaves on the DBOS executor thread

Merged
DS
23d ago
+9-0

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

test_remembered_approval_holds_on_replay_and_in_the_next_run errored at teardown with ValueError: Invalid file descriptor: -1 from BaseEventLoop.__del__ (failing run). Same flake as #12708, fixed for detect_loops in #12724.

Root cause. DBOS.resume_workflow() runs the recovered workflow on a DBOS executor thread. There, Agent.run_sync() creates a new event loop, sets it as the thread's loop, and nothing closes it. The teardown collection in tests/gc_isolation.py then finalizes it; when the collector reaches the loop's self-pipe socket first, BaseEventLoop.__del__ closes the loop over an already-closed socket and raises.

Fix. The test captures that loop during the recovered attempt and closes it after the last workflow run, like the detect_loops and refine_output DBOS tests close theirs. Two differences from those tests: this workflow runs a third time, directly on the main thread, whose current loop is the session-scoped session_event_loop from tests/harness/conftest.py, so only the recovered attempt's loop is closed; and the close waits until after that third run, because loop.close() shuts down the loop's default executor, which dbos==2.10.0 (the floor) sets to the DBOS executor itself (first head failed test on 3.14 (lowest-versions) with cannot schedule new futures after shutdown). Test-only; run_sync() is unchanged.

Reproduction. #12724's scratch plugin (not committed): it joins executor workers on shutdown and, after the test body, closes the self-pipe socket of each idle loop set on a non-main thread, the order the flaky run hit.

without the fixwith the fix
forced orderERROR at teardown: ValueError: Invalid file descriptor: -1; one leaked loop, set on dbos-executor-_0no leaked loop; passes
-W "error:unclosed event loop:ResourceWarning"ERROR at teardown: ResourceWarning: unclosed event loopno warning; passes

With the fix, both rows also pass on dbos==2.10.0 (uv run --with 'dbos==2.10.0' pytest tests/harness/permissions/test_dbos.py).

What changes for existing users: nothing.

PR decisions

2026-11-12 · fix shape: same loop close as the siblings · iter -

  • Decision: Add the detect_loops/refine_output asyncio.get_event_loop().close() to tests/harness/permissions/test_dbos.py; test-only
  • Why: Settled by the manager's dispatch; matches PR 12724's fix for the same flake
  • Source: https://github.com/pydantic/pydantic-ai/issues/13303
  • Session: 1dd407bd-2d0e-438c-9a2a-13994f81c3f5

2026-11-12 · close only on the recovered attempt · iter -

  • Decision: Guard the close with len(attempts) == 2, not every non-first attempt as the siblings do
  • Why: Probe: attempts 1 and 3 run on MainThread with the session-scoped session_event_loop; only the resume runs on dbos-executor-_0. An unguarded close would close the shared session loop
  • Source: https://github.com/pydantic/pydantic-ai/issues/13303
  • Session: 1dd407bd-2d0e-438c-9a2a-13994f81c3f5

2026-11-12 · merge path: flake fix · iter -

  • Decision: Follows the tracked pushing-commits-to-the-repo flake-fix path; agent enqueues once the head meets the merge bar; step-7 second-opinion choice does not come up
  • Why: Settled by the manager's dispatch
  • Source: https://github.com/pydantic/pydantic-ai/issues/13303
  • Session: 1dd407bd-2d0e-438c-9a2a-13994f81c3f5

2026-11-12 · scope: this file only · iter -

  • Decision: Only tests/harness/permissions/test_dbos.py changes; other DBOS test files lacking the close are reported with evidence, not folded
  • Why: Settled by the manager's dispatch; flake fix changes only the failing test's module
  • Source: https://github.com/pydantic/pydantic-ai/issues/13303
  • Session: 1dd407bd-2d0e-438c-9a2a-13994f81c3f5

2026-11-12 · pre-push review 1 of 3 at 8174ff140115fe5aff08d8f77aa46525bcd07959 · iter -

2026-11-12 · pre-push review 1 of 3 at 8174ff140115fe5aff08d8f77aa46525bcd07959 · iter -

2026-11-12 · exempt delta: comment-only at 413a3a32bca4100cf14b9e05e6aebffeb22e0101 · iter -

2026-11-13 · regression 1: close moved after the last workflow call · iter -

  • Decision: Capture the recovered attempt's loop and close it after the third workflow call, instead of inside the workflow
  • Why: test on 3.14 (lowest-versions) failed: dbos 2.10 sets dbos._executor itself as the loop's default executor, so loop.close() shut down the DBOS executor the third run still needs; dbos 3.4 wraps it in _SharedExecutor, which ignores shutdown
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43330801196/job/131574834041
  • Session: 1dd407bd-2d0e-438c-9a2a-13994f81c3f5
  • Supersedes: close only on the recovered attempt

2026-11-13 · pre-push review 2 of 3 at 2334399dc2c78166f90c2f74395f39a926c87380 · iter -

2026-11-13 · pre-push review 2 of 3 at 2334399dc2c78166f90c2f74395f39a926c87380 · iter -

2026-11-13 · standing rule: GitHub comments need no approval · iter -

  • Decision: Verbatim: "GitHub comments need no approval from David or me, on any PR or issue. Closing PRs and issues, merging and code changes keep their existing rules." A finding that touches several PRs or issues gets its main comment where it belongs, plus a short linking pointer on each related PR and issue
  • Why: Standing rule from David, relayed by the manager driving this PR
  • Source: https://github.com/pydantic/pydantic-ai/pull/13319
  • Session: 1dd407bd-2d0e-438c-9a2a-13994f81c3f5

2026-11-13 · enqueued as flake fix at 2334399dc2c78166f90c2f74395f39a926c87380 · iter -

2026-11-13 · defer: pr-review-gates-events.yml pull_request_review_thread trigger is unsupported · iter -

  • Decision: Filed as #13322 instead of fixing here; no routing labels and no assignee, routed by the manager driving this PR
  • Why: Out of scope for this PR — CI workflow logic with its own decision on which supported event replaces the trigger
  • Source: https://github.com/pydantic/pydantic-ai/issues/13322
  • Session: 1dd407bd-2d0e-438c-9a2a-13994f81c3f5

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.

https://claude.ai/code/session_01HhQjZAJFKpC2aWovtpMUZB