Skip to content

fix: isolate random byte generators per request - #767

Merged
harryjackson merged 1 commit into
postmanlabs:masterfrom
anticipate218:fix/request-local-byte-randomness
Nov 16, 2026
Merged

harryjackson merged 1 commit into
postmanlabs:masterfrom
anticipate218:fix/request-local-byte-randomness

Conversation

@anticipate218

Copy link
Copy Markdown
Contributor

A seeded /stream-bytes response currently uses the process-wide random generator. If another byte request runs between chunks, it reseeds or advances that generator and changes the remainder of the first response, even though its seed is unchanged.

Give /bytes and /stream-bytes their own random.Random instance for each request. The streaming closure retains that instance, so interleaved responses remain independent. Existing seeded byte sequences, size limits and chunk boundaries are preserved.

The two regression tests use real unbuffered Flask response iterators, without threads or RNG mocks. They cover a stream interrupted by a /bytes request and two interleaved streams with different chunk sizes. Both fail on the original implementation; the two existing seed controls still pass. All four pass with the fix.

Validation on Windows:

  • Installed the repository's requirements-test.txt with hash verification into isolated environments.
  • Python 3.11.18 and 3.15.2: python -W error::SyntaxWarning test_httpbin.py — 130 tests passed on each.
  • git diff --check passed.

The full runs emit an existing unclosed-iterator assertion from the WSGI validator test. The unmodified base reproduces that assertion while passing its 128 tests. Docker, native Linux, remote target-URL tests, and Python 3.12–3.14 were not run. The new iterator tests use the existing local_only marker because the remote test client eagerly consumes responses.

This was found through source inspection and reproduced locally. AI assistance: OpenAI Codex investigated, implemented and validated this change.

@harryjackson

Copy link
Copy Markdown
Collaborator

Thanks, this is a real bug: the global random.seed() let concurrent byte requests change each other's output. Verified locally: all 130 tests pass, and both new tests fail without the fix. Merging.

@harryjackson
harryjackson merged commit 2c4572b into postmanlabs:master Nov 16, 2026
14 checks passed
@anticipate218

Copy link
Copy Markdown
Contributor Author

Thank you for reproducing it locally and merging, much appreciated!

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