From 4a40e57cf059011e37aa45d43f48733b8e5546b0 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 26 Nov 2021 18:44:11 -0600 Subject: [PATCH] Fix leaking file descriptors to subprocess causing hangs. --- src/core/ev.c | 2 ++ src/core/os.c | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/core/ev.c b/src/core/ev.c index 3476937e..05317e9b 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -2641,6 +2641,8 @@ int janet_make_pipe(JanetHandle handles[2], int mode) { return 0; #else if (pipe(handles)) return -1; + if (mode != 2 && fcntl(handles[0], F_SETFD, FD_CLOEXEC)) goto error; + if (mode != 1 && fcntl(handles[1], F_SETFD, FD_CLOEXEC)) goto error; if (mode != 2 && fcntl(handles[0], F_SETFL, O_NONBLOCK)) goto error; if (mode != 1 && fcntl(handles[1], F_SETFL, O_NONBLOCK)) goto error; return 0; diff --git a/src/core/os.c b/src/core/os.c index 02fcfda0..b675c635 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -970,18 +970,24 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, int is_spawn) { posix_spawn_file_actions_init(&actions); if (pipe_in != JANET_HANDLE_NONE) { posix_spawn_file_actions_adddup2(&actions, pipe_in, 0); + posix_spawn_file_actions_addclose(&actions, pipe_in); } else if (new_in != JANET_HANDLE_NONE) { posix_spawn_file_actions_adddup2(&actions, new_in, 0); + posix_spawn_file_actions_addclose(&actions, new_in); } if (pipe_out != JANET_HANDLE_NONE) { posix_spawn_file_actions_adddup2(&actions, pipe_out, 1); + posix_spawn_file_actions_addclose(&actions, pipe_out); } else if (new_out != JANET_HANDLE_NONE) { posix_spawn_file_actions_adddup2(&actions, new_out, 1); + posix_spawn_file_actions_addclose(&actions, new_out); } if (pipe_err != JANET_HANDLE_NONE) { posix_spawn_file_actions_adddup2(&actions, pipe_err, 2); + posix_spawn_file_actions_addclose(&actions, pipe_err); } else if (new_err != JANET_HANDLE_NONE) { posix_spawn_file_actions_adddup2(&actions, new_err, 2); + posix_spawn_file_actions_addclose(&actions, new_err); } pid_t pid;