You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[platform-gap] Foreign children cannot receive exec-established numbered fds (no fork-style fd-table inheritance via std::process on Windows) #378
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.
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.
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:
STARTUPINFO.cbReserved2 — "Reserved for use by the C Run-time; must be zero."
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
Cygwin/MSYS2 can give fd>2 to children only when both sides are Cygwin: fork() uses CREATE_SUSPENDED (fork.cc:286), WriteProcessMemory into the child (child_copy, fork.cc:791), and the child reconstructs its fd table from Cygwin's own child_info blob — cooperative machinery that never applies to a foreign binary.
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
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.
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.
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.
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):
stdio redirections, pipelines, here-docs (fds 0/1/2 via std::process);
process-substitution /dev/fd/N operands, rewritten on Windows to real paths the child opens itself (dev_fd_operands.rs:285 materialize_dev_fd_operands);
rubash → rubash children via the cooperative __RUBASH_FD_* env-key channel (external_inner.rs:755), which foreign children ignore by construction.
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.
Activity
tests(#374): wt37 round-2 gap families ignored with tickets + residua…
caomengxuan666 commentedon Oct 12, 2026
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>&2keeps fd 3 open across exec, so a child that itself does1>&3works — 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 primarysh -cshape of this issue.1. The kernel layer offers only a filter, never a value
PROC_THREAD_ATTRIBUTE_HANDLE_LISTonly limits which already-inheritable handles are copied. Microsoft: an inherited handle "refers to the same object in the child process as it does in the parent process. It also has the same value and access privileges. … To use a handle, the child process must retrieve the handle value and 'know' the object to which it refers." — https://learn.microsoft.com/en-us/windows/win32/procthread/inheritanceSTARTF_USESTDHANDLES'shStdInput/hStdOutput/hStdError— exactly three, exactly fds 0/1/2. — https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoaDuplicateHandle'slpTargetHandleis[out]: the duplicate value is system-assigned, and the docs add that a cross-process duplicate is useless until "the source process must use interprocess communication to pass the value of the duplicate handle to the target process" — i.e. the child must cooperate. — https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandleSo 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 fromGetStdHandleand nothing else;initialize_inherited_file_handles_nolock(ioinit.cpp:59-118) is the only path that materializes higher fds: it parsesGetStartupInfoW→lpReserved2asint count; BYTE flags[count]; intptr_t handles[count]and fills__pioinfo[fh]->osfhnd/osfilefrom it.That layout is the ancient MSVCRT "io inherit block" — same format, still understood by
msvcrt.dll(which is whatSystem32\cmd.exeimports 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_blockat dlls/msvcrt/file.c:653-697, child sidemsvcrt_init_ioat file.c:703+ — https://github.com/wine-mirror/wine/blob/master/dlls/msvcrt/file.cBut the field it rides is explicitly out of bounds for applications:
STARTUPINFO.cbReserved2— "Reserved for use by the C Run-time; must be zero."STARTUPINFO.lpReserved2— "Reserved for use by the C Run-time; must be NULL." — https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa3. 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 SDKcorecrt_internal_lowio.h:79-86). Child command uses its own1>&3internally — the issue's shape:cmd /c echo cmd-via-fd3 1>&3cmd /c echo cmd-via-fd3 1>&3cmd-via-fd3— cmd's own internal dup workssh -c 'echo sh-via-fd3 1>&3'(MSYS2, msys-2.0.dll)3: Bad file descriptor, exit 1 — unreachableTwo 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-checkedchild_infoprotocol — winsup/cygwin/fork.cc:341-342 setssi.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
fork()usesCREATE_SUSPENDED(fork.cc:286),WriteProcessMemoryinto the child (child_copy, fork.cc:791), and the child reconstructs its fd table from Cygwin's ownchild_infoblob — cooperative machinery that never applies to a foreign binary.subprocesspass_fdsis marked "(POSIX only)"; on Windows you getclose_fds+ thehandle_listfilter + the three std handles, period. — https://docs.python.org/3/library/subprocess.htmlSTARTUPINFO.lpReserved2slot with a libuv-private layout — "int number_of_fds; unsigned char crt_flags[]; HANDLE os_handle[]" (src/win/process-stdio.c:29-33, count capped at 255 at :178; wired at src/win/process.c:1041-1042) — parsed only by a child that also contains libuv. — https://github.com/libuv/libuv/blob/v1.x/src/win/process-stdio.cstd::process::Commandon Windows has no API to map arbitrary handles to child fd numbers; the nightlyspawn_with_attributesexposes onlyProcThreadAttributeList(the §1 filter), notSTARTUPINFOreserved fields — so even the cmd-only CRT-channel bridge would require replacing the std spawn path with hand-rolledCreateProcessWFFI. — https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.htmlrubash'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
platform-gap. GNU parity for foreign children needs the child's runtime to invent an fd 3, which no documented Windows mechanism does.NtDuplicateObject/WriteProcessMemoryroute: 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.cmd /cin 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 MSYS2sh -cshape explicitly out of scope. Until then, the__RUBASH_FD_*cooperative channel plus this issue's ledger exclusion is the correct steady state.docs: platform gap — foreign children cannot receive exec-established…
caomengxuan666 commentedon Oct 13, 2026
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(andCreateProcessWbeneath it) passes exactly three std handles (STARTF_USESTDHANDLES);PROC_THREAD_ATTRIBUTE_HANDLE_LISTonly 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 enclosingexec N>&Mtherefore 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 (lpReserved2CRT 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 privatechild_infoprotocol (fork.cc:341-342). Every mainstream runtime lands in the same place (Cygwin cooperative fork, Pythonpass_fdsPOSIX-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):
std::process);exec 3>&2; cmd >&3resolves against the live fd table — regression(ba69f0d9): exec-copied fds (exec 3>&2) no longer honored by external command redirects (>&3 lands on wrong stream) #370 fix);/dev/fd/Noperands, rewritten on Windows to real paths the child opens itself (dev_fd_operands.rs:285 materialize_dev_fd_operands);__RUBASH_FD_*env-key channel (external_inner.rs:755), which foreign children ignore by construction.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.