Skip to content

fix(tests): the concurrent slot leasing test depends on thread timing #2841

Description

@dan-petty

Problem

tests/ai/client/test_ollama_dynamic_leasing.py::test_concurrent_slot_leasing_across_threads failed in one full devops ci run and passed in three later runs of the file alone.

The failure was: assert {'http://example.com:11434', 'http://example.com:11435'} == {... 'http://example.com:11436'}. The worker holds its slot for clock.sleep(0.05). The ClockFake sleep returns at once and does not hold the slot in real time. On a loaded host, one worker can finish and release its slot before another worker leases, so the six workers lease only two of the three nodes.

Expected

The test holds each slot until every worker has leased (for example with a threading.Barrier), or it asserts a property that does not depend on thread timing. The test gives the same result on every run.

Seen in

A full devops ci run of the worktree of #2800 on 2026-10-31, with 16 xdist workers and other agents running on the same host. The test took the fake clock in PR #2833 (#2169).

Proposed design

Problem statement

The test test_concurrent_slot_leasing_across_threads uses ClockFake.sleep to hold a slot, but ClockFake.sleep returns at once without blocking in real time. On a loaded host, a worker can release its slot before another worker leases one, so the six workers see only two of three nodes and the assertion fails. The test must hold every slot until all workers have leased, so the result is the same on every run.

Acceptance criteria

Requirement: deterministic concurrent leasing

The test SHALL use a synchronization primitive that holds every worker's slot until all six workers have leased a node.

Scenario: all six workers hold slots simultaneously

  • WHEN six workers each acquire a slot from three candidate nodes with max_parallel=1
  • THEN all three nodes appear in the set of leased URLs and the assertion passes

Requirement: no dependency on fake clock timing

The test SHALL NOT rely on ClockFake.sleep to hold a slot in real time.

Scenario: test runs without clock.sleep

  • WHEN the test function is executed on a loaded host with 16 xdist workers
  • THEN the test passes and no call to clock.sleep remains in the worker body

Requirement: reproducible result

The test SHALL produce the same assertion result on every run regardless of thread scheduling.

Scenario: repeated runs give the same result

  • WHEN the test is run ten times in a row on a loaded host
  • THEN every run passes with the same set of leased nodes

Existing Tool Considered

No check has run. The fix uses threading.Barrier from the Python standard library. It covers the need to hold all six workers at a point until every worker has acquired a slot. It lacks nothing for this use case. The change edits one test function in one file and adds no new code to build, keep or fix.

Key questions

  • How does the test hold every slot until all workers have leased?: Use threading.Barrier(6). Each worker acquires its slot, calls barrier.wait(), then releases. No worker releases until all six have acquired a slot. The Barrier blocks each thread at wait() until all six threads have called wait(), guaranteeing simultaneous slot holding. (best practice) [sources: tests/ai/client/test_ollama_dynamic_leasing.py:79]
    • Chosen: threading.Barrier(6). Guarantees all six workers hold their slots before any releases. Standard library, no new dependency, deterministic.
    • Rejected: Assert a weaker property (e.g. at least one node leased). Loses the guarantee that all three nodes are reachable. Does not test the full leasing path.
    • Rejected: Use real time.sleep instead of ClockFake.sleep. Still timing-dependent. On a very loaded host a worker can still finish before another starts. Not deterministic.
  • Can the clock fixture parameter be removed from this test?: Yes. The clock parameter at tests/ai/client/test_ollama_dynamic_leasing.py:69 is used only for clock.sleep(0.05) at line 79, which is replaced by the barrier. No other code in the function uses the clock fixture. [sources: tests/ai/client/test_ollama_dynamic_leasing.py:69, tests/ai/client/test_ollama_dynamic_leasing.py:79]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    status/new
    value/medium
    effort/low
    on Oct 31, 2026
  2. dan-petty commented on Oct 31, 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/bug. The fix prefix of the title gives the type.
    • Scope: none. The title has the scope tests, and .github/labels.yml declares no scope/tests label. Add the label to the file to give the scope to this item.
    • Priority: P2-Medium. The test causes intermittent CI failures on loaded hosts but does not block delivery or expose users to defects.
    • Value: Medium. Removes one source of CI flakiness but does not change user-facing behaviour.
    • Effort: Low. The fix is a threading.Barrier or an assertion rewrite in a single test function.
    • Related: fix(tests): the qdrant retry, the process fake's delay and 24 test bodies wait on the clock fixture or an event, not on real time #2169, which asks for a different change. Both concern tests that wait on real time instead of events; #2169 is the broader item that introduced the fake clock this test depends on, while the candidate fixes one specific concurrent-thread assertion.
  3. added
    priority/p2-medium
    on Oct 31, 2026
  4. added
    status/ready
    and removed
    status/new
    on Oct 31, 2026
  5. devops-cli-service commented on Oct 31, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 506f769 (see proposed design in issue body).

  6. added
    status/in-review
    and removed
    status/ready
    on Nov 1, 2026
  7. removed
    status/in-review
    on Nov 1, 2026
  8. added a commit that references this issue on Nov 1, 2026
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-mediumsource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions