Permalink
Browse files

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 nodejs/node-v0.x-archive#6297.
  • Loading branch information...
1 parent fc3a21f commit 11d80117936bea8da25d28bcf5402615dd3ead05 @bnoordhuis bnoordhuis committed Oct 2, 2013
Showing with 8 additions and 3 deletions.
  1. +8 −3 src/unix/process.c
View
@@ -186,7 +186,7 @@ int uv__make_pipe(int fds[2], int flags) {
/*
* 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 @@ int uv_spawn(uv_loop_t* loop,
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);
}

0 comments on commit 11d8011

Please sign in to comment.