Repository navigation
fix: bound process memory so a full RAM cache can't OOM-kill the container - #5
Merged
Merged
Conversation
…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
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.
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_MBcaps 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, sinceevictLockedskips!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=1024withmemory: 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:
exit 137, OOMKilledMeasured 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 explicitGOMEMLIMITstill wins.Also included:
torrent2httpnil-deref race —/status,/lsand/filesresolved a hash then looked it up again, ignoringokand dereferencing the result. A torrent can vanish in between (drop-after-playback fires when the last reader closes), so a client polling/statusas playback ends could panic the process. The resolver now returns the snapshot directly.docker-compose.ymlcomment.Notes
GOMEMLIMITis 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.usedcan 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