fix: prevent memory leaks in runner that cause OOM over time#67
Open
archon-agent wants to merge 1 commit intomoazbuilds:masterfrom
Open
fix: prevent memory leaks in runner that cause OOM over time#67archon-agent wants to merge 1 commit intomoazbuilds:masterfrom
archon-agent wants to merge 1 commit intomoazbuilds:masterfrom
Conversation
Three fixes for long-running daemon deployments:
1. collectStream with 10MB cap: replace unbounded `new Response().text()`
with a streaming reader that caps output at 10MB. Prevents runaway
Claude responses from consuming all available RAM.
2. clearTimeout after process completion: the timeout timer was never
cleared on normal exit, leaking timer references indefinitely.
3. Queue chain reference reset: `task.catch(() => {})` still passes
through fulfilled values, keeping references to all previous results.
Changed to `task.then(() => {}, () => {})` which discards them.
In production, these leaks caused the bun process to grow to 23GB over
several days, eventually triggering an OOM kill.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
Three fixes for memory leaks in
src/runner.tsthat cause the bun process to grow unboundedly over time, eventually triggering OOM kills on long-running daemon deployments.1. Capped stream collection (10 MB limit)
new Response(proc.stdout).text()with acollectStream()helper that caps output at 10 MB2. Timer cleanup after process exit
setTimeoutfor process timeout was never cleared on normal completionclearTimeoutin both success and error paths to prevent timer reference leaks3. Queue chain reference reset
task.catch(() => {})passes through fulfilled values, keeping references to all previous task results in the promise chaintask.then(() => {}, () => {})which discards result references, allowing GC to reclaim themglobalQueueandthreadQueuesContext
In production (24/7 daemon serving Telegram bot), these leaks caused the bun process to grow from ~43 MB to 23 GB over several days, eventually triggering an OOM kill and crashing the VM.
Test plan
🤖 Generated with Claude Code