Skip to content

Commit cd2b80c

Browse files
cjihrigtargos
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 fefa57a commit cd2b80c

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lib/internal/process/stdio.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,24 @@ function createWritableStdioStream(fd) {
182182
case 'PIPE':
183183
case 'TCP':
184184
var net = require('net');
185-
stream = new net.Socket({
186-
fd: fd,
187-
readable: false,
188-
writable: true
189-
});
185+
186+
// If fd is already being used for the IPC channel, libuv will return
187+
// an error when trying to use it again. In that case, create the socket
188+
// using the existing handle instead of the fd.
189+
if (process.channel && process.channel.fd === fd) {
190+
stream = new net.Socket({
191+
handle: process.channel,
192+
readable: false,
193+
writable: true
194+
});
195+
} else {
196+
stream = new net.Socket({
197+
fd,
198+
readable: false,
199+
writable: true
200+
});
201+
}
202+
190203
stream._type = 'pipe';
191204
break;
192205

0 commit comments

Comments
 (0)