Skip to main content

SKY-L043 · Logic and resilience

Sleep used for test synchronization

Low

A test starts background work, sleeps for a fixed time with time.sleep(), then checks that a threading.Event was set. The sleep is a guess at how long the work takes.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

On a busy CI runner the work takes longer than the guess and the test fails at random (a flaky test); on a fast machine every run wastes the full delay. Teams learn to rerun failures, and real failures get rerun too.

What Skylos flags

tests/test_worker.py · PythonReported on line 10
import threadingimport time from app.worker import start_worker  def test_worker_processes_job():    done = threading.Event()    start_worker(on_finish=done.set)    time.sleep(0.5)    assert done.is_set()

What Skylos leaves alone

tests/test_worker.py · Python
import threadingimport time from app.worker import debounce  def test_debounce_fires_after_quiet_period():    fired = threading.Event()    debounced = debounce(fired.set, wait=0.2)    debounced()    time.sleep(0.3)    assert fired.is_set()

Why Skylos leaves it alone

Here the waiting is what the test is about: a debounced call must fire only after a quiet period. Skylos skips tests whose names mention timing (debounce, delay, timeout, sleep, interval, latency and similar) and tests that read a clock such as time.monotonic().

How Skylos decides

  1. Runs with --quality on Python test files that pytest would collect (test_*.py and *_test.py by default) and on proven unittest.TestCase classes. Skipped and xfail tests are left out.
  2. Needs a local threading.Event() assigned once in the test and used only through its methods (done.set, done.is_set() and so on), and the standard time and threading modules, not patched or shadowed in the file.
  3. Reports a time.sleep() statement with a fixed positive number when the very next statement is assert event.is_set() (or == True/is True) for that event.
  4. Skips the sleep inside pytest.raises or assertRaises, and tests whose names contain a timing word or that call time.time(), time.monotonic() or time.perf_counter(). Reports LOW on the line of the sleep.

How to fix it

Wait for the event itself, with a generous upper bound. Event.wait() returns as soon as the event is set and returns False on timeout, so the test is both faster and stable.

tests/test_worker.py · Python · the fix, against the flagged file
import threadingRemoved: import time from app.worker import start_worker  def test_worker_processes_job():    done = threading.Event()    start_worker(on_finish=done.set)Removed:     time.sleep(0.5)Removed:     assert done.is_set()Added:     assert done.wait(timeout=5)

When it can be wrong

  • It misses sleeps followed by other checks, such as a queue length or a database row, and sleeps with a computed delay. Only the Event pattern above is proven.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the line of the sleep.

    Python
    time.sleep(0.5)  # skylos: ignore[SKY-L043] worker has a fixed start delay
  • [tool.skylos] ignore = ["SKY-L043"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.