Skip to content

fix: hold GET memory reservation for the whole streaming-response lifetime#98

Merged
ServerSideHannes merged 1 commit into
mainfrom
fix/govern-streaming-get-memory
Jun 30, 2026
Merged

fix: hold GET memory reservation for the whole streaming-response lifetime#98
ServerSideHannes merged 1 commit into
mainfrom
fix/govern-streaming-get-memory

Conversation

@ServerSideHannes

Copy link
Copy Markdown
Owner

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 StreamingResponse whose body is sent after the request handler returns. The handler released the GET's memory reservation in its finally — 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 accumulated N × 8MB and OOMKilled the pod (exit 137) while the limiter's active_mb read ~64MB.

This is what kept 2026.6.13 OOMing 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 held crypto.py:394 (aesgcm.decrypt) = 576–720MB across 72–90 live 8MB frames = 90% of tracked memory.

Fix

  • Transfer the reservation to the streaming body iterator (_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).
  • Drop the per-frame nested try_acquire_memory in 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)

Test Before After
90-concurrent multipart GET flood OOMKilled, exit 137, 0/180 180/180 ok, peak ~325MiB
realistic upload+GET mix 106/106 ok, peak ~305MiB

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

  • New 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).
  • Full unit suite green (451); ruff check + ruff format --check clean.

…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.
@ServerSideHannes ServerSideHannes merged commit 33e4d6c into main Jun 30, 2026
4 checks passed
@ServerSideHannes ServerSideHannes deleted the fix/govern-streaming-get-memory branch June 30, 2026 17:32
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.

1 participant