Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: fix nested uncaught exception handling #34310

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/internal/error_serdes.js
Expand Up @@ -14,6 +14,7 @@ const {
ObjectKeys,
ObjectPrototypeToString,
SafeSet,
SymbolToStringTag,
} = primordials;

const kSerializedError = 0;
Expand Down Expand Up @@ -116,6 +117,10 @@ function deserializeError(error) {
case kSerializedError:
const { constructor, properties } = deserialize(error.subarray(1));
const ctor = errors[constructor];
ObjectDefineProperty(properties, SymbolToStringTag, {
value: { value: 'Error', configurable: true },
enumerable: true
});
return ObjectCreate(ctor.prototype, properties);
case kSerializedObject:
return deserialize(error.subarray(1));
Expand Down
9 changes: 7 additions & 2 deletions test/parallel/test-error-serdes.js
Expand Up @@ -16,12 +16,17 @@ assert.strictEqual(cycle(null), null);
assert.strictEqual(cycle(undefined), undefined);
assert.strictEqual(cycle('foo'), 'foo');

{
const err = cycle(new Error('foo'));
let err = new Error('foo');
for (let i = 0; i < 10; i++) {
assert(err instanceof Error);
assert(Object.prototype.toString.call(err), '[object Error]');
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, 'foo');
assert(/^Error: foo\n/.test(err.stack));

const prev = err;
err = cycle(err);
assert.deepStrictEqual(err, prev);
}

assert.strictEqual(cycle(new RangeError('foo')).name, 'RangeError');
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-worker-nested-uncaught.js
@@ -0,0 +1,14 @@
'use strict';
const common = require('../common');
const { Worker } = require('worker_threads');

// Regression test for https://github.com/nodejs/node/issues/34309

const w = new Worker(
`const { Worker } = require('worker_threads');
new Worker("throw new Error('uncaught')", { eval:true })`,
{ eval: true });
w.on('error', common.expectsError({
name: 'Error',
message: 'uncaught'
}));