Skip to content

fix(cat): open the redirect target once in the streaming cat paths - #447

Merged
caomengxuan666 merged 1 commit into
unixwin:masterfrom
bbvdf:fix/cat-redirect-stream
Oct 9, 2026
Merged

caomengxuan666 merged 1 commit into
unixwin:masterfrom
bbvdf:fix/cat-redirect-stream

Conversation

@bbvdf

@bbvdf bbvdf commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Symptom

cat > file fed from a parent process pipe (inherited stdin) kept only the last ~4 KiB block of the stream — rc 0, stderr empty, silent truncation:

stdin bytes bytes on disk after cat > out
4 110 4 110 ✓
8 223 31 ✗
16 410 4 122 ✗
65 554 4 114 ✗
262 146 4 098 ✗
82 665 745 ✗

cat >> file and cat < file > file were unaffected.

Root cause

Executor::write_cat_output (src/executor/external_finish.rs) re-creates the > target on every call (create_redirect_output → File::create, i.e. truncate). The chunked cat paths — stream_inherited_cat and stream_cat_stdin_operand (src/executor/external_file_builtins.rs) — call it once per 8 KiB block, so each block truncated the previous one. GNU cat.c opens the output once. The one-shot paths (cat < file > out, formatted cat -n > out) call the writer once, which is why they looked fine.

Fix

  • Executor::open_cat_output_sink — opens the >/>> target once for the whole copy; fd aliases (/dev/stdout, /dev/fd/N) still resolve through the fd table per chunk, and the no-redirect stdout path is unchanged.
  • Both streaming loops write every chunk to that single handle.
  • tests/cat_redirect_stream_regressions.rs — chunked-stdin > keeps every byte; >> and <file > stay whole.

Verification

Cloud-built on windows-latest: niubash checked out with this branch as its [patch] ../rubash path dependency, same Python driver, only the engine revision differs — run 37875117138:

binary: D:\a\...\target\release\niu.exe True
on-disk bytes: 82665 expected: 82665
cat >> app.txt               -> 82665 bytes
cat < in.txt > fromfile.txt  -> 82665 bytes

The same workflow's second job rebuilds niu.exe against upstream master and shows the tail-fragment baseline.

Impact

Found through niubash#198: cat > "$tmp" is exactly how Hermes Agent's write_file/patch persist a file (payload on stdin), so every write above ~4 KiB kept only its tail and silently corrupted files (a config.py was destroyed this way, restored from git). Any parent process feeding niu -c "cat > f" was affected.

`write_cat_output` re-created (`File::create`) the `>` target on every call,
and the chunked paths (`stream_inherited_cat`, `stream_cat_stdin_operand`)
called it once per 8 KiB block — so `cat > out` with an inherited pipe kept
only the last block (745 of 82665 bytes, rc 0, stderr empty). GNU cat.c opens
the output once; `cat >> out` and `cat < file > out` were unaffected because
they append or write once.

Adds `Executor::open_cat_output_sink` (single open for the whole copy;
`/dev/*` fd aliases still resolve through the fd table per chunk) and uses it
in both streaming loops.

Regression test: tests/cat_redirect_stream_regressions.rs (niubash#198).
@bbvdf

bbvdf commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Baseline leg — same workflow, second job, upstream master (no fix), identical driver:

baseline on-disk bytes: 745 expected(bug): a tail fragment, not 82665

So the before/after pair is 745 → 82665 with only the engine revision differing. Fixed build's control legs: cat >> app.txt -> 82665, cat < in.txt > fromfile.txt -> 82665. Run: https://github.com/bbvdf/niubash/actions/runs/37876009583

(The PR's own CI checks are pending maintainer approval — GitHub holds workflow runs from a first-time fork contributor until someone approves them.)

@arookieofc

Copy link
Copy Markdown
Member

只是cat有这个情况吗?tail,head不会吗?能否验证一下,如果他们两个也有,那么或许可以一起修复一下

@caomengxuan666 caomengxuan666 added the P1-high 高:核心语义或高频路径受损,影响日常使用/发布 label Oct 9, 2026
@caomengxuan666

Copy link
Copy Markdown
Member

维护者复核完毕,质量到位,已 approve:

  • 诊断正确:write_cat_output 每块重建(截断)目标,只有两条分块 stdin 路径中招(745/82665 的基线对照扎实)。
  • 回复 @a-rookie-of-C-language 的 tail/head 疑问:不受影响。tail/head(pipeline_exec.rs 的内联 arm)与 cat 其余 write_cat_output 调用点全部是"先全量缓冲、单次写",不存在逐块截断类;write_cat_output 的分块调用只有本 PR 修的两处。
  • 一个非阻塞补充(已记为后续):open_cat_output_sink 对 fd 别名(> /dev/stdout、/dev/fd/N)有意走旧的逐块路径,这条分支目前没有回归测试锁定,建议后续补一条,防止将来有人把 fd 判断改坏。
  • CI:fork 首次贡献的 workflow 已由维护者批准运行。

先合并,fd 别名测试我们自己补(跟踪在 #198)。感谢 @bbvdf 的干净修复!

@caomengxuan666
caomengxuan666 merged commit b85811b into unixwin:master Oct 9, 2026
caomengxuan666 added a commit that referenced this pull request Oct 9, 2026
…d, 5 new engine bugs filed (#460 #461 #462 #463 #465), #435/#436/#425 confirmed fixed on fresh master

Findings doc + machine-readable triage JSON (139 hashes / 172 rows).

Also includes whitespace-only 'cargo fmt' normalization of
src/executor/external_finish.rs and tests/cat_redirect_stream_regressions.rs:
those files landed on master in b85811b (#447) under a different rustfmt and
fail the repo pre-commit hook on any commit (main worktree @a157656d fails
'cargo fmt --check' identically). No semantic changes.

Lane: wt98b/goldtri (triage only; no product changes). GNU-side evidence is
the recorded harvest oracle (WSL bash 5.3.0, 2026-10-04/05) — live WSL down
machine-side during this lane (WslService stopped, no admin); noted in the
doc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1-high 高:核心语义或高频路径受损,影响日常使用/发布

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants