Skip to content

WIP: fix(net-redir): loser fd-0 net opens settle in reject gate (#2098) + socket-aware flush for classified targets (#2102) - #2120

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2098-2102-net-redir
Oct 29, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2098-2102-net-redir

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: net 重定向批两单 — #2098 落选 fd-0 net open 结算 + #2102 已关闭对端写错误码。

#2098 — 非胜出 fd-0 net 重定向从不执行 open

GNU 按序执行每个重定向的 open(redir.c do_redirections),落选只是绑定被胜出者取代,open 照样发生(恰好 1 次连接),被拒的落选连接以两行诊断使命令失败。#1950 的胜出探针纯洁性设计让探针只验证不连接、只有胜出者绑定时连接,于是落选 net 条目在所有漏斗(spawn / 模拟 cat / 内建 / 管线级)都是 0 连接,被拒落选还静默放行。

修法(探针/绑定架构内):在所有漏斗恰好经过一次的公共门 reject_invalid_redirects 里结算落选者 open——fd-0 Input/ReadWrite net 条目若被后续候选取代(fd0_stdin_redirect_winner 同一谓词),经 open_net_redirect 连一次并丢弃描述符;连接失败在此条目处以已生效的 fd-2 绑定拒绝命令。胜出者不在门内 open(绑定仍是唯一连接)。spawn 漏斗的 body-胜出探针循环跳过 net 目标(其 ReadWrite 探针原本会连——落选连接现归门所有,防双连)。

GNU 5.3.20 对照(回环计数监听器):四种落选形状(Input/ReadWrite × heredoc/herestring)三个漏斗全部 0→1 连接且输出逐字节等;胜出对照恒 1;< net < net 连 2 次;被拒落选 rc=1 + 两行诊断、body 不输出。钉 tests/issue2098_devtcp_loser_open_regressions.rs(4 测)。

#2102 — /dev/tcp 对端已关闭时写报 EINVAL(GNU 为 EPIPE 族)

根因(Windows 面,pkg10 F 格发现):分类 open(create_redirect_output/open_append_target,redir.c netopen)把已连接 SOCKET 收养为目标 std::fs::File,而内建/cat 的刷写面走 std File::write_all = WriteFile——WriteFile 对 socket 句柄直接 ERROR_INVALID_PARAMETER。于是 Windows 上对 net 目标的每次写都 EINVAL(与对端死活无关),真错误永不浮现。(fd 表端点写面 #1976 已走 send、不在此列。)

修法:新增 crate::fd::write_file_all(&File, bytes),借用 File 的原始句柄走平台原语(Windows socket 走 send、Unix 同 write(2)),所有分类重定向目标的刷写面改走它:write_to_fd 与 sh_chkwrite 刷写(redirection.rs)、write_cat_output(external_finish.rs)、模拟 cat 的 sink 写(external_file_builtins.rs)。死对端从此报真错误(WSAECONNRESET → Connection reset by peer;Unix 为 EPIPE 族),经 sh_chkwrite(builtins/common.c)呈现。

SIGPIPE 面裁定(GNU 5.3.20 / Linux 实测):FIN 关闭后第一次写成功、第二次写的 EPIPE 使非交互 shell 被 SIGPIPE 杀死——rubash 本已一致,钉锁定;Windows 无 SIGPIPE,第二次写为 write-error 诊断 + 状态 1。钉 tests/issue2102_devtcp_closed_peer_write_regressions.rs(3 测,回环:活对端交付四面 + FIN/RST 错误族 + 永不出现 Invalid argument)。

验证

逐行关闭:
Fixes #2098
Fixes #2102

…ct gate (rubash#2098)

GNU applies every redirection's open in order (redir.c
do_redirections, redir.c:246): a superseded fd-0 `/dev/tcp` entry
still connects exactly once — only its binding is replaced by the
winner's — and a refused superseded connect fails the command with
redirection_error's two-line diagnostic. The #1950 winner-probe
purity design validated net targets without connecting and let
only the winner's bind connect, so a losing net entry produced
zero connections on every funnel, and a refused loser silently ran
the command on the winner's stdin.

Settle the loser's open in `reject_invalid_redirects` — the one
ordered walk every funnel (spawn, emulated, builtin, pipeline
stage, null command) passes exactly once: for an fd-0 Input/
ReadWrite net entry that a later candidate supersedes (the
fd0_stdin_redirect_winner predicate), connect once via
open_net_redirect and drop the descriptor; a connect failure
rejects the command at that entry through the fd-2 binding applied
so far. The winner is not opened here — its bind performs the one
connect. The spawn funnel's stdin-body probe loop now skips net
targets (its ReadWrite probe connected — the gate owns the loser's
single connect now).

Measured against GNU bash 5.3.20 with loopback connection-counting
listeners: the four documented loser shapes (Input/ReadWrite x
heredoc/herestring) now connect 1x on spawn/emulated/builtin
funnels with byte-equal output (was 0x), winner controls stay 1x,
`< net < net` connects 2x, a refused loser fails with status 1 and
both diagnostic lines. Pins: tests/issue2098_devtcp_loser_open_
regressions.rs (4 tests, hermetic loopback).

Fixes #2098
…rimitive (rubash#2102)

Writing to a closed `/dev/tcp` peer reported `write error:
Invalid argument` where GNU reports the broken-pipe family (found
on Windows by the pkg10 F cell). Root cause: the classified opens
(`create_redirect_output` / `open_append_target`, redir.c netopen
via rubash#1026) adopt the connected SOCKET as the target
std::fs::File, and the builtin/cat flush paths wrote it through
std's `File::write_all` — WriteFile on Windows, which rejects a
socket handle with ERROR_INVALID_PARAMETER outright. Every write
to a net target failed EINVAL on Windows, live peer or dead, and a
dead peer's genuine error never surfaced. (The fd-table endpoint
writes were already correct since #1976 routed them through send.)

New `crate::fd::write_file_all(&File, bytes)` borrows the File's
raw handle into the platform primitive — send for sockets on
Windows, the same write(2) on Unix — and every flush of a
classified redirect target now uses it: OutputFdState::write_to_fd
and the sh_chkwrite flush in redirection.rs, write_cat_output in
external_finish.rs, and the emulated-cat sink writes in
external_file_builtins.rs. A dead peer now surfaces its real error
(WSAECONNRESET => "Connection reset by peer" via the posix_errors
table; the EPIPE family on Unix) through sh_chkwrite
(builtins/common.c sh_chkwrite).

SIGPIPE face adjudicated against GNU bash 5.3.20 on Linux: after a
FIN close the first write succeeds and the second write's EPIPE
kills the non-interactive shell by SIGPIPE — rubash already
matches; the pins lock it in. On Windows (no SIGPIPE) the second
write is the write-error diagnostic with status 1.

Pins: tests/issue2102_devtcp_closed_peer_write_regressions.rs
(3 tests, hermetic loopback: live-peer delivery through the
direct/append/cat/exec flush faces, FIN-closed and RST-closed
error families, never "Invalid argument").

Fixes #2102
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 29, 2026 14:21
@caomengxuan666
caomengxuan666 merged commit b1468ab into master Oct 29, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2098-2102-net-redir branch October 30, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant