From cd2b80c1f57faa349232276e113fe09be5da1106 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. 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 e5bb50e8ed81de..f4a07cd06d09d9 100644 --- a/lib/internal/process/stdio.js +++ b/lib/internal/process/stdio.js @@ -182,11 +182,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;