Skip to content

Commit de6095c

Browse files
cjihrigMylesBorins
authored andcommitted
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: libuv/libuv#1851 Refs: libuv/libuv#1897 PR-URL: #21466 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 49910ce commit de6095c

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lib/internal/process/stdio.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,24 @@ function createWritableStdioStream(fd) {
161161
case 'PIPE':
162162
case 'TCP':
163163
var net = require('net');
164-
stream = new net.Socket({
165-
fd: fd,
166-
readable: false,
167-
writable: true
168-
});
164+
165+
// If fd is already being used for the IPC channel, libuv will return
166+
// an error when trying to use it again. In that case, create the socket
167+
// using the existing handle instead of the fd.
168+
if (process.channel && process.channel.fd === fd) {
169+
stream = new net.Socket({
170+
handle: process.channel,
171+
readable: false,
172+
writable: true
173+
});
174+
} else {
175+
stream = new net.Socket({
176+
fd,
177+
readable: false,
178+
writable: true
179+
});
180+
}
181+
169182
stream._type = 'pipe';
170183
break;
171184

0 commit comments

Comments
 (0)