Skip to content

Commit

Permalink
Unref shared worker when all test workers have deregistered
Browse files Browse the repository at this point in the history
This should prevent the main process from exiting prematurely with a 0 exit code. See background in #3149 (comment).
  • Loading branch information
codetheweb authored Dec 13, 2022
1 parent 4ecfe7d commit 639b905
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/plugin-support/shared-workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ function launchWorker(filename, initialData) {
export async function observeWorkerProcess(fork, runStatus) {
let registrationCount = 0;
let signalDeregistered;
let launched;
const deregistered = new Promise(resolve => {
signalDeregistered = resolve;
signalDeregistered = () => {
// Only unref the worker once all test workers have been deregistered, otherwise the worker may exit before test workers are deregistered
launched?.worker.unref();
resolve();
};
});

fork.promise.finally(() => {
Expand All @@ -62,7 +67,7 @@ export async function observeWorkerProcess(fork, runStatus) {
});

fork.onConnectSharedWorker(async ({filename, initialData, port, signalError}) => {
const launched = launchWorker(filename, initialData);
launched = launchWorker(filename, initialData);

const handleWorkerMessage = async message => {
if (message.type === 'deregistered-test-worker' && message.id === fork.threadId) {
Expand Down Expand Up @@ -104,14 +109,7 @@ export async function observeWorkerProcess(fork, runStatus) {
});

launched.worker.on('message', handleWorkerMessage);
} catch {
return;
} finally {
// Attaching listeners has the side-effect of referencing the worker.
// Explicitly unreference it now so it does not prevent the main process
// from exiting.
launched.worker.unref();
}
} catch {}
});

return deregistered;
Expand Down
9 changes: 9 additions & 0 deletions test/shared-workers/timeout/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "module",
"ava": {
"files": [
"*.js"
],
"timeout": "5s"
}
}
14 changes: 14 additions & 0 deletions test/shared-workers/timeout/fixtures/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import test from 'ava';
import {registerSharedWorker} from 'ava/plugin';
import delay from 'delay';

registerSharedWorker({
filename: new URL('worker.mjs', import.meta.url),
supportedProtocols: ['ava-4'],
});

test('time out', async t => {
await delay(10_000);

t.pass();
});
3 changes: 3 additions & 0 deletions test/shared-workers/timeout/fixtures/worker.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default ({negotiateProtocol}) => {
negotiateProtocol(['ava-4']).ready();
};
9 changes: 9 additions & 0 deletions test/shared-workers/timeout/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import test from '@ava/test';

import {fixture} from '../../helpers/exec.js';

test('times out', async t => {
const error = await t.throwsAsync(async () => fixture());

t.is(error.exitCode, 1);
});

0 comments on commit 639b905

Please sign in to comment.