Skip to content

Commit 5ed5142

Browse files
santigimenoMylesBorins
authored andcommitted
child_process: workaround fd passing issue on OS X
There's an issue on some `OS X` versions when passing fd's between processes. When the handle associated to a specific file descriptor is closed by the sender process before it's received in the destination, the handle is indeed closed while it should remain opened. In order to fix this behaviour, don't close the handle until the `NODE_HANDLE_ACK` is received by the sender. Added `test-child-process-pass-fd` that is basically `test-cluster-net-send` but creating lots of workers, so the issue reproduces on `OS X` consistently. Fixes: #7512 Ref: #8904 PR-URL: #7572 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 6effc4a commit 5ed5142

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

lib/internal/child_process.js

+33-8
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,21 @@ const handleConversion = {
9090
return handle;
9191
},
9292

93-
postSend: function(handle) {
94-
// Close the Socket handle after sending it
95-
if (handle)
96-
handle.close();
93+
postSend: function(handle, target) {
94+
// Store the handle after successfully sending it, so it can be closed
95+
// when the NODE_HANDLE_ACK is received. If the handle could not be sent,
96+
// just close it.
97+
if (handle) {
98+
if (target) {
99+
// There can only be one _pendingHandle as passing handles are
100+
// processed one at a time: handles are stored in _handleQueue while
101+
// waiting for the NODE_HANDLE_ACK of the current passing handle.
102+
assert(!target._pendingHandle);
103+
target._pendingHandle = handle;
104+
} else {
105+
handle.close();
106+
}
107+
}
97108
},
98109

99110
got: function(message, handle, emit) {
@@ -396,6 +407,7 @@ ChildProcess.prototype.unref = function() {
396407
function setupChannel(target, channel) {
397408
target._channel = channel;
398409
target._handleQueue = null;
410+
target._pendingHandle = null;
399411

400412
const control = new class extends EventEmitter {
401413
constructor() {
@@ -461,6 +473,11 @@ function setupChannel(target, channel) {
461473
target.on('internalMessage', function(message, handle) {
462474
// Once acknowledged - continue sending handles.
463475
if (message.cmd === 'NODE_HANDLE_ACK') {
476+
if (target._pendingHandle) {
477+
target._pendingHandle.close();
478+
target._pendingHandle = null;
479+
}
480+
464481
assert(Array.isArray(target._handleQueue));
465482
var queue = target._handleQueue;
466483
target._handleQueue = null;
@@ -587,13 +604,16 @@ function setupChannel(target, channel) {
587604
var err = channel.writeUtf8String(req, string, handle);
588605

589606
if (err === 0) {
590-
if (handle && !this._handleQueue)
591-
this._handleQueue = [];
607+
if (handle) {
608+
if (!this._handleQueue)
609+
this._handleQueue = [];
610+
if (obj && obj.postSend)
611+
obj.postSend(handle, target);
612+
}
613+
592614
req.oncomplete = function() {
593615
if (this.async === true)
594616
control.unref();
595-
if (obj && obj.postSend)
596-
obj.postSend(handle);
597617
if (typeof callback === 'function')
598618
callback(null);
599619
};
@@ -654,6 +674,11 @@ function setupChannel(target, channel) {
654674
// This marks the fact that the channel is actually disconnected.
655675
this._channel = null;
656676

677+
if (this._pendingHandle) {
678+
this._pendingHandle.close();
679+
this._pendingHandle = null;
680+
}
681+
657682
var fired = false;
658683
function finish() {
659684
if (fired) return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fork = require('child_process').fork;
5+
const net = require('net');
6+
7+
if ((process.config.variables.arm_version === '6') ||
8+
(process.config.variables.arm_version === '7')) {
9+
common.skip('Too slow for armv6 and armv7 bots');
10+
return;
11+
}
12+
13+
const N = 80;
14+
15+
if (process.argv[2] !== 'child') {
16+
for (let i = 0; i < N; ++i) {
17+
const worker = fork(__filename, ['child', common.PORT + i]);
18+
worker.once('message', common.mustCall((msg, handle) => {
19+
assert.strictEqual(msg, 'handle');
20+
assert.ok(handle);
21+
worker.send('got');
22+
23+
let recvData = '';
24+
handle.on('data', common.mustCall((data) => {
25+
recvData += data;
26+
}));
27+
28+
handle.on('end', () => {
29+
assert.strictEqual(recvData, 'hello');
30+
worker.kill();
31+
});
32+
}));
33+
}
34+
} else {
35+
let socket;
36+
const port = process.argv[3];
37+
let cbcalls = 0;
38+
function socketConnected() {
39+
if (++cbcalls === 2)
40+
process.send('handle', socket);
41+
}
42+
43+
const server = net.createServer((c) => {
44+
process.once('message', function(msg) {
45+
assert.strictEqual(msg, 'got');
46+
c.end('hello');
47+
});
48+
socketConnected();
49+
});
50+
server.listen(port, common.localhostIPv4, () => {
51+
socket = net.connect(port, common.localhostIPv4, socketConnected);
52+
});
53+
}

0 commit comments

Comments
 (0)