Skip to content

Commit

Permalink
Track worker errors
Browse files Browse the repository at this point in the history
They're not native due to nodejs/node#48716, but we want to treat them as such anyway.
  • Loading branch information
novemberborn committed Jul 30, 2023
1 parent f047694 commit beff6b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Worker} from 'node:worker_threads';
import Emittery from 'emittery';

import {controlFlow} from './ipc-flow-control.cjs';
import serializeError from './serialize-error.js';
import serializeError, {tagWorkerError} from './serialize-error.js';

let workerPath = new URL('worker/base.js', import.meta.url);
export function _testOnlyReplaceWorkerPath(replacement) {
Expand Down Expand Up @@ -134,7 +134,7 @@ export default function loadFork(file, options, execArgv = process.execArgv) {
});

worker.on('error', error => {
emitStateChange({type: 'worker-failed', err: serializeError('Worker error', false, error, file)});
emitStateChange({type: 'worker-failed', err: serializeError('Worker error', false, tagWorkerError(error), file)});
finish();
});

Expand Down
4 changes: 2 additions & 2 deletions lib/plugin-support/shared-workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import events from 'node:events';
import {pathToFileURL} from 'node:url';
import {Worker} from 'node:worker_threads';

import serializeError from '../serialize-error.js';
import serializeError, {tagWorkerError} from '../serialize-error.js';

const LOADER = new URL('shared-worker-loader.js', import.meta.url);

Expand Down Expand Up @@ -34,7 +34,7 @@ function launchWorker(filename, initialData) {
const launched = {
statePromises: {
available: waitForAvailable(worker),
error: events.once(worker, 'error').then(([error]) => error),
error: events.once(worker, 'error').then(([error]) => tagWorkerError(error)),
},
exited: false,
worker,
Expand Down
16 changes: 15 additions & 1 deletion lib/serialize-error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path';
import process from 'node:process';
import {fileURLToPath, pathToFileURL} from 'node:url';
import {isNativeError} from 'node:util/types';

import cleanYamlObject from 'clean-yaml-object';
import concordance from 'concordance';
Expand Down Expand Up @@ -145,8 +146,21 @@ function trySerializeError(error, shouldBeautifyStack, testFile) {
return retval;
}

const workerErrors = new WeakSet();
export function tagWorkerError(error) {
// Track worker errors, which aren't native due to https://github.com/nodejs/node/issues/48716.
// Still include the check for isNativeError() in case the issue is fixed in the future.
if (isNativeError(error) || error instanceof Error) {
workerErrors.add(error);
}

return error;
}

const isWorkerError = error => workerErrors.has(error);

export default function serializeError(origin, shouldBeautifyStack, error, testFile) {
if (!isError(error)) {
if (!isError(error) && !isWorkerError(error)) {
return {
avaAssertionError: false,
nonErrorObject: true,
Expand Down

0 comments on commit beff6b2

Please sign in to comment.