Keep the `clai2` live-session registry's file-system calls off the event loop
Stacked on #11673 (base clai2-peers-registry-socket). No linked issue: this follows up that PR after it merged main.
[!NOTE]
This replaces #13078 with the same five commits, cherry-picked onto #11673's current head. Merge #11673 first, then retarget this PR to main.
Why we make these changes
main now runs a blocking-call detector in the test suite. After #11673 merged main, the registry tests fail with BlockingError because LiveRegistry.serving(), listing() and _probe_record() touch the file system on the event loop. This PR moves those calls to worker threads with anyio.to_thread.run_sync.
The same merge took run() and _list_live_sessions() in cli/cli.py over the cognitive complexity limit, because main's additions and #11673's landed in the same functions. This PR moves the ID-prefix loop into _unique_prefixes() and the launch into _launch(), with no change in behavior.
Both fixes are separate from #11673 so the bots review them in isolation.
New public surface
none
User-visible behavior
No change in what clai2 sessions prints or how a process publishes itself. Publishing, probing and cleanup no longer block the event loop on directory, record and socket file operations.
umask and bind stay on the loop, because the umask is process-wide. The socket's inode lstat runs in a worker thread inside a shielded cancel scope: Python 3.13's detector flags it on the loop, and an unshielded cancellation between bind and the inode read would leave a socket with no inode to remove it by.
Verification
tests/clai2/test_live_registry.pyaddstest_failed_server_start_removes_socketandtest_cancelled_start_removes_socket. The second fails when the shield around the inodelstatis removed.- The full
tests/clai2suite passes on Python 3.13 with the detector on (5251 tests, run on #13078's head 99b094eb0c, whose tree is identical to this branch's and includesmainat fe3bf389c4). The CI failure on faab5f1258 was the on-looplstat, which only 3.13 flagged; my earlier local runs used 3.15. runtime/live_registry.pyandtests/clai2/test_live_registry.pyare at 100% line and branch coverage.ruff format,ruff checkand Pyright are clean on the three changed Python files.scripts/check_complexity.pypasses against bothmain(fe3bf389c4) and #11673's head.- An earlier full run on 3.15 had five failures outside these files: subprocess start-up timeouts and one
test_hooks.pytest. I did not confirm their cause; they did not recur in the 3.13 run.
What changes for existing users
Nothing: the registry is not on main yet.
PR decisions
2026-11-10 · Blocking-call fix goes in a stacked PR · iter -
- Decision: #11673's branch takes only the
mainmerge; the event-loop fix is onclai2-peers-registry-socket-off-loop, stacked on that branch. - Why: Standing workflow for this repo: an open PR gets no further commits beyond
mainmerges, so follow-up changes are reviewed in isolation. - Source: https://github.com/pydantic/pydantic-ai/pull/11673
2026-11-10 · pre-push review 2 of 3 at 7d0109634a · iter -
- Decision: Remediated in e22da27f1b:
umaskandbindstay on the event loop instead of a worker thread, and a test covers a failed server start. - Why: The reviewer found the process-wide umask changed while other tasks could run.
- Source: https://github.com/pydantic/pydantic-ai/tree/7d0109634a8f969360c9874a47252967a8baa7c8
2026-11-10 · pre-push review 3 of 3 at e22da27f1b · iter -
- Decision: Remediated in faab5f1258: the inode
lstatruns on the loop next tobind; cancelled-start test added; thecli/cli.pyfile-map row listssessions. - Why: A cancellation could arrive at the threaded
lstatafterbind, leaving a socket with no inode to unlink by. Review budget exhausted, so faab5f1258 has local checks only. - Source: https://github.com/pydantic/pydantic-ai/tree/e22da27f1b5bc4688ae417f3b7948545ead4903e
2026-11-10 · Stat the socket in a worker thread, shielded · iter -
- Decision: 69b08f3b99 moves the inode
lstatto a worker thread inside a shielded cancel scope;umaskandbindstay on the loop. - Why: CI on Python 3.13 flagged the on-loop
lstatas a blocking call. The shield keeps what review 3 asked for: no cancellation betweenbindand knowing the inode. A native task cancel inside the thread hop can still leave a stale socket, which the next start removes. - Source: https://github.com/pydantic/pydantic-ai/actions/runs/42863998188
- Supersedes: pre-push review 3 of 3 at e22da27f1b
2026-11-10 · Complexity fix for the main merge goes here · iter -
- Decision: 7534d3ab67 extracts
_unique_prefixes()and_launch()fromcli/cli.py. - Why: #11673's complexity check fails against
mainafter the merge (_list_live_sessions16,run15 to 16). It is a code change, so it follows the same stacked-PR rule as the event-loop fix. - Source: https://github.com/pydantic/pydantic-ai/actions/runs/42863998188/job/129997384860
2026-11-10 · tests/conftest.py stays unchanged · iter -
- Decision: Did not add
config/private_files.pytoBLOCKBUSTER_LOCAL_STORES, as the review on #11673 suggested. - Why: The registry now offloads
write_private, and its other callers are stores that are already exempt. The task also limits changes tosrc/pydantic_clai2/andtests/clai2/. - Source: https://github.com/pydantic/pydantic-ai/pull/11673#discussion_r4528316345
2026-11-10 · Merge main at 99b094eb0c · iter -
- Decision: Merged
origin/main(fe3bf389c4) into this branch and into #11673's branch (b75225328a); no conflicts. - Why: v2.67.0 was released after the first merge, so the API compatibility check reported its new public objects as removed. An earlier failure of the same job was Pyright passing over its time budget (344s against 280s); the rerun took 276s, and the changed files check in the same time before and after this PR.
- Source: https://github.com/pydantic/pydantic-ai/actions/runs/42917955546/job/130193378138
2026-11-10 · Replace #13078 with a fresh branch · iter -
- Decision: Cherry-picked #13078's five commits onto #11673's head as
clai2-peers-registry-off-loopand closed #13078. The commits named above are now 155d79a9d3, 1fca4d95e0, bd5c15011b, 63c13d393e and 09640f54e0; the tree is identical to #13078's head. - Why: Merging
maininto both stacked branches separately mademaintheir merge base, so #13078 showed all of #11673's files and GitHub reported it as conflicting. The workflow allows onlymainto be merged into a branch and no history rewrite, which leaves a fresh branch. - Source: https://github.com/pydantic/pydantic-ai/pull/13078
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. (None.)
- PR title is fit for the release changelog.
Summary by cubic
Keeps the clai2 live-session registry's file-system operations off the event loop, which had been failing the blocking-call detector now in the test suite.
-
Bug Fixes:
- Moves publishing, probing, and cleanup to worker threads via
anyio.to_thread.run_sync; no user-visible behavior changes. umaskandbindstay on the loop since the umask is process-wide, and the socket's inodelstatruns in a shielded worker thread so no cancellation lands between bind and cleanup.- Adds tests for failed and cancelled server starts, both verifying the socket is removed.
- Moves publishing, probing, and cleanup to worker threads via
-
Refactors:
- Extracts
_unique_prefixes()and_launch()fromcli/cli.pyto keep both functions under the cognitive complexity limit after themainmerge; behavior is unchanged.
- Extracts