Skip to content

fix(ext/node): support numeric FDs in child_process stdio array - #32959

Merged
bartlomieju merged 6 commits into
denoland:mainfrom
bartlomieju:fix/child-process-extra-stdio-fd
Mar 25, 2026
Merged

bartlomieju merged 6 commits into
denoland:mainfrom
bartlomieju:fix/child-process-extra-stdio-fd

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Mar 24, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Node.js allows passing raw file descriptors as numbers in the stdio array for child_process.spawn(), e.g. spawn('cmd', { stdio: ['ignore', 'pipe', 'pipe', fd] }). This previously failed with serde_v8 error: invalid type; expected: enum, got: Number
  • Refactor StdioOrRid → StdioOrFdOrRid with a new Fd(i32) variant for raw file descriptors
  • Numbers in extra_stdio now deserialize as Fd and are dup2'd to the target fd in the child process on Unix
  • as_stdio() also handles Fd by dup'ing it into a std::fs::File for use with stdin/stdout/stderr

Towards #32846

@bartlomieju
bartlomieju force-pushed the fix/child-process-extra-stdio-fd branch 2 times, most recently from 005309e to 5ccac4f Compare March 24, 2026 18:17
Node.js allows passing raw file descriptors as numbers in the `stdio`
array for `child_process.spawn()`, e.g.:
  spawn('cmd', { stdio: ['ignore', 'pipe', 'pipe', fd] })

Previously this failed with `serde_v8 error: invalid type; expected:
enum, got: Number` because `extra_stdio` was typed as `Vec<Stdio>`
which only accepts string variants.

Refactor `StdioOrRid` → `StdioOrFdOrRid` with a new `Fd(i32)` variant
for raw file descriptors. Numbers now deserialize as `Fd` and are
dup2'd to the target fd in the child process on Unix.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju
bartlomieju force-pushed the fix/child-process-extra-stdio-fd branch from 5ccac4f to b9fe3d9 Compare March 24, 2026 18:24
bartlomieju and others added 4 commits March 24, 2026 19:37
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Deno's fs.openSync returns a resource ID on Windows, not a CRT file
descriptor. Passing this to libc::get_osfhandle causes a crash
(STATUS_STACK_BUFFER_OVERRUN). Skip the test on Windows until Deno's
fs.openSync returns real CRT fds.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju

Copy link
Copy Markdown
Member Author

Review

The type refactor (StdioOrRid -> StdioOrFdOrRid with Fd(i32) variant) is clean. Deserializer mapping numbers to Fd, preserving Rid for programmatic construction (e.g. the Rid(1)/Rid(2) inherit mappings) is the right approach.

A few things to consider:

  1. Semantic change to stdin/stdout/stderr deserialization. Previously, numbers deserialized as Rid (resource IDs). Now they deserialize as Fd (raw file descriptors). This is correct for the Node compat path (where toDenoStdio returns raw FDs from fs.openSync), but it changes behavior for Deno.Command itself if anyone was passing a numeric resource ID for stdin/stdout/stderr. Worth verifying that the public Deno API only accepts string values there.

  2. Windows extra_stdio has a redundant match arm. Rid(_) and _ do the same thing -- the Rid arm can be folded into _.

  3. Unix Fd path doesn't close the original fd after dup2. In the extra_stdio Unix loop, fds_to_dup.push((raw_fd, fd)) passes the original fd for dup2, but doesn't add raw_fd to fds_to_close. The original fd will remain open in the child at both its original number and the target number. This differs from the Piped path which closes fd2 after dup. Might be intentional (caller owns it) but worth a note.

  4. _fd vs fd naming inconsistency between the Windows and Unix extra_stdio loops -- minor nit.

…ship

- Fold redundant Rid(_) match arm into _ on Windows
- Rename _fd to fd for consistency with Unix loop
- Document that the caller retains ownership of raw_fd (not added to
  fds_to_close) in the Unix Fd path
