Repository navigation
Description
Summary
A hook-rewritten tail -n 3000 big.log consumed 49 GB RSS + 42 GB swap and drove a 64 GB machine into OOM twice. The agent had issued a correctly-bounded tail; the PreToolUse hook converted it into rtk read --tail-lines 3000, which reads the entire file into memory before discarding all but the last N lines. The file was a 161 GB unrotated debug log.
Three distinct defects combine so that there is currently no supported configuration that avoids this.
Environment: rtk 0.43.0 (latest release), x86_64 Linux (WSL2), Claude Code PreToolUse hook installed per rtk init -g.
1. rtk read does not stream — it holds ~3.9x file size
Measured with /usr/bin/time -v, output piped to /dev/null so no shell command-substitution buffering is involved:
yes "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz" \
| head -c 2000000000 > big.log # 2.0 GB
/usr/bin/time -v rtk read big.log 2>&1 >/dev/null | grep 'Maximum resident'
/usr/bin/time -v rtk read big.log --tail-lines 5 2>&1 >/dev/null | grep 'Maximum resident'
/usr/bin/time -v rtk read big.log --max-lines 5 2>&1 >/dev/null | grep 'Maximum resident'
/usr/bin/time -v tail -n 5 big.log 2>&1 >/dev/null | grep 'Maximum resident'| command | peak RSS | ratio to file |
|---|---|---|
rtk read big.log |
7,818,312 KB (7.8 GB) | 3.91x |
rtk read big.log --tail-lines 5 |
4,338,312 KB (4.34 GB) | 2.17x |
rtk read big.log --max-lines 5 |
4,339,156 KB (4.34 GB) | 2.17x |
tail -n 5 big.log |
2,080 KB | 0.001x |
Plain read is the worst case at 3.9x — presumably input and compressed output are held simultaneously. Extrapolated to the 161 GB log this is ~630 GB of allocation; it died at 91 GB.
This appears to contradict the root-cause analysis in #2468, which states rtk streams and attributes the OOM to the shell holding the output in a variable (with rtk read F | wc -c marked as fine). The measurements above use no command substitution, so the 3.9x is inside rtk itself. I'll add this as a comment there too.
2. --tail-lines / --max-lines bound output, not memory
Both flags are documented as "Keep only last N lines" / "Max lines" — post-filters applied after the whole file is materialized. A bounded read is exactly the case where the caller has signalled the output is small; it is also the case where memory should be bounded, and currently isn't.
tail -n N seeks from EOF and reads backward in blocks at O(N). --tail-lines could do the same. Similarly --max-lines N can stop reading after N lines rather than reading to EOF first.
3. exclude_commands does not apply to the tail -n rewrite
The configuration docs state patterns match "against the full command after stripping env prefixes", that subcommand patterns work ("git push"), and that ^-prefixed patterns are regex. None of the four documented forms suppress this rewrite:
exclude_commands = [..., "tail"] -> tail -n 400 f.log => rtk read f.log --tail-lines 400
exclude_commands = [..., "^tail"] -> tail -n 400 f.log => rtk read f.log --tail-lines 400
exclude_commands = [..., "^tail .*"] -> tail -n 400 f.log => rtk read f.log --tail-lines 400
exclude_commands = [..., "^tail -n"] -> tail -n 400 f.log => rtk read f.log --tail-lines 400
Verified via echo '{"tool_name":"Bash","tool_input":{"command":"tail -n 400 /var/log/foo.log"}}' | rtk hook claude.
The mechanism itself works — "head" and "ps" in the same list are excluded correctly, and other tail forms (tail -f, tail -c N, /usr/bin/tail -n N) already pass through unrewritten. Only tail -n N <file> — the exact form that triggers the memory blowup — is unreachable from config.
cat has the same exposure: cat f.log | grep x is O(1) memory, rtk read f.log | grep x is O(filesize).
Impact
The hook silently converts a memory-safe command into a memory-unsafe one, with no signal to the caller, no documented size threshold, and no config escape. On any host with a large log this is an OOM waiting to happen.
Suggestions
- Stream
rtk read, or bail out to passthrough above a size threshold (a few hundred MB would have prevented this entirely). - Implement
--tail-linesas a backward seek, and make--max-linesstop reading early. - Honour
exclude_commandsin thetail/catrewrite paths. - Consider not rewriting
tail -n Nandhead -Nat all — they are already token-minimal, so the rewrite has little upside. head -N rewritten to whole-file compressed view folds structure without inline markers — false-positive "orphaned }" nearly triggered a wrong edit #3046 reaches the same conclusion from a correctness angle ("a bounded read is already token-cheap and should not be replaced by a whole-file summary").
Related: #2468 (savings accounting on failed big reads), #3046 (head -N folding), #2805.
Activity
pszymkowiak commentedon Jul 20, 2026
🔍 Automated Triage
bugcriticalSummary
rtk read materializes whole files in memory (3.9x filesize RSS) instead of streaming, tail-lines/max-lines don't bound memory, and exclude_commands can't suppress the tail -n rewrite — combined, this turned a safe bounded
tail -n 3000into a 49GB RSS OOM on a 161GB log with no config escape hatch.📁 Relevant files
src/commands/read.rssrc/hook/rewrite.rssrc/config.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
albatrossflyon-coder commentedon Jul 21, 2026
Hi — I'd like to work on this one. Real memory-safety issue: making
rtk readstream/bound its memory use regardless of file size, and making sure--tail-lines/--max-linesactually bound memory during the read, not just the final output. Will also look at whyexclude_commandsdoesn't stop thetail -nrewrite as part of the same fix.KuSh commentedon Oct 31, 2026
Re-checked on v1.1.0 (fresh
HOME,LC_ALL=C, a 200 MB file built with theyes | head -cline from the description, peak RSS per command):exclude_commands = ["tail"],rtk rewrite "tail -n 400 big.log"exits 1 and the command runs as typed. Without it, the rewrite is stillrtk read big.log --tail-lines 400.--head-linesstreams since v0.50.0:rtk read big.log --head-lines 5peaks at 11 MB, the same astail -n 5.So what is left is defects 1 and 2: bounded memory for plain
rtk read,--tail-linesand--max-lines. A title like "rtk readholds the whole file in memory for plain reads,--tail-linesand--max-lines" would match the remaining scope. #4012 covers the stdin side of the same buffering.swang commentedon Nov 28, 2026
Wish I knew this about
rtkas I got hit with this trying to tail 20 lines of a 20GB file only to have my entire laptop frozen due to this.No one should be installing this library when its doing something as silly as rewriting a tail call to read an entire file. To me thats's not the same command.