Skip to content

[platform-gap] Foreign children cannot receive exec-established numbered fds (no fork-style fd-table inheritance via std::process on Windows) #378

Closed

Description

@caomengxuan666

Found by the wt24/regfix lane while fixing #370 (verified broken identically at the good commit c70d109 — pre-existing platform limitation, not a regression).

Shape

A foreign child (e.g. sh -c '...', cmd /c ...) that itself opens or dups the fd number that an enclosing exec N>&M established inside rubash cannot receive that numbered descriptor: Windows std::process spawns children with only the three standard handles; there is no fork()-style descriptor table inheritance for arbitrary fd numbers.

Concretely: cases 02/05 of the #370 reproducer family (m370*.sh in the lane artifacts) differ from GNU in this shape and are byte-identical between the good commit and the fixed commit.

What already works (landed in #370's fix)

exec 3>&2; rubash-planned-command >&3 — redirects that rubash itself plans for the child resolve against the fd table's live bindings and land on the right stream (10-case fd matrix byte-identical to GNU, including v=$(exec 3>&1; cmd >&3) capture chains).

Direction

Any fix is a spawn-layer project (e.g. ProcThreadAttributeHandleList / manual handle duplication for declared fd numbers passed to CreateProcess), not an fd_table semantics change. GNU contract for reference: redir.c:1170 do_redirection_internal (exec N>&M = dup2, kept open across exec per redir.c:1188-1192), execute_cmd.c child inherits the full descriptor table.

Classify as platform-gap unless the spawn layer gains fd-passing; keep it out of the rubash semantic ledger until then.

Activity

  1. added a commit that references this issue on Oct 2, 2026
  2. added
    P3-low
    on Oct 9, 2026
  3. caomengxuan666 commented on Oct 12, 2026

    @caomengxuan666
    MemberAuthor

    Verdict after a full pass through the Windows spawn surface (docs + shipped CRT sources + a live probe): no sound fix path exists for foreign children. Recommend closing as platform-gap. The GNU contract (exec 3>&2 keeps fd 3 open across exec, so a child that itself does 1>&3 works — redir.c:1170/1188-1192) cannot be reproduced for arbitrary foreign children on Windows; the one mechanism that even partially works rides a field the OS documentation reserves for the C runtime, and it is dead on arrival for MSYS2/Cygwin children — the primary sh -c shape of this issue.

    1. The kernel layer offers only a filter, never a value

    So there is no supported way to make a chosen fd number exist in a child that was not written to look for it. That is the gap in one paragraph.

    2. Where "fd 3" actually lives: a CRT fiction, and the CRT has one private channel

    On Windows, fd numbers are CRT bookkeeping over HANDLEs. The UCRT builds fds only from the three std handles plus its reserved inherit block — verified in the UCRT sources shipped with the Windows SDK (C:\Program Files (x86)\Windows Kits\10\Source\10.0.26100.0\ucrt\lowio\ioinit.cpp):

    • initialize_stdio_handles_nolock (ioinit.cpp:123-149) creates fds 0/1/2 from GetStdHandle and nothing else;
    • initialize_inherited_file_handles_nolock (ioinit.cpp:59-118) is the only path that materializes higher fds: it parses GetStartupInfoW → lpReserved2 as int count; BYTE flags[count]; intptr_t handles[count] and fills __pioinfo[fh]->osfhnd/osfile from it.

    That layout is the ancient MSVCRT "io inherit block" — same format, still understood by msvcrt.dll (which is what System32\cmd.exe imports on Win10). Wine documents it in source comments as "Create an inheritance data block (for spawned process): 00 int nb of file descriptor; 04 char file flags; 4+NBFD HANDLE file handle" — msvcrt_create_io_inherit_block at dlls/msvcrt/file.c:653-697, child side msvcrt_init_io at file.c:703+ — https://github.com/wine-mirror/wine/blob/master/dlls/msvcrt/file.c

    But the field it rides is explicitly out of bounds for applications:

    3. Live probe on this machine (Windows 10 19045) — the gap and its exact boundary

    Parent = ctypes CreateProcessW, bInheritHandles=TRUE, STARTF_USESTDHANDLES, and a hand-built io-inherit block (flags[3]=0x01 FOPEN, handle = inheritable dup of a temp file) per the layout above (flags per SDK corecrt_internal_lowio.h:79-86). Child command uses its own 1>&3 internally — the issue's shape:

    child parent block result
    cmd /c echo cmd-via-fd3 1>&3 none (today) file empty — reproduces this issue
    cmd /c echo cmd-via-fd3 1>&3 io-inherit block file gets cmd-via-fd3 — cmd's own internal dup works
    sh -c 'echo sh-via-fd3 1>&3' (MSYS2, msys-2.0.dll) io-inherit block 3: Bad file descriptor, exit 1 — unreachable

    Two conclusions: (a) the CRT channel is real and would rescue only MSVCRT/UCRT-linked children (cmd /c); (b) MSYS2/Cygwin children can never be served by it, because their runtime claims the same reserved field for its own private, magic-checked child_info protocol — winsup/cygwin/fork.cc:341-342 sets si.lpReserved2 = (LPBYTE) &ch; si.cbReserved2 = sizeof (ch); — https://github.com/cygwin/cygwin/blob/main/winsup/cygwin/fork.cc. A foreign block in that slot is noise to msys-2.0.dll, and implementing Cygwin's private protocol per-DLL-version inside rubash is not a fix, it is a hostage situation.

    4. Why every other runtime lands in the same place

    rubash's spawn layer already says this out loud: numbered fds travel via the __RUBASH_FD_* env-key channel that rubash children honor and foreign children ignore by construction (src/executor/external_inner.rs, spawn_external_process — "Non-rubash children simply see one extra env var").

    5. Recommendation

    1. Close as platform-gap. GNU parity for foreign children needs the child's runtime to invent an fd 3, which no documented Windows mechanism does.
    2. Do not spend spawn-layer effort on the SUSPENDED+NtDuplicateObject/WriteProcessMemory route: it cannot dictate child handle values (values are system-assigned, §1), it creates no CRT fds in a foreign child, and it is the shape of process-injection tooling rather than process plumbing — Cygwin only gets away with it because both sides are its own DLL.
    3. If demand ever materializes for a partial bridge (cmd/UCRT-shaped children only, e.g. cmd /c in scripts), the io-inherit block in §2-§3 is the only known channel — and any such work must be labeled as depending on a field documented as "Reserved for use by the C Run-time", with the MSYS2 sh -c shape explicitly out of scope. Until then, the __RUBASH_FD_* cooperative channel plus this issue's ledger exclusion is the correct steady state.
  4. caomengxuan666 commented on Oct 13, 2026

    @caomengxuan666
    MemberAuthor

    Closing comment (auto-closed by PR #580 merge; adding the verdict here for the record).

    The limit's essence: Windows has no fork-style descriptor-table inheritance. std::process::Command (and CreateProcessW beneath it) passes exactly three std handles (STARTF_USESTDHANDLES); PROC_THREAD_ATTRIBUTE_HANDLE_LIST only filters already-inheritable handles and never assigns fd numbers; DuplicateHandle's target value is [out] and useless without child cooperation. A numbered fd established by an enclosing exec N>&M therefore cannot materialize in a foreign child (sh -c, cmd /c) that was not written to look for it — GNU's contract (redir.c:1170, kept open across exec at redir.c:1188-1192) rides fork semantics Windows does not have. The only value-carrying channel (lpReserved2 CRT io-inherit block) is documented "Reserved for use by the C Run-time" and is dead on arrival for MSYS2/Cygwin children, whose runtime claims the same field for its private child_info protocol (fork.cc:341-342). Every mainstream runtime lands in the same place (Cygwin cooperative fork, Python pass_fds POSIX-only, libuv private layout, Rust std with no mapping API).

    Durable record: docs/platform-fd-inheritance.md, merged via PR #580 — includes the OS/CRT analysis with file:line anchors, the live-probe boundary, and the supported-forms ledger.

    Already-supported shapes (kept out of the gap):

    Ruling basis: the 2026-10 verdict that no sound fix path exists for foreign children (a fix would require either a reserved-field hostage protocol or process-injection tooling, both incompatible with rubash's native-Windows, no-MSYS-runtime identity); plus owner instruction 2026-10-13 to clear all open issues — non-defect platform gaps are closed by documentation rather than left open. If demand ever materializes for a partial UCRT-only bridge, the candidate routes are recorded in the doc's closing section, without commitment.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3-low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions