Skip to content

Give the memory-limited response tests room for allocation noise - #5234

Merged
illia-v merged 4 commits into
urllib3:mainfrom
SEPURI-SAI-KRISHNA:fix-flaky-memray-limit
Sep 9, 2026
Merged

illia-v merged 4 commits into
urllib3:mainfrom
SEPURI-SAI-KRISHNA:fix-flaky-memray-limit

Conversation

@SEPURI-SAI-KRISHNA

@SEPURI-SAI-KRISHNA SEPURI-SAI-KRISHNA commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Several limit_memory marks in test/test_response.py sit within a rounding error of their measured peak, so ordinary allocation noise fails them. Two have failed on CI in the past day:

MEMORY PROBLEMS test/test_response.py::TestResponse::test_buffer_memory_usage_no_decoding[False-None-read1]
  - Test was limited to 10.5MiB but allocated 11.0MiB

MEMORY PROBLEMS test/test_response.py::TestBytesQueueBuffer::test_memory_usage_splitting_chunk[finish_with_get_all]
  - Test was limited to 11.0MiB but allocated 12.0MiB

Measured with memray on Python 3.13:

test peak old limit headroom new limit
test_memory_usage 12.13 MiB 12.5 MB +0.37 MiB 15 MB
test_memory_usage_single_chunk 10.00 MiB 10.01 MB +0.01 MiB 12 MB
test_memory_usage_splitting_chunk 11.00 MiB 11.01 MB +0.01 MiB 13 MB
test_buffer_memory_usage_no_decoding 10.00 MiB 10.5 MB +0.50 MiB 13 MB

Three of them allow 10 KiB of slack on a 10 MiB allocation. That is not a memory bound, it is an assertion that the allocator behaves exactly as it did when the number was written.

Each of these tests guards against the body being retained a second time, which takes the peak to roughly 20 MiB, so there is a wide gap between the real peak and the regression being caught. The new limits keep at least a 5 MiB margin below a doubled body while giving noise 2-3 MiB instead of 0.01 MiB.

test_memory_usage_decode_with_max_length (10 MB) and test_buffer_memory_usage_decode_one_chunk (25 MB) are left alone: neither has failed, and I did not measure them.

Verification

  • test/test_response.py passes on 3.12 and 3.13: 169 passed, 45 skipped.
  • 15 consecutive runs of the whole memory_usage family under
    coverage run --parallel-mode, as the CI session runs them: clean.
  • Adding a genuine extra copy of the body to each test makes all six
    parametrizations fail at the new limits, so they still catch what they
    are for.

One caveat stated plainly: I could not reproduce the CI overshoot locally on either 3.12 or 3.13, with or without coverage. The case rests on the measured margins and the observed failures rather than on a local reproduction. If you would rather see the extra megabyte accounted for before widening anything, I am happy to dig into that instead.

The test reads a 10 MiB body and allowed a peak of 10.5 MiB, so half a
mebibyte of headroom. It exists to catch the body being buffered a second
time, which takes the peak to 20 MiB, and 5% is far tighter than that
needs. On CI the test intermittently peaked at 11.0 MiB and failed.

Allow 13 MiB instead. A retained second copy still trips the limit by a
7 MiB margin, and the noise that was failing the job no longer does.
test_memory_usage_single_chunk and test_memory_usage_splitting_chunk
allowed 10 KiB over their measured peak, and test_memory_usage allowed
0.37 MiB. Margins that thin do not measure a memory bound, they measure
whether the allocator behaves exactly as it did when the number was
written, and CI has now failed on two of these tests in a day.

Each of these guards against the body being held a second time, which
takes the peak to about 20 MiB, so there is a lot of room between the
real peak and the thing being caught. Use it: every limit here keeps at
least a 5 MiB margin below a doubled body, and an added copy still trips
all six parametrizations.
@SEPURI-SAI-KRISHNA SEPURI-SAI-KRISHNA changed the title Give test_buffer_memory_usage_no_decoding room for allocation noise Give the memory-limited response tests room for allocation noise Sep 8, 2026
Comment thread test/test_response.py Outdated
@illia-v
illia-v merged commit 9a97e7b into urllib3:main Sep 9, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants