Skip to content

Commit fcc6479

Browse files
TrottMyles Borins
authored and
Myles Borins
committed
child_process: guard against race condition
It is possible that the internal handleMessage() might try to send to a channel that has been closed. The result can be an AssertionError. Guard against this. Fixes: #4205 PR-URL: #5153 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent d421e85 commit fcc6479

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/child_process.js

+3
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ function getSocketList(type, slave, key) {
313313

314314
var INTERNAL_PREFIX = 'NODE_';
315315
function handleMessage(target, message, handle) {
316+
if (!target._channel)
317+
return;
318+
316319
var eventName = 'message';
317320
if (!util.isNull(message) &&
318321
util.isObject(message) &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
// This code triggers an AssertionError on Linux in Node.js 5.3.0 and earlier.
4+
// Ref: https://github.com/nodejs/node/issues/4205
5+
6+
var common = require('../common');
7+
var assert = require('assert');
8+
var net = require('net');
9+
var cluster = require('cluster');
10+
cluster.schedulingPolicy = cluster.SCHED_NONE;
11+
12+
if (cluster.isMaster) {
13+
var worker1, worker2;
14+
15+
worker1 = cluster.fork();
16+
worker1.on('message', common.mustCall(function() {
17+
worker2 = cluster.fork();
18+
worker1.disconnect();
19+
worker2.on('online', common.mustCall(worker2.disconnect));
20+
}, 2));
21+
22+
cluster.on('exit', function(worker, code) {
23+
assert.strictEqual(code, 0, 'worker exited with error');
24+
});
25+
26+
return;
27+
}
28+
29+
var server = net.createServer();
30+
31+
server.listen(common.PORT, function retry() {
32+
process.send('listening');
33+
process.send('listening');
34+
});

0 commit comments

Comments
 (0)