Skip to content

fix(tests): the polls of wait_until count as real sleeps #6696

Closed

Description

@dan-petty

Problem

The real-sleep guard of #4669 (refuse_real_sleeps in tests/conftest.py) adds the seconds of each time.sleep call during a test, and fails the test above 0.25 s. wait_until in tests/support/waits.py polls its condition with a real sleep of 0.01 s (POLL_INTERVAL_SECONDS) until the condition holds, within THREAD_HANDOFF_BOUND_SECONDS. So each poll counts toward the budget of the guard. When a thread handoff takes more than 0.25 s on a loaded runner, the test fails, although wait_until gives the handoff a generous bound on purpose (#1664).

Evidence: on PR #6692 (head f78aa20, 2026-11-21), Tests & Coverage shard 4 of 5 failed with one error. The PR does not change this module.

ERROR tests/github/test_github_session.py::test_closing_a_session_cancels_a_request_waiting_out_a_retry - Failed: real sleep of 0.45 s exceeded 0.25 s budget; fake the clock with the `clock` fixture or mark the test with `@pytest.mark.real_sleep`

The test fakes the retry wait (_sleep_on_the_loop, an anyio sleep). Its only real sleeps are the polls of wait_until(lambda: bool(github_api.sent)). 18 test files call wait_until, so each of them can fail in the same way under load.

Expected

A test that waits for a thread handoff with wait_until passes under load, and the guard still fails a test that sleeps real time where it could fake the clock. The budget of 0.25 s stays as it is.

Acceptance criteria

  • A test shows that a wait_until whose condition holds after more than 0.25 s of polling does not fail the real-sleep guard.
  • A test shows that a plain time.sleep of more than 0.25 s in a test still fails the guard.

Dependencies

None. Related: #4669, #6317, #1664.

Alternatives check of 2026-11-21: none.
Tool: none found.
Reason: Searches found only clock-faking libraries (freezegun, time-machine) and no pytest plugin with a per-test real-sleep budget or a way to exempt a poll sleep; the guard is the repository's own conftest code.

Lacks:

  • No per-test real-sleep budget
  • No exemption for a wait helper's poll sleeps
  • Fakes clocks instead of allowing a real handoff wait

Proposed design

Problem statement

The real-sleep guard in tests/conftest.py charges the poll sleeps of wait_until to the test's 0.25 s budget. A slow thread handoff then fails a test that only waits for the handoff. The change exempts the poll sleeps of wait_until and keeps the guard for every other real sleep.

Acceptance criteria

Requirement: wait_until polls stay off the real-sleep budget

wait_until SHALL poll with a real sleep that the real-sleep guard does not charge to the test's budget.

Scenario: A poll past the budget

  • WHEN A test monkeypatches tests.support.waits.POLL_INTERVAL_SECONDS to 0.3 and calls wait_until with a condition that is false once and then true.
  • THEN The test passes, and tests.support.sleep_budget.charged_seconds() returns 0.0.

Requirement: A plain real sleep still fails the guard

The real-sleep guard SHALL fail a test whose time.sleep call takes its real-sleep total above 0.25 s.

Scenario: A plain sleep over the budget

  • WHEN A pytester run loads the real-sleep guard and executes a test that calls time.sleep(0.3) with no exemption active.
  • THEN The run reports one teardown error that matches real sleep of and exceeded 0.25 s budget.

Requirement: The exemption covers only wait_until polls

The real-sleep guard SHALL charge a real sleep that a test makes outside a wait_until poll to the test's budget.

Scenario: A sleep outside the exemption

  • WHEN A test calls time.sleep(0.05) with no wait_until poll active.
  • THEN tests.support.sleep_budget.charged_seconds() returns a value of at least 0.05.

Existing Tool Considered

The alternatives check in the issue body records verdict none and names no tool. No open, maintained tool covers a per-test real-sleep budget with an exemption for a poll helper. The change builds about 25 lines in tests/support/sleep_budget.py and the wrapper in tests/conftest.py, plus tests. Written cost: about 8,000 tokens to build, keep and fix that code, and about 2 hours elapsed.

