SKY-L043 · Logic and resilience
Sleep used for test synchronization
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
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
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
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
- Runs with
--qualityon Python test files that pytest would collect (test_*.pyand*_test.pyby default) and on provenunittest.TestCaseclasses. Skipped andxfailtests are left out. - 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 standardtimeandthreadingmodules, not patched or shadowed in the file. - Reports a
time.sleep()statement with a fixed positive number when the very next statement isassert event.is_set()(or== True/is True) for that event. - Skips the sleep inside
pytest.raisesorassertRaises, and tests whose names contain a timing word or that calltime.time(),time.monotonic()ortime.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.
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.