forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-child-process-fork-regr-gh-2847.js
68 lines (59 loc) · 1.81 KB
/
test-child-process-fork-regr-gh-2847.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const net = require('net');
var connectcount = 0;
var sendcount = 0;
if (!cluster.isMaster) {
// Exit on first received handle to leave the queue non-empty in master
process.on('message', function() {
process.exit(1);
});
return;
}
var server = net.createServer(function(s) {
if (common.isWindows) {
s.on('error', function(err) {
// Prevent possible ECONNRESET errors from popping up
if (err.code !== 'ECONNRESET' || sendcount === 0)
throw err;
});
}
setTimeout(function() {
s.destroy();
}, 100);
}).listen(common.PORT, function() {
var worker = cluster.fork();
function send(callback) {
var s = net.connect(common.PORT, function() {
worker.send({}, s, callback);
connectcount++;
});
// Errors can happen if the connections
// are still happening while the server has been closed.
// This can happen depending on how the messages are
// bundled into packets. If they all make it into the first
// one then no errors will occur, otherwise the server
// may have been closed by the time the later ones make
// it to the server side.
// We ignore any errors that occur after some connections
// get through
s.on('error', function(err) {
if (connectcount < 3)
console.log(err);
});
}
worker.process.once('close', common.mustCall(function() {
// Otherwise the crash on `_channel.fd` access may happen
assert(worker.process._channel === null);
server.close();
}));
// Queue up several handles, to make `process.disconnect()` wait
for (var i = 0; i < 100; i++)
send(function(err) {
if (err && sendcount < 3)
console.log(err);
sendcount++;
});
});