Skip to content

Commit f02b007

Browse files
authored
test: Deflake memory estimation test and restore its RSS regression coverage (#2202)
`test_memory_estimation_does_not_overestimate_due_to_shared_memory` fails intermittently on Linux CI (e.g. [this run](https://github.com/apify/crawlee-python/actions/runs/33366608389/job/99408458117), ratio 0.52 vs the 0.3 tolerance; previously #1288). The sharing children only map the 100 MB segment and never touch it, so its pages are the coldest in the process tree — under memory pressure the kernel reclaims them first, and every reclaimed page silently leaves the PSS readings that the phase deltas are built from. Two changes: 1. The sharing children now fault every page of the shared segment before the measurement. Besides removing the reclaim target, this restores the test's original purpose: untouched mappings never enter RSS either, so the test passed even with PSS unavailable — the exact overcount regression it was written to catch in #1210. With this change, a forced RSS fallback fails every round by ~11x the tolerance. 2. A distorted measurement round is retried (up to 3 rounds). Transient reclaim does not repeat identically, while a genuine overcount misses the expectation several times over in every round, so the retries cannot mask it. Verified by reproducing CI's failure signature locally under cgroup memory pressure (`systemd-run -p MemoryHigh=` plus a hot-page churner): 2/10 failures before, 0/50 after, 0/40 idle runs, and the forced-RSS regression still fails deterministically. *✍️ Drafted by Claude Code*
1 parent a82e6af commit f02b007

1 file changed

Lines changed: 33 additions & 19 deletions

File tree

‎tests/unit/_utils/test_system.py‎

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def parent_process() -> None:
235235
children_count = 4
236236
# Memory calculation is not exact, so allow for some tolerance.
237237
test_tolerance = 0.3
238+
measurement_rounds = 3
238239

239240
def no_extra_memory_child(ready: synchronize.Barrier, measured: synchronize.Barrier) -> None:
240241
ready.wait()
@@ -254,7 +255,10 @@ def shared_extra_memory_child(
254255
ready: synchronize.Barrier, measured: synchronize.Barrier, memory: SharedMemory
255256
) -> None:
256257
assert memory.buf is not None
257-
print(f'Using the memory... {memory.buf[-1]}')
258+
# Fault every page in: untouched pages never enter the RSS (hiding the overcount this test guards
259+
# against) and are reclaimed first under memory pressure, which drops them from every mapper's PSS.
260+
page_sum = sum(memory.buf[::4096])
261+
print(f'Using the memory... {page_sum}')
258262
ready.wait()
259263
measured.wait()
260264

@@ -293,28 +297,38 @@ def get_additional_memory_estimation_while_running_processes(
293297

294298
return (memory_during - memory_before).to_mb() / count
295299

296-
additional_memory_simple_child = get_additional_memory_estimation_while_running_processes(
297-
target=no_extra_memory_child, count=children_count
298-
)
299-
additional_memory_extra_memory_child = (
300-
get_additional_memory_estimation_while_running_processes(target=extra_memory_child, count=children_count)
301-
- additional_memory_simple_child
302-
)
303-
additional_memory_shared_extra_memory_child = (
304-
get_additional_memory_estimation_while_running_processes(
305-
target=shared_extra_memory_child, count=children_count, use_shared_memory=True
300+
# Under memory pressure the kernel reclaims cold pages, which silently leave the PSS readings and skew a
301+
# round's deltas, so a distorted round is re-measured. A genuine overcount of shared memory misses the
302+
# expectation several times over in every round, so the retries cannot mask it.
303+
for _ in range(measurement_rounds):
304+
additional_memory_simple_child = get_additional_memory_estimation_while_running_processes(
305+
target=no_extra_memory_child, count=children_count
306+
)
307+
additional_memory_extra_memory_child = (
308+
get_additional_memory_estimation_while_running_processes(
309+
target=extra_memory_child, count=children_count
310+
)
311+
- additional_memory_simple_child
312+
)
313+
additional_memory_shared_extra_memory_child = (
314+
get_additional_memory_estimation_while_running_processes(
315+
target=shared_extra_memory_child, count=children_count, use_shared_memory=True
316+
)
317+
- additional_memory_simple_child
306318
)
307-
- additional_memory_simple_child
308-
)
309319

310-
memory_estimation_difference_ratio = (
311-
abs((additional_memory_shared_extra_memory_child * children_count) - additional_memory_extra_memory_child)
312-
/ additional_memory_extra_memory_child
313-
)
320+
memory_estimation_difference_ratio = (
321+
abs(
322+
(additional_memory_shared_extra_memory_child * children_count)
323+
- additional_memory_extra_memory_child
324+
)
325+
/ additional_memory_extra_memory_child
326+
)
314327

315-
estimated_memory_expectation.value = memory_estimation_difference_ratio < test_tolerance
328+
if memory_estimation_difference_ratio < test_tolerance:
329+
estimated_memory_expectation.value = True
330+
break
316331

317-
if not estimated_memory_expectation.value:
318332
print(
319333
f'{additional_memory_shared_extra_memory_child=}\n'
320334
f'{children_count=}\n'

0 commit comments

Comments
 (0)