- Verified Deno.Command public API only accepts string stdio values,
  so the Fd deserialization change doesn't affect it

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju
bartlomieju enabled auto-merge (squash) March 25, 2026 07:41
@bartlomieju
bartlomieju merged commit ffbe236 into denoland:main Mar 25, 2026
111 checks passed
@bartlomieju
bartlomieju deleted the fix/child-process-extra-stdio-fd branch March 25, 2026 09:01
bartlomieju added a commit that referenced this pull request Mar 26, 2026
bartlomieju added a commit that referenced this pull request Mar 27, 2026
…ay (#32959)" (#33017)

## Summary

Reverts #32959 (commit ffbe236) which added support for numeric FDs in
`child_process` stdio array.

The reverted commit changed deserialization of numeric values in stdio
config from `Rid(ResourceId)` to `Fd(i32)`, meaning all numeric stdio
values are now interpreted as raw OS file descriptors instead of Deno
resource IDs. This is a breaking change because Deno's Node-compat
`fs.openSync` currently returns Deno resource IDs, not raw file
descriptors (unlike Node.js which returns raw FDs).

Any code that passes a numeric value from `fs.openSync` (or similar) as
a stdio option now calls `dup()` on a wrong/nonexistent OS file
descriptor instead of looking it up in Deno's resource table. This was
reported as breaking Claude Code installed via Deno -- it could not run
Bash at all.

#31965 needs to land first (making `fs.openSync` return raw FDs), and
then #32959 can be re-landed safely.
bartlomieju added a commit that referenced this pull request Apr 2, 2026
Now that `fs.openSync` returns real OS file descriptors (#33039), numeric
values in child_process stdio arrays should be treated as raw fds rather
than Deno resource IDs. This re-lands the functionality from #32959
(reverted in #33017) with the correct fd-based approach.

- Rename `StdioOrRid` to `StdioOrFd`, replace `Rid(ResourceId)` with `Fd(i32)`
- `as_stdio()` dups the fd directly (Unix: `libc::dup`, Windows: handle clone)
  instead of looking up the resource table
- `extra_stdio` now accepts `StdioOrFd` so numeric fds work beyond stdin/stdout/stderr
- Hardcoded `Rid(1)`/`Rid(2)` for inherit become `Fd(1)`/`Fd(2)`

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
bartlomieju added a commit that referenced this pull request Apr 3, 2026
## Summary

Now that `fs.openSync` returns real OS file descriptors (#33039),
numeric values in child_process stdio arrays should be treated as raw fds rather
than Deno resource IDs. This re-lands the functionality from #32959
(reverted in #33017) with the correct fd-based approach.

- Rename `StdioOrRid` to `StdioOrFd`, replace `Rid(ResourceId)` with
`Fd(i32)`
- `as_stdio()` dups the fd directly (`libc::dup` on Unix, handle clone
on Windows) instead of looking up the resource table
- `extra_stdio` now accepts `StdioOrFd` so numeric fds work beyond
stdin/stdout/stderr
- Hardcoded `Rid(1)`/`Rid(2)` for inherit become `Fd(1)`/`Fd(2)`
- Remove unused `FileResource` import

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
bartlomieju added a commit that referenced this pull request Apr 3, 2026
## Summary

Now that `fs.openSync` returns real OS file descriptors (#33039),
numeric values in child_process stdio arrays should be treated as raw fds rather
than Deno resource IDs. This re-lands the functionality from #32959
(reverted in #33017) with the correct fd-based approach.

- Rename `StdioOrRid` to `StdioOrFd`, replace `Rid(ResourceId)` with
`Fd(i32)`
- `as_stdio()` dups the fd directly (`libc::dup` on Unix, handle clone
on Windows) instead of looking up the resource table
- `extra_stdio` now accepts `StdioOrFd` so numeric fds work beyond
stdin/stdout/stderr
- Hardcoded `Rid(1)`/`Rid(2)` for inherit become `Fd(1)`/`Fd(2)`
- Remove unused `FileResource` import

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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