Skip to content

Commit

Permalink
Attempt to workaround worker thread termination crash
Browse files Browse the repository at this point in the history
See discussion in nodejs/node#38418
  • Loading branch information
novemberborn committed Jun 20, 2021
1 parent c1c7edb commit f97da78
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {fileURLToPath} from 'url';
import {Worker} from 'worker_threads';

import Emittery from 'emittery';
import pEvent from 'p-event';

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

Expand Down Expand Up @@ -31,8 +32,14 @@ const createWorker = (options, execArgv) => {
stderr: true
});
postMessage = worker.postMessage.bind(worker);

// Ensure we've seen this event before we terminate the worker thread, as a
// workaround for https://github.com/nodejs/node/issues/38418.
const starting = pEvent(worker, 'message', ({ava}) => ava && ava.type === 'starting');

close = async () => {
try {
await starting;
await worker.terminate();
} finally {
// No-op
Expand Down
1 change: 1 addition & 0 deletions lib/worker/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ const onError = error => {
};

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

0 comments on commit f97da78

Please sign in to comment.