authorgravatar for rabingaire20@gmail.comRabin Gaire <rabingaire20@gmail.com> 2022-04-20 18:02:41+05:45
committergravatar for rabingaire20@gmail.comRabin Gaire <rabingaire20@gmail.com> 2022-04-20 18:02:41+05:45
log9e4cd1f4e6c3195665daa80c6a438cfd4daa92b1
tree3eb75978b7f806eaf4be1429ca6c1174d68be002
parent1a1b5ee264d8b2219c34d53cc9602692e6d2ba24

fix child process spawn on macos hangs issue

When a child process with stdin, stdout behavior set to pipe is ran on macos it used to hang which has been fixed. Issue existed because we forgot to call `posix_spawn_file_actions_addclose` syscall on user exposed file descriptor which resulted on file descriptor not closing properly.

1 files changed, 9 insertions(+), 5 deletions(-)

lib/std/child_process.zig+9-5
...@@ -593,9 +593,9 @@ pub const ChildProcess = struct {...@@ -593,9 +593,9 @@ pub const ChildProcess = struct {
593 var actions = try os.posix_spawn.Actions.init();593 var actions = try os.posix_spawn.Actions.init();
594 defer actions.deinit();594 defer actions.deinit();
595595
596 try setUpChildIoPosixSpawn(self.stdin_behavior, &actions, stdin_pipe[0], os.STDIN_FILENO, dev_null_fd);596 try setUpChildIoPosixSpawn(self.stdin_behavior, &actions, stdin_pipe, os.STDIN_FILENO, dev_null_fd);
597 try setUpChildIoPosixSpawn(self.stdout_behavior, &actions, stdout_pipe[1], os.STDOUT_FILENO, dev_null_fd);597 try setUpChildIoPosixSpawn(self.stdout_behavior, &actions, stdout_pipe, os.STDOUT_FILENO, dev_null_fd);
598 try setUpChildIoPosixSpawn(self.stderr_behavior, &actions, stderr_pipe[1], os.STDERR_FILENO, dev_null_fd);598 try setUpChildIoPosixSpawn(self.stderr_behavior, &actions, stderr_pipe, os.STDERR_FILENO, dev_null_fd);
599599
600 if (self.cwd_dir) |cwd| {600 if (self.cwd_dir) |cwd| {
601 try actions.fchdir(cwd.fd);601 try actions.fchdir(cwd.fd);
...@@ -650,12 +650,16 @@ pub const ChildProcess = struct {...@@ -650,12 +650,16 @@ pub const ChildProcess = struct {
650 fn setUpChildIoPosixSpawn(650 fn setUpChildIoPosixSpawn(
651 stdio: StdIo,651 stdio: StdIo,
652 actions: *os.posix_spawn.Actions,652 actions: *os.posix_spawn.Actions,
653 pipe_fd: i32,653 pipe_fd: [2]i32,
654 std_fileno: i32,654 std_fileno: i32,
655 dev_null_fd: i32,655 dev_null_fd: i32,
656 ) !void {656 ) !void {
657 switch (stdio) {657 switch (stdio) {
658 .Pipe => try actions.dup2(pipe_fd, std_fileno),658 .Pipe => {
659 const idx: usize = if (std_fileno == 0) 0 else 1;
660 try actions.dup2(pipe_fd[idx], std_fileno);
661 try actions.close(pipe_fd[1-idx]);
662 },
659 .Close => try actions.close(std_fileno),663 .Close => try actions.close(std_fileno),
660 .Inherit => {},664 .Inherit => {},
661 .Ignore => try actions.dup2(dev_null_fd, std_fileno),665 .Ignore => try actions.dup2(dev_null_fd, std_fileno),