Skip to content

Commit f97da78

Browse files
committed
Attempt to workaround worker thread termination crash
See discussion in nodejs/node#38418
1 parent c1c7edb commit f97da78

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/fork.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {fileURLToPath} from 'url';
33
import {Worker} from 'worker_threads';
44

55
import Emittery from 'emittery';
6+
import pEvent from 'p-event';
67

78
import {controlFlow} from './ipc-flow-control.cjs';
89

@@ -31,8 +32,14 @@ const createWorker = (options, execArgv) => {
3132
stderr: true
3233
});
3334
postMessage = worker.postMessage.bind(worker);
35+
36+
// Ensure we've seen this event before we terminate the worker thread, as a
37+
// workaround for https://github.com/nodejs/node/issues/38418.
38+
const starting = pEvent(worker, 'message', ({ava}) => ava && ava.type === 'starting');
39+
3440
close = async () => {
3541
try {
42+
await starting;
3643
await worker.terminate();
3744
} finally {
3845
// No-op

lib/worker/base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ const onError = error => {
222222
};
223223

224224
if (isRunningInThread) {
225+
channel.send({type: 'starting'}); // AVA won't terminate the worker thread until it's seen this message.
225226
const {options} = workerData;
226227
delete workerData.options; // Don't allow user code access.
227228
run(options).catch(onError); // eslint-disable-line promise/prefer-await-to-then

0 commit comments

Comments
 (0)