From 0187e3bef8c4f9898555832ed9ffa9fc81c6a517 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 21 Jun 2018 22:18:21 -0400 Subject: [PATCH] process: avoid using the same fd for ipc and stdio There is already a check in place to prevent stdin and the IPC channel from sharing a file descriptor. This commit adds a similar check to stdout and stderr. Backport-PR-URL: https://github.com/nodejs/node/pull/24103 Refs: https://github.com/libuv/libuv/pull/1851 Refs: https://github.com/libuv/libuv/issues/1897 PR-URL: https://github.com/nodejs/node/pull/21466 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno Reviewed-By: James M Snell --- lib/internal/process/stdio.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/internal/process/stdio.js b/lib/internal/process/stdio.js index 45568ae631698b..7faef381f895bf 100644 --- a/lib/internal/process/stdio.js +++ b/lib/internal/process/stdio.js @@ -161,11 +161,24 @@ function createWritableStdioStream(fd) { case 'PIPE': case 'TCP': var net = require('net'); - stream = new net.Socket({ - fd: fd, - readable: false, - writable: true - }); + + // If fd is already being used for the IPC channel, libuv will return + // an error when trying to use it again. In that case, create the socket + // using the existing handle instead of the fd. + if (process.channel && process.channel.fd === fd) { + stream = new net.Socket({ + handle: process.channel, + readable: false, + writable: true + }); + } else { + stream = new net.Socket({ + fd, + readable: false, + writable: true + }); + } + stream._type = 'pipe'; break;