Repository navigation
Description
Problem Description
The first refine round of a new roadmap-service pod can fail with an import deadlock. _run_refine_adapter (src/devops_cli/roadmap/run.py:405) imports devops_cli.roadmap.refine in the worker thread when the round starts. That module imports refine_investigation, which imports pydantic_ai. When the pod starts, the workers of all lanes and repositories start at the same time. Two worker threads then import pydantic_ai at the same time, and the import lock of Python 3.14 raises _DeadlockError. The round fails, and the failure counts toward the three failed rounds that suspend a job.
This can occur after each pod start. Each merge to main builds a new image, and Argo CD Image Updater starts a new pod (strategy Recreate).
Reproduction Steps
- Start the Service with two or more repositories in
service.repos, so that two model-lane workers start together. - Let the first round of each worker run refine before any thread has imported
pydantic_ai. - One round can fail with
_DeadlockError.
Seen in the cluster on 2026-10-28 at 20:36:24 UTC, in pod roadmap-service-b89d69c88-k6cj5, 16 s after the pod started, on dan-petty/dotfiles, lane model.
Expected vs Actual Behavior
Expected: a worker thread never imports a module for the first time while other threads can import the same package. The Service imports the modules of every job before it starts its workers, or the job modules import them at module level.
Actual: refine fails with deadlock detected by _ModuleLock('pydantic_ai.embeddings'). service-failures.json records one failed refine round for dan-petty/dotfiles.
Environment Diagnostics
Image ghcr.io/dan-petty/devops-cli/service:latest from main at 657b03d, Python 3.14, pydantic-ai 2.54, devops serve --service --workers 1.
Logs, Traces, or Terminal Output
Service job execution failed for dan-petty/dotfiles: Roadmap jobs failed: refine: deadlock detected by _ModuleLock('pydantic_ai.embeddings') at 139976972769232
File ".../devops_cli/roadmap/run.py", line 405, in _run_refine_adapter
from devops_cli.roadmap.refine import apply_refine, plan_refine, raise_for_failed_items
File ".../devops_cli/roadmap/refine.py", line 109, in <module>
from devops_cli.roadmap.refine_investigation import (
File ".../devops_cli/roadmap/refine_investigation.py", line 24, in <module>
from pydantic_ai import Agent, RunContext, Tool, capture_run_messages
File ".../pydantic_ai/__init__.py", line 25, in <module>
from .embeddings import (
_frozen_importlib._DeadlockError: deadlock detected by _ModuleLock('pydantic_ai.embeddings') at 139976972769232
Pre-Submission Checklist
- I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
- I have checked existing issues and pull requests to ensure this is not a duplicate.
Proposed design
Problem statement
The first refine round of a new service pod can fail with a Python 3.14 import deadlock. Two worker threads import pydantic_ai at the same time through a lazy import in _run_refine_adapter. The deadlock counts as a failed round and can suspend a job after three failures. This happens on every pod restart.
Acceptance criteria
- Criterion 1: The import of devops_cli.roadmap.refine is at module level in run.py, not inside _run_refine_adapter.
Verification: Read src/devops_cli/roadmap/run.py and confirm the import statement is at the top of the file, outside any function body. - Criterion 2: Starting the service with two or more repositories does not produce a _DeadlockError on the first refine round.
Verification: Run the service with two repositories in service.repos and confirm no _DeadlockError appears in the logs during the first refine round. - Criterion 3: Existing tests for run.py and refine.py pass.
Verification: Run uv run devops ci test tests/test_roadmap_run.py tests/test_roadmap_refine.py and confirm all tests pass.
Key questions
- Where should the import of devops_cli.roadmap.refine be placed to avoid the thread-safety deadlock?: Move the import to module level in src/devops_cli/roadmap/run.py. This ensures pydantic_ai is imported in the main thread before any worker thread starts, which is the standard Python practice for imports. (best practice) [sources: src/devops_cli/roadmap/run.py:405, src/devops_cli/roadmap/refine.py:109, src/devops_cli/roadmap/refine_investigation.py:24]
- Chosen: Move import to module level in run.py. Standard Python practice. The import runs in the main thread before workers start. Minimal change: move one import statement from inside _run_refine_adapter to the top of the file.
- Rejected: Add a pre-import step in service startup before thread creation. Requires finding and modifying the service startup code. Adds a new code path that must be maintained. More complex than moving one import statement.
- Rejected: Use a threading lock around the import in _run_refine_adapter. Does not fix the root cause. Python 3.14 import lock is internal and cannot be replaced by a user lock. Over-engineered for a one-line fix.
- Does the lazy import in _run_refine_adapter serve a purpose that would be lost by moving it to module level?: No. The service always runs refine jobs. The CLI command that calls _run_refine_adapter also needs the refine module. Moving the import to module level does not add an unused import in any code path. [sources: src/devops_cli/roadmap/run.py:398]
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Oct 28, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
The candidate reports a runtime deadlock that causes job failures on pod start.The deadlock causes job failures on every pod restart and counts toward job suspension.Every pod restart can lose a refine round, and three such losses suspend a job.The fix moves existing imports to module level or adds a pre-import step before thread start.Both items concern import timing at service start, but #858 removes unnecessary imports while the candidate ensures required imports complete before threads begin.devops-cli-service commentedon Oct 29, 2026
Status set to Ready at ef6e028 (see proposed design in issue body).
fix(roadmap): import refine at module level to avoid deadlock (#2535) (…
chore(main): release 0.13.1 (#2598)