node/test/sequential/test-child-process-pass-fd.js
Santiago Gimeno 5ed5142158 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: https://github.com/nodejs/node/issues/7512
Ref: https://github.com/nodejs/node/pull/8904
PR-URL: https://github.com/nodejs/node/pull/7572
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2016-10-26 14:09:13 -04:00

53 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const fork = require('child_process').fork;
const net = require('net');
if ((process.config.variables.arm_version === '6') ||
(process.config.variables.arm_version === '7')) {
common.skip('Too slow for armv6 and armv7 bots');
return;
}
const N = 80;
if (process.argv[2] !== 'child') {
for (let i = 0; i < N; ++i) {
const worker = fork(__filename, ['child', common.PORT + i]);
worker.once('message', common.mustCall((msg, handle) => {
assert.strictEqual(msg, 'handle');
assert.ok(handle);
worker.send('got');
let recvData = '';
handle.on('data', common.mustCall((data) => {
recvData += data;
}));
handle.on('end', () => {
assert.strictEqual(recvData, 'hello');
worker.kill();
});
}));
}
} else {
let socket;
const port = process.argv[3];
let cbcalls = 0;
function socketConnected() {
if (++cbcalls === 2)
process.send('handle', socket);
}
const server = net.createServer((c) => {
process.once('message', function(msg) {
assert.strictEqual(msg, 'got');
c.end('hello');
});
socketConnected();
});
server.listen(port, common.localhostIPv4, () => {
socket = net.connect(port, common.localhostIPv4, socketConnected);
});
}