fix: hold GET memory reservation for the whole streaming-response lifetime#98
Merged
Merged
Conversation
…etime A multipart-encrypted GET returns a StreamingResponse whose body is sent AFTER the request handler returns. The handler released the GET's memory reservation in its finally -- before a single byte was streamed -- so concurrent streaming GETs ran completely ungoverned. Each stream holds ~one decrypted 8MB frame in uvicorn's transport send buffer, so a flood of concurrent GETs accumulated frames (N x 8MB) and OOMKilled the pod while the limiter's active_mb read ~64MB. This was the dominant prod OOM (the kill windows were dominated by 100+ concurrent GETs), and it survived the earlier copy-govern fix untouched. Fix: transfer the reservation to the streaming body iterator so it's released only when the stream is exhausted or the consumer disconnects (admission control) -- the limiter now bounds how many streaming GETs run at once. Drop the per-frame nested acquires in the multipart GET path: stream concurrency is now bounded at admission, so the working set is O(concurrent streams) not O(frames), and a nested per-frame acquire would deadlock against the held reservation. Verified locally at prod config (512Mi cap, 64MB budget): the 90-concurrent multipart GET flood that OOMKilled the pod (exit 137, 0/180) now peaks ~325MiB and completes 180/180; a realistic upload+GET mix peaks ~305MiB, 106/106 ok. Profiler confirms tracked memory drops 812MB -> 112MB and live frames 90 -> 11.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fix the dominant s3proxy concurrent-backup OOM: streaming GET responses ran completely ungoverned.
Root cause (reproduced + profiled locally)
A multipart-encrypted GET returns a
StreamingResponsewhose body is sent after the request handler returns. The handler released the GET's memory reservation in itsfinally— i.e. before a single byte was streamed — so concurrent streaming GETs ran with no governance. Each stream holds ~one decrypted 8MB frame in uvicorn's transport send buffer, so a flood of concurrent GETs accumulatedN × 8MBand OOMKilled the pod (exit 137) while the limiter'sactive_mbread ~64MB.This is what kept
2026.6.13OOMing in prod: the kill windows were dominated by 100+ concurrent GETs, and the earlier copy-govern fix (#97) never touched this path.Profiler evidence (
S3PROXY_TRACEMALLOC): peak snapshot heldcrypto.py:394(aesgcm.decrypt) = 576–720MB across 72–90 live 8MB frames = 90% of tracked memory.Fix
_release_after_stream) so it's released only when the stream is exhausted or the consumer disconnects. The limiter now bounds how many streaming GETs run at once (admission control).try_acquire_memoryin the multipart GET path: concurrency is now bounded at admission, so the working set is O(concurrent streams) not O(frames), and a nested per-frame acquire would deadlock against the held reservation.Proof (local, prod config: 512Mi cap / 64MB budget)
Profiler with fix: tracked memory 812MB → 112MB, live frames 90 → 11.
Scope / follow-up
A synthetic 192-parallel-UploadPart torture test (far harsher than prod's ~57 PUTs) can still OOM via untracked uvicorn C-level socket receive buffers for concurrent PUT bodies — a separate, lesser concern (candidate:
uvicorn --limit-concurrency). Not addressed here.Tests
tests/unit/test_streaming_get_reservation.py: pins that the reservation is held for the whole stream and released exactly once (incl. early consumer disconnect).ruff check+ruff format --checkclean.