Repository navigation
Description
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
- fix(read): make head/tail rewrites faithful to the native commands #3941 — fixed the content-loss half; merged.
- Inflated savings metric, unreachable truncation log on Windows, and passthrough that doesn't pass through #3339 — savings accounting on these windows, including the
from_utf8_lossyinflation that shares this code path.
Environment
develop@cfe248c1, Linux,LC_ALL=C
Activity
rtk-wshm-sync-bot commentedon Sep 13, 2026
🔍 Automated Triage
bughighSummary
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.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
[-]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[/+]taljeon commentedon Sep 14, 2026
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-linesat 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
headreturns before EOF, and bounded-reader behavior rather than relying on timing alone. I’ll also keep tracking from materializing the discarded input again.KuSh commentedon Oct 29, 2026
Where this stands on develop at 7e92da0, each command given 5 seconds:
rtk read /dev/urandom --head-lines 3yes | rtk read - --head-lines 3rtk read /dev/urandom -n --head-lines 3yes | rtk read - -n --head-lines 3The file case returns since #4122, and #4153 makes the stdin case return as well. The
--line-numberscases still read to the end with #4153:-ntakes 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.