Skip to content

rtk read consumes the whole input before windowing: unbounded streams never terminate, peak RSS scales with file size #4012

Open

Description

@KuSh

Summary

rtk read consumes its entire input before applying a line window. Every head/tail hook rewrite routes through it, so a bounded request against an unbounded or very large input behaves quite differently from the native command: head -2 /dev/urandom returns instantly, the rewritten form never returns.

What is already fixed

The non-UTF-8 content loss described in the original version of this issue is fixed on develop by #3941 (merged as cfe248c1). read::run now reads bytes and applies the window on byte offsets when filtering and line numbering are off. Verified against the develop binary:

$ printf '\xff\xfe bad\nline2\nline3\n' > bin.log
$ head -2 bin.log                       → 13 bytes, rc=0
$ rtk read bin.log --head-lines 2       → 13 bytes, rc=0   byte-identical

That holds for head -N, head -n N, head --lines[=]N, bare head, and the tail equivalents. This issue is narrowed to what that change deliberately left open.

1. Unbounded streams never terminate

$ head -2 /dev/urandom                  # returns instantly
$ rtk read /dev/urandom --head-lines 2  # no output, killed at 5s

head -2 /dev/urandom is rewritten today. read/read_to_end has no line budget, so it reads until an EOF that never arrives.

Note for whoever picks this up: a FIFO whose writer closes is not affected — rtk read fifo --head-lines 1 completes normally, because EOF does arrive. The failure is specific to inputs with no EOF (character devices, yes |, a FIFO held open by a live writer), not to non-regular files as a category. The original title said "FIFOs"; that was too broad.

2. Peak memory is proportional to input size, not window size

On a 143 MB file, head -n 5, measured against develop @ cfe248c1:

wall peak RSS
native head -n 5 0.00 s 11 MB
rtk read --head-lines 5 0.08 s 151 MB

#3941 improved this (the byte path removed a String round-trip, so the same case was 293 MB before it), but the whole file is still resident to emit five lines.

Suggested direction

Read incrementally and stop once the window is satisfied. head_window already scans forward for the Nth \n, so a buffered reader that stops there closes both (1) and (2) for the --head-lines case. --tail-lines needs the tail kept in a ring buffer rather than the whole input. Falling back to the native binary when the input is not a regular file would close (1) alone.

Related

Environment

  • develop @ cfe248c1, Linux, LC_ALL=C

Activity

  1. added
    bug
    help wanted
    on Sep 13, 2026
  2. rtk-wshm-sync-bot commented on Sep 13, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 94%

    Summary

    rtk read's use of fs::read_to_string breaks non-UTF-8 input, hangs on FIFOs/char devices, and wastes memory on large files — should read incrementally as bytes with an early-stop window

    📁 Relevant files
    • src/cmds/system/read.rs

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

  3. changed the title [-]rtk read loses all content on non-UTF-8 files and never terminates on FIFOs/character devices — every head/tail rewrite routes through it[/-] [+]rtk read consumes the whole input before windowing: unbounded streams never terminate, peak RSS scales with file size[/+] on Sep 13, 2026
  4. taljeon commented on Sep 14, 2026

    @taljeon

    I’m taking this. I’ll keep the existing full-read path for filtering and line-number modes, and make the byte-preserving plain-window path incremental: stop --head-lines at the requested newline without waiting for EOF, and retain only a bounded tail window while reading to EOF.

    The regression coverage will exercise files and stdin, binary/CRLF/unterminated input, zero and oversized windows, a held-open producer that proves head returns before EOF, and bounded-reader behavior rather than relying on timing alone. I’ll also keep tracking from materializing the discarded input again.

  5. KuSh commented on Oct 29, 2026

    @KuSh
    CollaboratorAuthor

    Where this stands on develop at 7e92da0, each command given 5 seconds:

    command develop
    rtk read /dev/urandom --head-lines 3 returns
    yes | rtk read - --head-lines 3 killed at 5 s
    rtk read /dev/urandom -n --head-lines 3 killed at 5 s
    yes | rtk read - -n --head-lines 3 killed at 5 s

    The file case returns since #4122, and #4153 makes the stdin case return as well. The --line-numbers cases still read to the end with #4153: -n takes the filter path, which reads the whole input, although a numbered head window needs only its first N lines. The hook never adds -n, so these are reached only when typed.

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:performancebughelp wantedpriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions