Skip to content

Commit 798dad2

Browse files
indutnyFishrock123
authored andcommitted
child_process: null channel handle on close
`HandleWrap::OnClose` destroys the underlying C++ object and null's the internal field pointer to it. Therefore there should be no references to the wrapping JavaScript object. `null` the process' `_channel` field right after closing it, to ensure no crashes will happen. Fix: #2847 PR-URL: #3041 Reviewed-By: Trevor Norris <[email protected]>
1 parent 039f73f commit 798dad2

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/internal/child_process.js

+1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ function setupChannel(target, channel) {
449449
target.disconnect();
450450
channel.onread = nop;
451451
channel.close();
452+
target._channel = null;
452453
maybeClose(target);
453454
}
454455
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
6+
const cluster = require('cluster');
7+
const net = require('net');
8+
const util = require('util');
9+
10+
if (!cluster.isMaster) {
11+
// Exit on first received handle to leave the queue non-empty in master
12+
process.on('message', function() {
13+
process.exit(1);
14+
});
15+
return;
16+
}
17+
18+
var server = net.createServer(function(s) {
19+
setTimeout(function() {
20+
s.destroy();
21+
}, 100);
22+
}).listen(common.PORT, function() {
23+
var worker = cluster.fork();
24+
25+
function send(callback) {
26+
var s = net.connect(common.PORT, function() {
27+
worker.send({}, s, callback);
28+
});
29+
}
30+
31+
worker.process.once('close', common.mustCall(function() {
32+
// Otherwise the crash on `_channel.fd` access may happen
33+
assert(worker.process._channel === null);
34+
server.close();
35+
}));
36+
37+
// Queue up several handles, to make `process.disconnect()` wait
38+
for (var i = 0; i < 100; i++)
39+
send();
40+
});

0 commit comments

Comments
 (0)