Key questions

  • Where do the polls of wait_until charge the real-sleep budget?: refuse_real_sleeps in tests/conftest.py wraps time.sleep and adds each call's seconds to the test's total. wait_until in tests/support/waits.py sleeps POLL_INTERVAL_SECONDS per poll. The guard reports the total at teardown, as the reply's ERROR at teardown shows. [sources: tests/conftest.py:793, tests/support/waits.py:37, tests/github/test_service_limits.py:1060]
  • How does the guard know that a sleep is a poll of wait_until?: wait_until enters a narrow thread-local exemption in a new tests/support/sleep_budget.py around each poll sleep. The time.sleep wrapper charges a sleep only when that exemption is not active. The module holds the budget, the thread-local flag, charged_seconds() and reset(). (best practice) [sources: tests/conftest.py:793, tests/support/waits.py:37]
    • Chosen: An explicit exemption context manager that only wait_until enters. It is narrow, it has one call site, the guard keeps charging every other sleep, and no test file changes.
    • Rejected: Detect the caller frame in the wrapper. A stack walk on every sleep is slow and fragile, and it exempts any sleep that a test makes through that module.
    • Rejected: Mark each of the 18 test files with @pytest.mark.real_sleep. It hides every real sleep in those tests, so the guard loses its value there, and it edits many files.
    • Rejected: Subtract the polling time from the total after wait_until returns. It needs a second global counter that can drift, and it hides an over-budget sleep until after it happens.
  • When does the guard fail a test whose real sleeps pass the budget?: The guard fails at the teardown of the test, as the reply's ERROR at teardown shows. The change keeps that timing. [sources: tests/conftest.py:793, tests/github/test_service_limits.py:1060]
  • How does the test show that a plain time.sleep still fails the guard?: A pytester nested run loads the real-sleep guard and executes a test that calls time.sleep(0.3) with no exemption. The run reports one teardown error with the budget message. (best practice) [sources: tests/conftest.py:793, tests/github/test_service_limits.py:1060]
    • Chosen: A pytester nested run that loads the real guard. The guard fails at teardown, so a test cannot catch the failure around the sleep call; the nested run observes the real teardown error.
    • Rejected: pytest.raises(pytest.fail.Exception) around time.sleep(0.3). The guard reports the failure at teardown, not at the call, so the context manager does not catch it.
    • Rejected: Call the charge function directly. It does not show that a plain time.sleep still fails the guard.
  • How does the test make one poll pass the budget without a long wait?: The test monkeypatches tests.support.waits.POLL_INTERVAL_SECONDS to 0.3. wait_until reads the module constant at each call, so one real poll sleep passes the 0.25 s budget and the test costs 0.3 s. (best practice) [sources: tests/support/waits.py:37, tests/conftest.py:793]
    • Chosen: Patch POLL_INTERVAL_SECONDS to 0.3 and keep the real sleep. The sleep goes through the real time.sleep path, and the test costs 0.3 s.
    • Rejected: Poll for 0.26 s at the real 0.01 s interval. It costs 26 polls, and its timing depends on the load of the runner.
    • Rejected: Fake the poll sleep at a seam. The guard then sees no sleep, so the test cannot show that the guard exempts it.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    source/agent
    status/new
    value/medium
    on Nov 21, 2026
  2. dan-petty commented on Nov 21, 2026

    @dan-petty
    OwnerAuthor
    • Priority: P0-Critical. The guard causes CI test shards to fail under load, which is a CI failure.
    • Related: fix(tests): refuse real sleeps in tests #4669, which asks for a different change. Both concern the real-sleep guard in tests/conftest.py; #4669 added the guard and the candidate fixes a false positive in it.
  3. added
    effort/low
    scope/tests
    priority/p0-critical
    on Nov 21, 2026
  4. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  5. dan-petty commented on Nov 21, 2026

    @dan-petty
    OwnerAuthor

    One more test fails for this cause. CI run of PR #6697 (head 5b2d17a, a documentation-only change), job Tests & Coverage (shard 3 of 5), https://github.com/dan-petty/devops-cli/actions/runs/44955966509/job/137004044493:

    ERROR at teardown of tests/github/test_service_limits.py::test_each_github_request_of_a_round_names_the_workers_repository_and_its_job_row[webhook] - Failed: real sleep of 0.30 s exceeded 0.25 s budget

    The test fakes the clock of the manager (_manager(..., clock=_Clock()), line 1057) and waits with wait_until(lambda: len(seen) == 3) (line 1060), so the polls of wait_until are the real sleep that the guard counts.

  6. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • pytest plugin detect real time.sleep calls in tests fail, results: 5
    • pytest sleep budget plugin limit total sleep time per test, results: 5
    • "time.sleep" pytest plugin "real sleep" guard tests conftest github, results: 5
    • pytest-flakefinder pytest plugin that fails tests which call time.sleep, results: 5
    • pytest-sleep package PyPI, results: 5
    • python library detect slow tests time.sleep monkeypatch pytest plugin, results: 5
    • freezegun pytest fake clock sleep still real, results: 5
    • github pytest plugin assert no time.sleep calls test suite, results: 5
    • "pytest" plugin "sleep" detection open source tool 2025, results: 5
    • pytest plugin fails test that sleeps real time instead of faking clock, results: 5

    Sources:

  7. added
    status/ready
    and removed
    status/new
    on Nov 21, 2026
  8. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  9. added
    owner/service
    on Nov 22, 2026
  10. 4 remaining items

  11. added a commit that references this issue on Nov 22, 2026
  12. added
    status/planned
    owner/service
    status/in-progress
    and removed
    status/ready
    status/planned
    on Nov 22, 2026
  13. dan-petty commented on Nov 22, 2026

    @dan-petty
    OwnerAuthor

    One more test fails for this cause, in the CI run of PR #6719 (head 477d709, job Tests & Coverage (shard 1 of 5)): ERROR tests/github/test_service_limits.py::test_a_suspended_installation_halts_the_service[gh-says-the-installation-is-suspended] - Failed: real sleep of 0.29 s exceeded 0.25 s budget. It is the second test of this file with the same cause (the first is in the comment above).

  14. added
    status/in-review
    and removed
    status/in-progress
    on Nov 22, 2026
  15. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    The draft pull request #6737 holds the final title and body. In mode on of roadmap.fix_checks.mode, the watch row marks it ready when its checks pass, and a fix-checks Job repairs a failed check whose name gives the class test, lint or types. In each other case the watch row stops the item: another failed check, a cancelled check, a conflict, a fix-checks Job that ended with no repair, or another mode.

    Spend: 73 model requests, 2974229 input tokens, 28864 output tokens.

  16. removed
    owner/service
    on Nov 22, 2026
  17. removed
    status/in-review
    on Nov 22, 2026
  18. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-22 04:24 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/p0-criticalscope/testssource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions