Skip to content

Commit a1a8e62

Browse files
committed
Keep IPC channel referenced once it's needed again
Ensure the IPC channel stays referenced. This is a safe-guard: it's easy to end up in a situation where the channel needs to be referenced but it's unreferenced because other code wasn't aware.
1 parent a631a73 commit a1a8e62

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/process-adapter.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ exports.send = (name, data) => {
2424

2525
// `process.channel` was added in Node.js 7.1.0, but the channel was available
2626
// through an undocumented API as `process._channel`.
27-
exports.ipcChannel = process.channel || process._channel;
27+
const ipcChannel = process.channel || process._channel;
28+
let allowUnref = true;
29+
exports.unrefChannel = () => {
30+
if (allowUnref) {
31+
ipcChannel.unref();
32+
}
33+
};
34+
exports.forceRefChannel = () => {
35+
allowUnref = false;
36+
ipcChannel.ref();
37+
};
2838

2939
const opts = JSON.parse(process.argv[2]);
3040
require('./worker-options').set(opts);

lib/test-worker.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function exit() {
4141
exiting = true;
4242

4343
// Reference the IPC channel so the exit sequence can be completed.
44-
adapter.ipcChannel.ref();
44+
adapter.forceRefChannel();
4545

4646
const stats = runner.buildStats();
4747
adapter.send('results', {stats});
@@ -116,7 +116,7 @@ function handleUncaughtException(exception) {
116116

117117
// Ensure the IPC channel is referenced. The uncaught exception will kick off
118118
// the teardown sequence, for which the messages must be received.
119-
adapter.ipcChannel.ref();
119+
adapter.forceRefChannel();
120120

121121
adapter.send('uncaughtException', {exception: serialized});
122122
}
@@ -139,7 +139,7 @@ process.on('ava-teardown', () => {
139139
tearingDown = true;
140140

141141
// Reference the IPC channel so the teardown sequence can be completed.
142-
adapter.ipcChannel.ref();
142+
adapter.forceRefChannel();
143143

144144
let rejections = currentlyUnhandled()
145145
.filter(rejection => !attributedRejections.has(rejection.promise));
@@ -206,6 +206,6 @@ try {
206206
// used to detect when tests stall.
207207
// If AVA was not required then the parent process will initiated a teardown
208208
// sequence, for which this process ought to stay active.
209-
adapter.ipcChannel.unref();
209+
adapter.unrefChannel();
210210
}
211211
}

0 commit comments

Comments
 (0)