From 11d80117936bea8da25d28bcf5402615dd3ead05 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 2 Oct 2013 11:17:18 +0200 Subject: [PATCH] unix: don't close inherited fds on uv_spawn() fail The cleanup-after-error code path in uv_spawn() was closing file descriptors indiscriminately. Only close file descriptors that we created ourselves, not the ones that are passed in by the user. Fixes joyent/node#6297. --- src/unix/process.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/unix/process.c b/src/unix/process.c index ad7ca648..389817f2 100644 --- a/src/unix/process.c +++ b/src/unix/process.c @@ -186,7 +186,7 @@ skip: /* * Used for initializing stdio streams like options.stdin_stream. Returns - * zero on success. + * zero on success. See also the cleanup section in uv_spawn(). */ static int uv__process_init_stdio(uv_stdio_container_t* container, int fds[2]) { int mask; @@ -465,8 +465,13 @@ error: if (pipes != NULL) { for (i = 0; i < stdio_count; i++) { - close(pipes[i][0]); - close(pipes[i][1]); + if (i < options.stdio_count) + if (options.stdio[i].flags & (UV_INHERIT_FD | UV_INHERIT_STREAM)) + continue; + if (pipes[i][0] != -1) + close(pipes[i][0]); + if (pipes[i][1] != -1) + close(pipes[i][1]); } free(pipes); }