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.
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 at 99b094eb0c, which 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
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 so the registry tests pass the blocking-call detector now running on main. Publishing, probing, and cleanup run in worker threads via anyio.to_thread.run_sync, with no change to clai2 sessions output or process publishing behavior.
umask and bind stay on the loop: the umask is process-wide, and a cancellation between bind and the inode read would leave a socket with no inode to remove it by. The inode read runs in a worker thread inside a shielded cancel scope, so no cancellation can land before the inode is known. Adds tests that a failed or cancelled server start removes the socket and record file, and splits _list_live_sessions and run into _unique_prefixes and _launch to keep cli.py under the complexity limit after merging main.