Skip to content

fix(cat): deliver VEOF (^D) on the cooked console face - #1454

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/1450-cat-veof
Oct 30, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/1450-cat-veof

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem

On a Windows cooked console (real ConPTY interactive face), cat never ends on ^D (VEOF): the 0x04 keystroke enters the console line buffer as a plain character, ReadFile returns only on Enter, and an interactive cat swallows every subsequently typed line until ^C kills it (exit 0xC000013A = -1073741510).

Under GNU (termios line discipline), ^D with an empty canonical buffer ends the input stream, and ^D after pending characters flushes them to the reader without a newline. Windows cooked mode has no line discipline, so the behavior is reimplemented at the WinuxCmd input layer — the disposition already recorded in #1450.

Root cause

cat's two live-stdin arms read the console handle with plain ReadFile:

  • src/commands/cat.cpp fast-passthrough loop (hIn)
  • src/commands/cat.cpp transform loop (hLiveIn)

Fix

Shared input-layer helper file_io::read_stdin_record() (in src/utils/file_io.cppm, next to the other stdin primitives stdin_is_bad / set_stdin_binary), armed only when stdin is a console handle with ENABLE_LINE_INPUT; pipe/file input falls through to plain ReadFile unchanged. On the armed face it filters the records the console delivers:

  • 0x04 at line start (empty canonical buffer) → zero-byte read (EOF); the unread tail of that console record is re-injected into the console input queue via WriteConsoleInput so the next reader — the shell — receives it, the way the Unix tty queue retains input typed after EOF;
  • 0x04 mid-line → the pending prefix is delivered as its own record (no newline); the record continues;
  • every other byte passes through untouched (including the console's \r\n), so non-^D bytes are byte-identical to the old path.

cat's two live-stdin ReadFile sites now call the helper; the ReadFile contract (TRUE + got == 0 at EOF) is preserved, so the surrounding loops are unchanged apart from the call.

GNU anchor

termios VEOF semantics, verified live against WSL GNU bash 5.3.20 under the same ConPTY relay shape: cat + hello + ^D → cat exits 0, prompt returns (gnu-cat-rc=0).

Evidence (ConPTY battery, niubash session, matched baseline/fixed builds from this same tree)

scenario baseline (pristine main) this branch
cat + hello + ^D + second + ^C swallows second; dies only at ^C, cat-rc=-1073741510 cat ends at ^D; shell reports niu: second: command not found (queue pushback), GNU-parity
cat + hello + ^D → echo cat-rc=$? swallows echo/exit forever cat-rc=0
abc + ^D + def + Enter + ^D → cat of captured file — abcdef (prefix flushed, remainder continues — GNU read-sequence semantics)
pipe / file stdin (smoke + 38 gtest cat.* cases) — unchanged behavior; 37/38 pass, the 1 failure (nul_txt_reads_file_issue1333) reproduces identically on the pristine-main baseline binary (pre-existing, environment-bound)

Fixes #1450

Root cause: cat's live-stdin arms read the console handle with plain
ReadFile (src/commands/cat.cpp fast-passthrough and transform loops).
Windows cooked mode (ENABLE_LINE_INPUT) has no line discipline, so the
0x04 keystroke enters the console line buffer as a plain character,
ReadFile returns only on Enter, and an interactive cat never observes
EOF - it swallows every line typed afterwards until ^C (exit 0xC000013A).

Fix: shared input-layer helper file_io::read_stdin_record() (the
file_io stdin layer next to stdin_is_bad/set_stdin_binary), armed only
when stdin is a console handle with ENABLE_LINE_INPUT, filtering the
records ReadFile delivers:

- 0x04 at line start (empty canonical buffer) -> zero-byte read (EOF);
  the unread tail of that console record is re-injected into the console
  input queue via WriteConsoleInput so the next reader - the shell -
  receives it, the way the Unix tty queue does;
- 0x04 mid-line -> the pending prefix is delivered as its own record
  (no newline), the record continues;
- every other byte passes through, so pipe/file input (where the helper
  is not armed) and non-^D console bytes stay byte-identical.

cat's two live-stdin ReadFile sites now call the helper; the ReadFile
contract (TRUE + got==0 at EOF) is preserved.

GNU anchor: termios VEOF line discipline - ^D with an empty canonical
buffer ends the stream; ^D after pending characters flushes them to the
reader without a newline (verified against WSL GNU bash 5.3.20 under
the same ConPTY shape: cat + hello + ^D -> cat exits 0).

Known limits: mid-line flush bytes are handed over only when the next
Enter completes the console record (earlier release would need raw-mode
discipline emulation); std::cin-based whole-input readers and other
applet-private loops (head, dd) still need to adopt the helper;
if WriteConsoleInput fails the EOF tail is dropped.

ConPTY evidence (niubash session, matched baseline/fixed builds):
baseline keeps running after ^D, swallows 'second', dies only at ^C
(cat-rc=-1073741510); fixed prints 'hello' and exits 0 (cat-rc=0), and
the shell observes 'second: command not found' via the queue pushback.

Fixes #1450
@caomengxuan666
caomengxuan666 merged commit 2a3cefc into main Oct 30, 2026
8 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1450-cat-veof branch November 2, 2026 01:13
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.

ConPTY: 外部命令自身读控制台时 Ctrl+D 不产生 EOF(VEOF 需在 WinuxCmd 输入层模拟)

1 participant