Skip to content

rtk read buffers whole file (3.9x filesize RSS); --tail-lines/--max-lines bound output but not memory; exclude_commands cannot disable the tail -n rewrite #3107

Open

Description

@Cybercommanders

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

  1. Stream rtk read, or bail out to passthrough above a size threshold (a few hundred MB would have prevented this entirely).
  2. Implement --tail-lines as a backward seek, and make --max-lines stop reading early.
  3. Honour exclude_commands in the tail/cat rewrite paths.
  4. Consider not rewriting tail -n N and head -N at 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

  1. added
    bug
    help wanted
    on Jul 20, 2026
  2. pszymkowiak commented on Jul 20, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🔴 Priority critical
    🎯 Confidence 94%

    Summary

    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 3000 into a 49GB RSS OOM on a 161GB log with no config escape hatch.

    📁 Relevant files
    • src/commands/read.rs
    • src/hook/rewrite.rs
    • src/config.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  3. albatrossflyon-coder commented on Jul 21, 2026

    @albatrossflyon-coder
    Contributor

    Hi — I'd like to work on this one. Real memory-safety issue: making rtk read stream/bound its memory use regardless of file size, and making sure --tail-lines/--max-lines actually bound memory during the read, not just the final output. Will also look at why exclude_commands doesn't stop the tail -n rewrite as part of the same fix.

  4. KuSh commented on Oct 31, 2026

    @KuSh
    Collaborator

    Re-checked on v1.1.0 (fresh HOME, LC_ALL=C, a 200 MB file built with the yes | head -c line from the description, peak RSS per command):

    • Defect 3 is fixed since v0.47.0 (fix(hooks): honour exclude_commands for head and tail #3324): with exclude_commands = ["tail"], rtk rewrite "tail -n 400 big.log" exits 1 and the command runs as typed. Without it, the rewrite is still rtk read big.log --tail-lines 400.
    • --head-lines streams since v0.50.0: rtk read big.log --head-lines 5 peaks at 11 MB, the same as tail -n 5.
    • Still reading the whole file:
    rtk read big.log                  772 MB
    rtk read big.log --tail-lines 5   200 MB
    rtk read big.log --max-lines 5    432 MB
    tail -n 5 big.log                  11 MB
    

    So what is left is defects 1 and 2: bounded memory for plain rtk read, --tail-lines and --max-lines. A title like "rtk read holds the whole file in memory for plain reads, --tail-lines and --max-lines" would match the remaining scope. #4012 covers the stdin side of the same buffering.

  5. swang commented on Nov 28, 2026

    @swang

    Wish I knew this about rtk as 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:cliarea:performancebughelp wantedpriority:critical

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions