Repository navigation
Description
Summary
test/back2back_test.py::TestThreadSafeBus::test_send_periodic_duration fails intermittently on CI with IndexError: list index out of range, from a line that assumes at least one message was received. On a loaded runner that assumption can be false, so the test reports a failure that says nothing about send_periodic.
Observed on fix/bit-timing-configurable-ranges (c029247), job test (macos-latest, py312), run 46472917027:
FAIL [366.7ms] test/back2back_test.py::TestThreadSafeBus::test_send_periodic_duration
IndexError: list index out of range
Everything else in that job passed (739 passed, 140 skipped).
Code involved
test/back2back_test.py, lines 270-284:
def test_send_periodic_duration(self):
"""
Verify that send_periodic only transmits for the specified duration.
Regression test for #1713.
"""
for duration, period in [(0.01, 0.003), (0.1, 0.011), (1, 0.4)]:
messages = []
self.bus2.send_periodic(can.Message(), period, duration)
while (msg := self.bus1.recv(period + self.TIMEOUT)) is not None:
messages.append(msg)
delta_t = messages[-1].timestamp - messages[0].timestamp
assert delta_t < duration + 0.05TIMEOUT is 0.1 (1.0 on PyPy), defined at line 37.
Why it is a race
The last line assumes messages is non-empty, but nothing in the test guarantees it.
On the first iteration the periodic task runs for duration=0.01 s with period=0.003 s — a 10 ms window, roughly three periods. If the timer thread does not get scheduled inside that window (busy CI runner, cold start of the task thread), bus1.recv() returns None on the first call, the while loop appends nothing, and messages[-1] raises IndexError.
The IndexError also hides whichever assertion was intended: the test never reaches assert delta_t < duration + 0.05, so a genuine send_periodic regression and a slow runner produce indistinguishable output.
Impact
- Failures that do not indicate a product defect, on an unrelated feature.
- The message points at
messages[-1], not at "no message was received", which is what actually happened.
Similar races in this file have been fixed before: #1349 (UDP multicast) and #2172 (listen_only).
Possible fixes
- Assert the assumption explicitly instead of raising
IndexError, e.g.assert messages, "no message was received within ...". Cheap, but the test still fails on a slow runner. - Wait for the first message before measuring the duration, so the window is measured from the first delivery rather than from
send_periodic(), and keep thedelta_tassertion unchanged. - Retry the smallest case once before failing, since a missed 10 ms window is not evidence of a defect.
Option 2 is the one that keeps the regression coverage for #1713 intact. I am happy to send a patch if any of these is the preferred direction.
Activity