Skip to content

Commit 6349a34

Browse files
committed
fix: Failing with remaining tests (Windows)
This change ensures that all messages from the worker are received by the main process before the worker exits. It solves issues on Windows that are occurring with Node.js v20 and later when `workerThreads: false` is set in the AVA configuration. Fixes: #3390
1 parent 41a684f commit 6349a34

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

lib/fork.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ export default function loadFork(file, options, execArgv = process.execArgv) {
104104
}
105105

106106
switch (message.ava.type) {
107+
case 'worker-finished': {
108+
send({type: 'worker-should-exit'});
109+
break;
110+
}
111+
107112
case 'ready-for-options': {
108113
send({type: 'options', options});
109114
break;

lib/worker/base.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ const run = async options => {
120120
return;
121121
}
122122

123+
channel.send({type: 'worker-finished'});
124+
125+
// As the channel has already been unreferenced
126+
// we need to reference it again to wait for the worker-should-exit message.
127+
// Otherwise the process may exit prematurely, especially on Windows.
128+
channel.ref();
129+
await channel.workerShouldExit;
130+
channel.unref();
131+
123132
nowAndTimers.setImmediate(() => {
124133
const unhandled = currentlyUnhandled();
125134
if (unhandled.length === 0) {

lib/worker/channel.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ handle.ref();
107107

108108
exports.options = selectAvaMessage(handle.channel, 'options').then(message => message.ava.options);
109109
exports.peerFailed = selectAvaMessage(handle.channel, 'peer-failed');
110+
exports.workerShouldExit = selectAvaMessage(handle.channel, 'worker-should-exit');
110111
exports.send = handle.send.bind(handle);
112+
exports.ref = handle.ref.bind(handle);
111113
exports.unref = handle.unref.bind(handle);
112114

113115
let channelCounter = 0;

0 commit comments

Comments
 (0)