Skip to content

fix: readers bridge the winux fifo marker (Fixes #1493) - #1502

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/1493-fifo-marker-read-path
Nov 2, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/1493-fifo-marker-read-path

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: fix(fifo): reader-applet operand opens bridge the !<fifo> marker (Refs #1493, Refs rubash#2473; targeting 1.2.1)

Root cause

Since #1038 the shared open layer (src/utils/file_io.cppm) bridges an
on-disk !<fifo> marker file (src/utils/native_path.cppm:1082 kFifoMarker, is_winux_fifo_w) to a deterministic
\\.\pipe\winuxcmd-fifo-<hash> endpoint with POSIX blocking open()
semantics:

  • reader: file_io.cppm:134 open_fifo_read_handle — CreateNamedPipeW
    • ConnectNamedPipe, blocks until a writer arrives;
  • writer: file_io.cppm:149 open_fifo_write_handle — CreateFileW
    retry loop, blocks until a reader creates the endpoint.

The write side and the shared readers
(open_binary_file/create_binary_file/read_all_file) all consult
is_winux_fifo_w — but a family of reader applets opened operands with
a raw std::ifstream over normalize_api_operand(), bypassing the
bridge entirely and reading the 7 marker bytes as file content. That is
the asymmetry #1493 reports: od -c p printed !<fifo> literally
while echo hi > p correctly blocked in the write-bridge rendezvous.

The asymmetry points fixed (all now via file_io::open_binary_file)

File Site
src/commands/od.cpp read_file_bytes (was :582 raw ifstream)
src/commands/hexdump.cpp data operand (was :1094)
src/commands/paste.cpp operand read (was :249)
src/commands/comm.cpp operand read (was :230)
src/commands/nl.cpp operand read (was :514)
src/commands/md5sum.cpp / sha1sum.cpp / sha256sum.cpp / sha512sum.cpp file hashing (were :237/:244/:243/:243)
src/commands/cksum.cpp read_crc_file (the main CRC operand path, was :435) + --check list file

Two sibling read-path bugs the same probe surfaced

  1. src/utils/file_io.cppm read_handle_to_string: a bridged fifo
    handle ends with ReadFile failing ERROR_BROKEN_PIPE /
    ERROR_NO_DATA / ERROR_HANDLE_EOF once the writer closes — that is
    clean EOF (GNU read(2) returns 0), not an error. The accumulated
    bytes were being discarded (uniq p → error reading 'p'). The
    fix mirrors the rule read_all_stdin already documents for fd 0.
  2. src/commands/wc.cpp: GNU takes the st_size shortcut only for
    regular files (wc.c S_ISREG); a fifo marker IS a regular file on
    NTFS, so wc -c p reported the 7 marker bytes (7 p instead of
    3 p), and the column-width total loop counted markers as regular
    sizes. Markers now fall through to the streaming count / the
    non-regular width floor.

GNU oracle (WSL GNU bash, script-file harness, byte-for-byte)

mkfifo + background writer × od -c / od -An -tx1 / hexdump -c /
wc [-c] / cat / nl / paste (two fifos) / comm (two fifos) /
uniq / md5sum / sha1sum / sha256sum / sha512sum / cksum /
128 KiB payload — 16/16 outputs byte-identical to WSL GNU
(including the rubash#2473 reproducer shape paste <(echo 1) <(echo x)
→ 1\tx).

Artifacts: /d/tmp/wc1493/oracle.sh / oracle-full.out (GNU side) vs
probe2.sh / probe2.out (WinuxCmd side), run in this session.

Boundary behavior (GNU semantics kept)

  • A reader on a writer-less fifo blocks (open(O_RDONLY) blocks in
    GNU) and is cancellable via Ctrl-C or external termination — pinned
    by mkfifo.reader_without_writer_blocks_like_gnu.
  • test -p / stat -c %F (fifo) / find -type p / ls -l (p
    type bit) keep identifying markers as FIFOs (pre-existing, verified
    not regressed). ls -l's size column still shows the marker's 7
    where GNU shows 0 — pre-existing cosmetic difference, left as is.
  • Regular-file reads are untouched: read_handle_to_string's new
    broken-pipe cases cannot occur on regular files, and wc's marker
    check is attributes-gated (FILE_ATTRIBUTE_SYSTEM + first-7-byte
    probe) inside is_winux_fifo_w.

Tests

11 new unit tests in tests/unit/mkfifo/mkfifo_unit_test.cpp (od, wc
-c, wc full, md5sum, cksum, paste×2-fifos, comm×2-fifos, nl, hexdump,
uniq broken-pipe-EOF, blocking-without-writer), each pinning GNU
byte-exact output with a background tee writer and a 10 s
rendezvous-deadline guard so a regression fails instead of hanging the
suite.

  • Affected suites (od.*:wc.*:cksum.*:md5sum.*:sha*sum.*:comm.*:paste.*:nl.*:hexdump.*:uniq.*:mkfifo.*:cat.*:tee.*:sort.*:head.*:tail.*):
    528/529 pass; the 1 failure (cat.nul_txt_reads_file_issue1333)
    is proven pre-existing on clean main (stash-verified) and is part of
    the known environment-bound NUL.txt device-name family.
  • Full ctest: 3648/3667 pass; the 19 failures are, by name, the
    same environment-bound set that fails on clean main in this sandbox
    (nul_txt ×4, shell_root/issue1364/issue1385 ×13, nohup ×2) —
    and main additionally fails md5sum/cksum.issue_1288_ignore_missing…
    there, which pass with this PR.

Deliberately out of scope (follow-ups, not silently changed)

  • grep/sed engine operand opens: their BOM/binary fast paths
    (should_stream_file_fast_path) consume stream bytes and need their
    own pipe audit before bridging.
  • less/more pagers, column/rev/strings/expand/unexpand/
    fold/fmt/csplit/split/d2u and config-file readers
    (du excludes, dircolors, hostname -F, ldd): same raw-ifstream
    shape, lower FIFO likelihood; left for a follow-up sweep.

Docs

DOCS/gnu-coreutils/mkfifo.md still described FIFOs as unsupported;
rewritten to describe the #1038 marker + bridge design and the #1493
reader-side unification.

Refs #1493, Refs unixwin/rubash#2473

…e (Refs #1493)

Root cause: since #1038 the shared open layer bridges an on-disk
`!<fifo>` marker file to a \\.\pipe\winuxcmd-fifo-<hash> endpoint with
POSIX blocking open() semantics. The write side (file_io.cppm
create_binary_file) and the shared readers (open_binary_file /
read_all_file) all consult is_winux_fifo_w, but a family of reader
applets opened operands with a raw std::ifstream over
normalize_api_operand(), bypassing the bridge: od -c p read the 7
marker bytes as content instead of blocking on the rendezvous (#1493),
same for paste/wc/comm/diff as rubash procsub consumers (rubash#2473;
wc and diff were already bridged on main).

Reader opens now go through file_io::open_binary_file (the same
predicate and rendezvous mechanism the write side uses):
- od.cpp read_file_bytes; hexdump.cpp data operand
- paste.cpp / comm.cpp / nl.cpp operand reads
- md5sum / sha1sum / sha256sum / sha512sum file hashing
- cksum.cpp read_crc_file (main CRC operand path) + --check list file

Two sibling read-path bugs surfaced by the same probe:
- file_io.cppm read_handle_to_string: a bridged fifo handle ends with
  ReadFile failing ERROR_BROKEN_PIPE / ERROR_NO_DATA /
  ERROR_HANDLE_EOF once the writer closes; that is clean EOF (GNU
  read(2) returns 0), not an error -- the accumulated bytes were
  discarded (uniq p failed with "error reading 'p'"). Mirrors the rule
  read_all_stdin already applies to fd 0.
- wc.cpp: GNU takes the st_size shortcut only for regular files
  (wc.c S_ISREG); a fifo marker IS a regular file on NTFS, so
  `wc -c p` reported the 7 marker bytes and the width-total loop
  counted markers as regular sizes. Markers now fall through to the
  streaming count / non-regular width floor.

GNU oracle (WSL bash, script-file harness, byte-for-byte): mkfifo +
background writer x od -c / od -An -tx1 / hexdump -c / wc [-c] / cat /
nl / paste (two fifos) / comm (two fifos) / uniq / md5sum / sha1sum /
sha256sum / sha512sum / cksum / 128 KiB payload -- all byte-identical.
11 new mkfifo rendezvous unit tests pin these (incl. the rubash#2473
paste-of-two-fifos shape and GNU blocking-without-writer semantics,
cancellable by Ctrl-C / external termination).

Out of scope, listed for follow-up (NOT silently changed): grep/sed
engine opens (their BOM/binary fast paths consume stream bytes and need
their own pipe audit), less/more pagers, and the remaining config-file
readers (column/rev/strings/expand/unexpand/fold/fmt/csplit/split/d2u/
du excludes/dircolors/hostname/ldd).

Refs #1493, Refs rubash#2473
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 01:31
@caomengxuan666 caomengxuan666 changed the title WIP: fix(fifo): reader-applet operand opens bridge the !<fifo> marker (Refs #1493, rubash#2473; 1.2.1) fix: readers bridge the winux fifo marker (Fixes #1493) Nov 2, 2026
@caomengxuan666
caomengxuan666 merged commit b010ea4 into main Nov 2, 2026
13 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1493-fifo-marker-read-path branch November 3, 2026 09:03
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.

1 participant