Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

EventLogger.hook.ts buffers stdin without a size cap → multi-GB allocation / OOM

Open christauff opened this issue 4 months ago • 1 comments

Issue: EventLogger.hook.ts buffers stdin without a size cap → multi-GB allocation / OOM

Repo: danielmiessler/LifeOS · File: LifeOS/install/hooks/EventLogger.hook.ts (readStdin, ~l.61–69) Version seen: current main (last touched in 7.0.0, 2026-07-12). Line refs approximate — please confirm against the live file.

Thanks for LifeOS and the 7.0.0 hook consolidation. Offered as contributor support, not a complaint. This is a small robustness gap (verified by reproduction on Linux, bun 1.3.7 / node 20).

Summary

readStdin() accumulates stdin with an unbounded data += chunk and only a 2-second setTimeout to settle. There is no size ceiling. If the hook's stdin is a fast or continuous stream, the buffer grows without bound during the 2-second window:

  • bun: buffers ~1.1 GB in ~2 s (RSS ~2.5 GB) before the timer settles.
  • node: throws RangeError: Invalid string length at ~1 GB (uncaught → non-zero exit).

Either way it's a memory spike / OOM risk on a hook that runs on every PostToolUse (and 3 other events), for every user. Under existing memory pressure it can push a machine into OOM.

What I got wrong initially (for transparency)

I first assumed this caused a CPU-pegged infinite hang (O(n²) concat starving the timer). Independent measurement disproved that: string concat in bun/JSC is ~linear (rope strings), and the 2 s timer fires on schedule (~1999 ms) even under a full firehose. The bug is unbounded memory, not a starved core. (I separately observed a hook process at 99.9% CPU for ~13 h in the wild, but I could not reproduce or attribute that to this code path — likely a different wedge, e.g. a synchronous block, or a degenerate-fd read loop. I'm not claiming this code causes that; only that the unbounded buffer is a real memory-DoS gap.)

Reproduction

yes | bun run hooks/EventLogger.hook.ts   # buffers ~1.1 GB in ~2s, then exits (bun) / RangeError (node)

Current code (~l.61–69)

async function readStdin(): Promise<string> {
  return new Promise((resolve) => {
    let data = '';
    const timer = setTimeout(() => resolve(data), 2000);
    process.stdin.on('data', (chunk) => { data += chunk.toString(); }); // <-- no size cap
    process.stdin.on('end', () => { clearTimeout(timer); resolve(data); });
    process.stdin.on('error', () => { clearTimeout(timer); resolve(data); });
  });
}

Proposed fix (tested)

Cap the buffer and settle once:

async function readStdin(): Promise<string> {
  return new Promise((resolve) => {
    let data = ''; let done = false;
    const finish = () => { if (done) return; done = true; clearTimeout(timer); try { process.stdin.pause(); } catch {} resolve(data); };
    const timer = setTimeout(finish, 2000);
    process.stdin.on('data', (chunk) => { data += chunk.toString(); if (data.length > 10_000_000) finish(); });
    process.stdin.on('end', finish);
    process.stdin.on('error', finish);
  });
}

10 MB accommodates large tool_response payloads (big Bash stdout, file Read) while preventing the multi-GB spike. Beyond the cap the JSON is truncated and that one event is dropped (fail-open, consistent with the hook's existing behavior). Verified: yes | bun run … now exits in ~10–120 ms with the buffer bounded. Happy path unchanged. (An optional .unref()'d setTimeout(process.exit, 10s) is harmless defense-in-depth, but note a JS timer can't rescue a genuinely wedged loop — the cap is the real fix.)

Happy to open a PR.

christauff avatar Jul 18 '26 18:07 christauff

Fixed — and your report paid off six times over: the same uncapped reader existed in five sibling hooks (SatisfactionCapture, LoopDetector, DriftReminder, PromptProcessing, HookHealer), so all six now cap at 10MB and settle immediately past the cap. Verified with your exact repro: yes | bun run hooks/EventLogger.hook.ts now exits in ~140ms, bounded. The transparency section on what you initially got wrong made this a pleasure to verify. Ships in the next release.

danielmiessler avatar Jul 18 '26 23:07 danielmiessler