Skip to content

Commit 2526282

Browse files
TrottFishrock123
authored andcommitted
child_process: guard against race condition
It is possible that the internal hnadleMessage() might try to send to a channel that has been closed. The result can be an AssertionError. Guard against this. Fixes: #4205 PR-URL: #4418 Reviewed-By: Brian White <[email protected]> Reviewed-By: Johan Bergström <[email protected]>
1 parent cd79ec2 commit 2526282

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/internal/child_process.js

+3
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ function setupChannel(target, channel) {
675675

676676
const INTERNAL_PREFIX = 'NODE_';
677677
function handleMessage(target, message, handle) {
678+
if (!target._channel)
679+
return;
680+
678681
var eventName = 'message';
679682
if (message !== null &&
680683
typeof message === 'object' &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
const common = require('../common');
7+
const assert = require('assert');
8+
const net = require('net');
9+
const 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+
}));
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() {
32+
process.send('listening');
33+
});

0 commit comments

Comments
 (0)