Keep the `clai2` live-session registry's file-system calls off the event loop

Closed
MP
24d ago
+7471-1051

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.py adds test_failed_server_start_removes_socket and test_cancelled_start_removes_socket. The second fails when the shield around the inode lstat is removed.
  • The full tests/clai2 suite passes on Python 3.13 with the detector on (5251 tests at 99b094eb0c, which includes main at fe3bf389c4). The CI failure on faab5f1258 was the on-loop lstat, which only 3.13 flagged; my earlier local runs used 3.15.
  • runtime/live_registry.py and tests/clai2/test_live_registry.py are at 100% line and branch coverage. ruff format, ruff check and Pyright are clean on the three changed Python files.
  • scripts/check_complexity.py passes against both main (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.py test. 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 main merge; the event-loop fix is on clai2-peers-registry-socket-off-loop, stacked on that branch.
  • Why: Standing workflow for this repo: an open PR gets no further commits beyond main merges, 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 -

2026-11-10 · pre-push review 3 of 3 at e22da27f1b · iter -

2026-11-10 · Stat the socket in a worker thread, shielded · iter -

  • Decision: 69b08f3b99 moves the inode lstat to a worker thread inside a shielded cancel scope; umask and bind stay on the loop.
  • Why: CI on Python 3.13 flagged the on-loop lstat as a blocking call. The shield keeps what review 3 asked for: no cancellation between bind and 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 -

2026-11-10 · tests/conftest.py stays unchanged · iter -

2026-11-10 · Merge main at 99b094eb0c · iter -

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.