Skip to content

fix: bound process memory so a full RAM cache can't OOM-kill the container - #5

Merged
jodacame merged 3 commits into
mainfrom
fix/ram-cache-oom
Aug 8, 2026
Merged

jodacame merged 3 commits into
mainfrom
fix/ram-cache-oom

Conversation

@jodacame

@jodacame jodacame commented Aug 8, 2026

Copy link
Copy Markdown
Owner

A RAM-mode stream can kill the server mid-playback. The process is OOM-killed by the kernel (exit 137, OOMKilled: true) — no panic, no error in the logs, the container simply dies while a movie is playing.

Cause

FT_CACHE_SIZE_MB caps the piece payload held by the RAM store, not the process. On top of it sit pieces still in flight (allocated in full on first write, and unevictable until they verify, since evictLocked skips !complete), per-peer buffers, and heap Go has not returned to the OS. Resident memory therefore settles well above the cap, scaling with peer count.

That makes the documented defaults unsafe: FT_CACHE_SIZE_MB=1024 with memory: 1536M, whose comment claimed to be "headroom over FT_CACHE_SIZE_MB". It only bites on healthy, fast, well-seeded public torrents — private ones are unaffected because auto-seeding forces them to disk storage, so they never fill the ring buffer.

Reproduction

6.3GB torrent, 50+ peers, ~200 Mbps, cache 1024MB in a 1536MB container:

Before After
Outcome died at 60s, exit 137, OOMKilled stable for 3+ min, still streaming
Resident memory 1.28 GiB and climbing ~1.2 GiB, flat

Measured overhead: ~2.7× the cap at a 64MB cache with ~20 peers; the margin grows with peers.

Fix

Derive a Go soft memory limit from the cache size at startup (cache + 25%, clamped to a 192–512MB margin) and clamp it to 85% of the container cgroup limit. The GC now pushes back as the ceiling approaches instead of the kernel killing the process, and a cache too large for its container is clamped and logged rather than fatal. An explicit GOMEMLIMIT still wins.

Also included:

  • torrent2http nil-deref race — /status, /ls and /files resolved a hash then looked it up again, ignoring ok and dereferencing the result. A torrent can vanish in between (drop-after-playback fires when the last reader closes), so a client polling /status as playback ends could panic the process. The resolver now returns the snapshot directly.
  • README section on budgeting memory in RAM mode, and a corrected docker-compose.yml comment.

Notes

  • GOMEMLIMIT is a soft limit: if the working set genuinely exceeds it, the collector works harder (CPU) instead of the process dying — degradation rather than a crash.
  • The underlying accounting is unchanged: in-flight pieces remain unevictable and used can still exceed the cap. The GC absorbs it; a ring-buffer redesign is out of scope here.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LcSmC4nXzcn2UAofR7JCgZ

jodacame and others added 3 commits August 8, 2026 13:51
…ntainer

FT_CACHE_SIZE_MB caps only the piece payload held by the RAM store. Pieces in
flight are allocated in full on first write and cannot be evicted until they
verify, and per-peer buffers and GC lag sit on top, so resident memory settles
well above the configured cap and scales with peer count. A container sized
"cache + a little headroom" is therefore OOM-killed exactly when a torrent is
healthy and fast — public, well-seeded torrents in RAM mode. Private torrents
were unaffected because auto-seeding forces them to disk storage.

Reproduced with a 6.3GB torrent at 200Mbps and 50+ peers: cache 1024MB in a
1536MB container died at 60s with exit 137 / OOMKilled, no panic. With the
heap bounded, the same run holds flat at ~1.2GiB indefinitely.

Derive a Go soft memory limit from the cache size at startup and clamp it to
85% of the container's own cgroup limit, so raising the cache in Settings can
never push the process past what the container may use — the GC pushes back
instead of the kernel, and an oversized cache is reported rather than fatal.
An explicit GOMEMLIMIT still wins.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LcSmC4nXzcn2UAofR7JCgZ
/status, /ls and /files resolved a hash and then looked it up again, ignoring
the ok flag and dereferencing the result. A torrent can disappear between the
two calls — drop-after-playback fires the moment the last reader closes — so a
client polling /status as playback ends could panic the process.

Return the Info snapshot from the resolver instead of a hash to look up twice.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LcSmC4nXzcn2UAofR7JCgZ
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LcSmC4nXzcn2UAofR7JCgZ
@jodacame
jodacame merged commit 6122e01 into main Aug 8, 2026
2 checks passed
@jodacame
jodacame deleted the fix/ram-cache-oom branch August 8, 2026 13:54
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