Skip to content

perf(ci): direct pytest runs ignore the gate slot #4525

Closed

Description

@dan-petty

Problem Description

On the shared 16-CPU devcontainer host, the local test row is slow because of load, not because of the cost of the checks. A direct uv run pytest takes no gate slot (ci.gate_slots, #2336), and addopts of pyproject.toml starts -n logical workers (16 here) for each run. So the gate slot limits only devops ci runs, and the runs of other agents still start 16 workers each beside a gate.

Measured on 2026-11-11 from 02:22 UTC, one full uv run devops ci --check --no-cache that held the only gate slot:

  • The load average went from 2 to 41 within 2 minutes and stayed at 40 to 55 for the run.
  • A snapshot by process working directory: the gate's test row used 615% CPU (17 processes); a direct pytest over a list of test files in another worktree used 458% (17 processes, 16 workers); other scripts used about 250%. The 16 workers of the gate got 25% to 43% of a CPU each.
  • The test row hit its 1800 s timeout. mypy took 9 min 40 s and bandit 12 min 30 s, where the GitHub runner runs every static row in 84 to 90 s.
  • Over 45 local gate logs of the last 5 days, the test row had a median of 756 s (minimum 296 s, maximum 1,811 s).

Each xdist worker also collects the whole suite (22,533 items) before it runs a test, so 16 workers pay the collection 16 times in CPU.

Reproduction Steps

  1. Start uv run devops ci --check --no-cache in one worktree.
  2. While it runs, start uv run pytest <many test files> in another worktree.
  3. Read /proc/loadavg and the CPU of each pytest worker (top -b -n 2).

Expected vs Actual Behavior

Expected: a second large pytest run waits for the gate slot, or starts fewer workers, so the gate runs near its idle time.

Actual: both runs start 16 workers, and the gate runs 3 to 6 times slower than alone.

Proposed fix

Let a pytest run with more than one module take part in the gate slot rule. pytest-xdist has the hook pytest_xdist_auto_num_workers, which sets the number of workers for -n logical and -n auto. The root conftest.py can give the narrowed count DEFAULT_CI_TEST_NARROWED_WORKERS (4) when another process holds a gate slot. A run of devops ci keeps its count. Measure the gate time with a second large run before and after.

Environment Diagnostics

Devcontainer on WSL2, 16 CPUs, rootless Podman. Several agents work in their own worktrees at the same time.

Logs, Traces, or Terminal Output

Found by the measurement of #4405.

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

Direct uv run pytest runs ignore the ci.gate_slots rule. On the 16-CPU host they start 16 xdist workers beside a gate run. The gate test row then times out and the static rows run 6 to 9 times slower. The change makes a direct pytest run narrow to 4 workers when another process holds a gate slot. A devops ci run keeps its count.

Acceptance criteria

Requirement: Direct pytest narrows on a held gate slot

A direct pytest run with -n logical or -n auto SHALL use DEFAULT_CI_TEST_NARROWED_WORKERS workers when another process holds a gate slot.

Scenario: Another process holds the gate slot

  • WHEN a direct uv run pytest starts with addopts -n logical and another process holds the gate slot
  • THEN pytest starts 4 xdist workers, not 16.

Requirement: Gate run keeps its worker count

A devops ci test row SHALL keep its configured worker count when it holds a gate slot.

Scenario: The gate run owns the slot

  • WHEN the devops ci test row starts pytest while it owns the gate slot
  • THEN the hook sees DEVOPS_CI_GATE_RUN=1 and returns None, so addopts -n logical selects the worker count.

Requirement: Gate run marks its pytest environment

The devops ci test row MUST set DEVOPS_CI_GATE_RUN=1 in the environment of its pytest process.

Scenario: Marker is present

  • WHEN the devops ci test row starts its pytest process
  • THEN DEVOPS_CI_GATE_RUN=1 is present in that process environment.

Requirement: Explicit worker count is kept

A pytest run with an explicit -n value SHALL keep that value even when a gate slot is held.

Scenario: Explicit -n 8 during a held slot

  • WHEN a user runs uv run pytest -n 8 while another process holds a gate slot
  • THEN pytest starts 8 xdist workers.

Requirement: Narrowed count comes from the constant

The narrowed worker count SHALL come from DEFAULT_CI_TEST_NARROWED_WORKERS.

Scenario: The constant changes

  • WHEN a test sets DEFAULT_CI_TEST_NARROWED_WORKERS to 2 and another process holds a gate slot
  • THEN the hook returns 2.

Requirement: Hook uses the existing gate lock

The hook MUST read the same gate-slot lock that devops ci uses and MUST NOT create a second lock.

Scenario: The gate lock is held

  • WHEN the gate-slot lock records a held slot and DEVOPS_CI_GATE_RUN is absent
  • THEN the hook treats the slot as held and returns the narrowed count.

Existing Tool Considered

No alternatives check has run. pytest-xdist 3.8.0, already a dev dependency in pyproject.toml, covers worker-count selection through the pytest_xdist_auto_num_workers hook. It lacks awareness of the gate slot. A small root conftest.py hook reads the slot and returns DEFAULT_CI_TEST_NARROWED_WORKERS. The change adds no new dependency and builds no worker pool.

Key questions

  • Does pytest-xdist 3.8.0 provide a hook that sets the worker count for -n logical and -n auto?: Yes. pytest_xdist_auto_num_workers(config) returns the worker count for auto and logical detection. An explicit -n value bypasses the hook. [sources: pyproject.toml]
  • Where must the worker narrowing live so that a direct uv run pytest takes it?: In the root conftest.py as a pytest_xdist_auto_num_workers hook. A wrapper around devops ci cannot affect a direct pytest run. (best practice) [sources: pyproject.toml]
    • Chosen: Root conftest hook. pytest loads it for every run, including a direct uv run pytest, and it controls auto and logical worker selection.
    • Rejected: Fixed low worker count in addopts. It would slow the full suite and the gate run, which need logical workers.
    • Rejected: Wrapper script or alias around pytest. A direct uv run pytest bypasses the wrapper.
  • How does a direct pytest run know that another process holds a gate slot, while a devops ci run keeps its count?: The devops ci test row sets DEVOPS_CI_GATE_RUN=1 in the environment of its pytest process. The hook narrows only when the marker is absent and the gate-slot lock is held. (best practice) [sources: src/devops_cli/commands/ci.py, src/devops_cli/config/defaults.py:470]
    • Chosen: Marker environment variable plus the existing gate-slot lock. It tells the owner from another process without guessing process ancestry.
    • Rejected: Compare the lock owner PID with the pytest PID. devops ci holds the lock in the parent process, not in pytest.
    • Rejected: Use the system load average. Load average does not say who holds the slot and changes between reads.
  • Which worker count does a narrowed direct run use?: DEFAULT_CI_TEST_NARROWED_WORKERS, currently 4, from src/devops_cli/config/defaults.py. [sources: src/devops_cli/config/defaults.py:470, docs/TESTING.md:159]
  • Should the hook narrow every direct auto or logical run, or only a run with more than one module?: Narrow every direct auto or logical run when another process holds a gate slot. A single module still starts 16 workers and pays the collection and startup cost 16 times. (best practice) [sources: pyproject.toml, src/devops_cli/config/defaults.py:470]
    • Chosen: Narrow every direct auto or logical run. It removes the contention for a single module as well as for many modules.
    • Rejected: Narrow only a run with more than one module. A single module still starts 16 workers and pays the collection and startup cost 16 times.
  • Should the change also reduce the duplicate collection that each xdist worker does?: No. Keep the change to the worker count. Duplicate collection is a separate cost and a separate change. (best practice) [sources: pyproject.toml]
    • Chosen: Keep the change to the worker count. The issue measures worker contention. Collection is a second cause with a different fix.
    • Rejected: Also change collection in this issue. It expands the change and mixes two risks.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-11: not-applicable.
Class: perf, no search.
Tool: none found.

Activity

  1. added
    source/agent
    type/infra
    scope/ci
    status/new
    value/medium
    effort/low
    on Nov 11, 2026
  2. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/infra. The perf prefix of the title gives the type.
    • Scope: scope/ci. The (ci) scope of the title gives the scope.
    • Priority: P2-Medium. Severe local slowdown (3-6x, timeout hits) but limited to shared devcontainer hosts, not the production CI pipeline.
    • Value: Medium. Restores near-idle gate times for developers on shared 16-CPU hosts, removing a recurring source of failed local runs.
    • Effort: Low. The fix is a single pytest_xdist_auto_num_workers hook in the root conftest.py that reads the gate slot state and returns a narrowed count.
    • Related: chore(ci): the local test row takes over 5 minutes #4405, which asks for a different change. Both address slow local test rows on a shared host; #4405 asks to measure and diagnose the cause, while this candidate proposes a specific fix (conftest.py hook to narrow workers when a gate slot is held).
  3. added
    priority/p2-medium
    on Nov 11, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 11, 2026
  5. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 5f75d25 (see proposed design in issue body).

  6. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Scope from the CI audit (2026-11-11)

    Added facts (measured)

    • The worker cap needs no new hook. xdist reads PYTEST_XDIST_AUTO_NUM_WORKERS for -n logical (xdist/plugin.py:16-20), and build_subprocess_env already lets it through (src/devops_cli/core/process.py:103-106).
    • The waiter has no limit of its own, but each holder stops within its 1800 s check timeout.
    • The figure of 19.6 s against 6.2 s for a trivial test is the start cost of the workers, not contention.

    Solution

    • In pytest_configure of the controller, take the slot with filelock. Skip this in xdist workers and when devops ci already holds the slot.
    • Set PYTEST_XDIST_AUTO_NUM_WORKERS to the CPUs per slot for direct runs.

    Acceptance criteria

    • A direct pytest run waits while devops ci holds the only slot.
    • -n logical gives the CPUs-per-slot number of workers.

    Risk: a slot holder that hangs makes direct runs wait. The lock is released when the process stops.

  7. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: perf. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-11. A person decides what this means for the item. The check changes no label and closes no issue.

  8. added
    owner/agent
    status/in-progress
    and removed
    status/ready
    on Nov 11, 2026
  9. removed
    status/in-progress
    owner/agent
    on Nov 12, 2026
  10. devops-cli-service commented on Nov 12, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-12 04:08 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/cisource/agenttype/infravalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions