Skip to content

fix(cat,head): stream optioned cat per-line; short-read head -n on pipe/console stdin - #1289

Merged
caomengxuan666 merged 1 commit into
unixwin:mainfrom
Catapult291:fix/cat-optioned-head-stream-short-read
Oct 24, 2026
Merged

caomengxuan666 merged 1 commit into
unixwin:mainfrom
Catapult291:fix/cat-optioned-head-stream-short-read

Conversation

@Catapult291

Copy link
Copy Markdown
Collaborator

Summary

After #1286 fixed bare cat streaming (short ReadFile in fast_passthrough), two related pipeline-tail shapes still withheld output until EOF on a dribbling producer:

  1. Optioned cat (-n / -A / -b / -e / -T / -v / -s): the option path already read via istream::get (streamable), but out was only flushed at 64KiB or EOF — so python lines.py | cat -n printed nothing until the producer exited.
  2. head -n N: output_first_records used istream::read(64KiB). MSVC xsgetn fills the request or EOF, so head -n 1 waited for the whole stream before emitting the first line.

Shell-independent repro (python parent + this cat.exe/head.exe, 1 line / 0.4s): both were BUFFER (~last-line time) before, STREAM (~0.03s) after; Git Bash streams the same shapes.

Changes

  • cat (optioned path): when stdin is std::cin and the handle type is FILE_TYPE_PIPE / FILE_TYPE_CHAR, use a single short ReadFile and feed the state machine; flush after each completed line (append_output still coalesces mid-line up to 64KiB). Disk ifstream keeps the filled loop.
  • head: output_first_records uses a single short ReadFile on live pipe/console stdin and returns as soon as N delimiters have been emitted (remaining bytes are discarded, matching GNU head). File/stdin-not-pipe paths unchanged. head -c and all-but-last semantics unchanged.

Pairs with the merged #1286 (bare cat) and rubash pipeline streaming (unixwin/rubash#1445). Follow-up to niubash defect log D4; this is R2 in niubash-remaining-defects-2026-10-24.

Test plan (Windows 11, WinuxCmd 1.1.20 + this patch, installed and build-tree binaries)

Probe Before After
python lines.py | cat (D4 regression) STREAM STREAM
python lines.py | cat -n / -A / -e / -b / -s BUFFER ~EOF STREAM ~0.03s
python lines.py | head -n 1 / -n 2 BUFFER STREAM
python lines.py | head -c 3 STREAM STREAM
cat -n / -b / -A / -e, head -n 2 / -c 2 byte-exact ok ok
yes | head -3 → PIPESTATUS 141 0 ok ok (D10/SIGPIPE)
niu -c 'python … | cat -n' / | head -n 1 BUFFER STREAM

Note: default head (10 lines) with a short producer now emits lines as they arrive and only closes at EOF — same as GNU. head -n +N keeps WinuxCmd's existing "first N lines" semantics (covered by tests/unit/head).

cherry-picked from local 14ed026.

- cat: re-apply D4 short ReadFile on live stdin for optioned path;
  flush after each completed line so cat -n/-A/-b/... stream with
  a dribbling producer (was withheld until 64KiB or EOF).
- head: output_first_records short-ReadFile live pipe/console stdin
  so head -n N emits as soon as N delimiters arrive (xsgetn filled
  a 64KiB request before returning).
- Disk/ifstream paths keep the fill loop; head -c / all-but-last
  semantics unchanged.
@caomengxuan666
caomengxuan666 merged commit 7d9f9ac into unixwin:main Oct 24, 2026
8 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member

Following up your own #1286 with the optioned path and head -n — that's the complete streaming story, not a patch series that stops at the demo case. The 64KiB flush detail is a nice find: get was already streamable, only the flush cadence lied. Merged. Three for three today — the pipeline tail finally keeps up with the producer.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants