Skip to content

Commit cb261c8

Browse files
committed
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 <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6396574 commit cb261c8

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
@@ -183,11 +183,24 @@ function createWritableStdioStream(fd) {
183183
case 'PIPE':
184184
case 'TCP':
185185
var net = require('net');
186-
stream = new net.Socket({
187-
fd: fd,
188-
readable: false,
189-
writable: true
190-
});
186+
187+
// If fd is already being used for the IPC channel, libuv will return
188+
// an error when trying to use it again. In that case, create the socket
189+
// using the existing handle instead of the fd.
190+
if (process.channel && process.channel.fd === fd) {
191+
stream = new net.Socket({
192+
handle: process.channel,
193+
readable: false,
194+
writable: true
195+
});
196+
} else {
197+
stream = new net.Socket({
198+
fd,
199+
readable: false,
200+
writable: true
201+
});
202+
}
203+
191204
stream._type = 'pipe';
192205
break;
193206

0 commit comments

Comments
 (0)