Io.Threaded.spawnPosix: implement passing file descriptors as stdio (#31379)

`std.process.spawn`: remove the TODO for nonblocking file stdio and document the behavior.

Fix a bug in Io.Uring.dup2 where the function does not return on success

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31379
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: breakmit <breakmit@noreply.codeberg.org>
Co-committed-by: breakmit <breakmit@noreply.codeberg.org>
This commit is contained in:
breakmit 2026-03-06 04:51:28 +01:00 committed by Andrew Kelley
parent 30c8a75997
commit 46658257f4
4 changed files with 10 additions and 10 deletions

View file

@ -4404,10 +4404,7 @@ fn setUpChildIo(
.close => closeFd(std_fileno), .close => closeFd(std_fileno),
.inherit => {}, .inherit => {},
.ignore => try ev.dup2(dev_null_fd, std_fileno), .ignore => try ev.dup2(dev_null_fd, std_fileno),
.file => |file| { .file => |file| try ev.dup2(file.handle, std_fileno),
if (file.flags.nonblocking) @panic("TODO implement setUpChildIo when nonblocking file is used");
try ev.dup2(file.handle, std_fileno);
},
} }
} }

View file

@ -15659,7 +15659,7 @@ fn setUpChildIo(stdio: process.SpawnOptions.StdIo, pipe_fd: i32, std_fileno: i32
.close => closeFd(std_fileno), .close => closeFd(std_fileno),
.inherit => {}, .inherit => {},
.ignore => try dup2(dev_null_fd, std_fileno), .ignore => try dup2(dev_null_fd, std_fileno),
.file => @panic("TODO implement setUpChildIo when file is used"), .file => |file| try dup2(file.handle, std_fileno),
} }
} }

View file

@ -4550,10 +4550,7 @@ fn setUpChildIo(
.close => _ = linux.close(std_fileno), .close => _ = linux.close(std_fileno),
.inherit => {}, .inherit => {},
.ignore => try dup2(sync, dev_null_fd, std_fileno), .ignore => try dup2(sync, dev_null_fd, std_fileno),
.file => |file| { .file => |file| try dup2(sync, file.handle, std_fileno),
if (file.flags.nonblocking) @panic("TODO implement setUpChildIo when nonblocking file is used");
try dup2(sync, file.handle, std_fileno);
},
} }
} }
@ -4565,7 +4562,7 @@ pub fn dup2(sync: *CancelRegion.Sync, old_fd: fd_t, new_fd: fd_t) DupError!void
while (true) { while (true) {
try sync.cancel_region.await(.nothing); try sync.cancel_region.await(.nothing);
switch (linux.errno(linux.dup2(old_fd, new_fd))) { switch (linux.errno(linux.dup2(old_fd, new_fd))) {
.SUCCESS => {}, .SUCCESS => return,
.BUSY, .INTR => {}, .BUSY, .INTR => {},
.INVAL => |err| return errnoBug(err), // invalid parameters .INVAL => |err| return errnoBug(err), // invalid parameters
.BADF => |err| return errnoBug(err), // use after free .BADF => |err| return errnoBug(err), // use after free

View file

@ -409,6 +409,12 @@ pub const SpawnOptions = struct {
/// Inherit the corresponding stream from the parent process. /// Inherit the corresponding stream from the parent process.
inherit, inherit,
/// Pass an already open file from the parent to the child. /// Pass an already open file from the parent to the child.
///
/// Nonblocking mode will be kept in the child process if present. This is
/// likely not supported by the child process. For example:
/// - Zig's std.Io.File.stdout() assumes blocking mode
/// - Rust explicity documents that nonblocking stdio may cause panics
/// - C++ standard streams do not support nonblocking file descriptors
file: File, file: File,
/// Pass a null stream to the child process by opening "/dev/null" on POSIX /// Pass a null stream to the child process by opening "/dev/null" on POSIX
/// and "NUL" on Windows. /// and "NUL" on Windows.