Description
test_snapshot_pruning_removes_outdated_records in tests/unit/_autoscaling/test_snapshotter.py is consistently failing across multiple CI runs on Ubuntu (Python 3.13 and 3.14), even when running in isolation (run_alone marker with numprocesses=1).
Failure
FAILED tests/unit/_autoscaling/test_snapshotter.py::test_snapshot_pruning_removes_outdated_records - assert 3 == 2
The test expects that after emitting 4 events with timestamps [now-5h, now-3h, now-2h, now] and a _SNAPSHOT_HISTORY of 2 hours, only 2 snapshots should remain (the ones within the 2-hour window). However, 3 snapshots are retained instead of 2.
Root cause hypothesis
The snapshot pruning logic likely has an off-by-one or boundary condition issue when determining which records fall outside the history window. The now-2h timestamp sits exactly at the boundary of the 2-hour history window, and depending on timing/precision of datetime.now() between the test setup and the pruning execution, it may or may not be pruned.
Observed in
Steps to reproduce
Run the test repeatedly:
pytest tests/unit/_autoscaling/test_snapshotter.py::test_snapshot_pruning_removes_outdated_records -x --count=50
Description
test_snapshot_pruning_removes_outdated_recordsintests/unit/_autoscaling/test_snapshotter.pyis consistently failing across multiple CI runs on Ubuntu (Python 3.13 and 3.14), even when running in isolation (run_alonemarker withnumprocesses=1).Failure
The test expects that after emitting 4 events with timestamps
[now-5h, now-3h, now-2h, now]and a_SNAPSHOT_HISTORYof 2 hours, only 2 snapshots should remain (the ones within the 2-hour window). However, 3 snapshots are retained instead of 2.Root cause hypothesis
The snapshot pruning logic likely has an off-by-one or boundary condition issue when determining which records fall outside the history window. The
now-2htimestamp sits exactly at the boundary of the 2-hour history window, and depending on timing/precision ofdatetime.now()between the test setup and the pruning execution, it may or may not be pruned.Observed in
ubuntu-latestwith Python 3.13 and 3.14 (PR test: Mark flaky tests withrun_aloneto reduce CI failures #1733)ubuntu-latestwith Python 3.13 (commit 9d3da48)test_get_cpu_samplefailure (same file, similar pattern) on Windows with Python 3.13 (commit df1347a)Steps to reproduce
Run the test repeatedly: