Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fixed/unflappable-flamingo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workers no longer abort on startup when Node.js runs with `--abort-on-uncaught-exception`. The Worker now preloads the Workflow bundle for the debugger only when an inspector is active (#1578).
19 changes: 19 additions & 0 deletions packages/test/src/test-bundler.local.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** Bundler generation and validation tests that do not require a server. */
import { spawnSync } from 'node:child_process';
import { randomUUID } from 'crypto';
import test from 'ava';
import { moduleMatches, parseWorkflowCode } from '@temporalio/worker/internal';
Expand Down Expand Up @@ -33,6 +34,24 @@ test('Workflow bundle contains the SDK version annotation after minification', a
t.notThrows(() => parseWorkflowCode(code));
});

// Regression test for https://github.com/temporalio/sdk-typescript/issues/1578:
// V8 can't tell that an error thrown inside a vm context gets caught outside of it, so
// evaluating the bundle in an unconfigured context was fatal under this flag.
test('parseWorkflowCode does not abort a process started with --abort-on-uncaught-exception (#1578)', async (t) => {
const { code } = await bundleWorkflowCode({
workflowsPath: require.resolve('./workflows/preload-shared-counter'),
});

const script = `require('@temporalio/worker/internal').parseWorkflowCode(require('node:fs').readFileSync(0, 'utf8'))`;
const { status, signal, stderr } = spawnSync(process.execPath, ['--abort-on-uncaught-exception', '-e', script], {
cwd: __dirname,
input: code,
encoding: 'utf8',
});

t.deepEqual({ status, signal }, { status: 0, signal: null }, stderr);
});

test('Workflow bundle from a different SDK version is rejected', async (t) => {
const { code } = await bundleWorkflowCode({
workflowsPath: require.resolve('./workflows/preload-shared-counter'),
Expand Down
43 changes: 24 additions & 19 deletions packages/worker/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from 'node:crypto';
import fs from 'node:fs/promises';
import * as inspector from 'node:inspector';
import * as path from 'node:path';
import * as vm from 'node:vm';
import { EventEmitter, on } from 'node:events';
Expand Down Expand Up @@ -2476,26 +2477,30 @@ export function parseWorkflowCode(code: string, codePath?: string): WorkflowBund
code = actualCode + fixedSourceMappingUrl;
}

// Preloading the script makes breakpoints significantly more reliable and more responsive
let script: vm.Script | undefined = new vm.Script(code, { filename });
let context: any = vm.createContext({});
try {
// displayErrors: false keeps Node from formatting the stack of an error we ignore, which would parse and cache
// the bundle's source map.
script.runInContext(context, { displayErrors: false });
} catch (_e) {
// Context has not been properly configured, so eventual errors are possible. Just ignore at this point
}
// Preloading the script makes breakpoints significantly more reliable and more responsive. That only matters to a
// debugger, and the preload is expected to throw, which V8 treats as fatal under `--abort-on-uncaught-exception`
// because it can't see that the error is caught outside of the vm context.
if (inspector.url() !== undefined) {
let script: vm.Script | undefined = new vm.Script(code, { filename });
let context: any = vm.createContext({});
try {
// displayErrors: false keeps Node from formatting the stack of an error we ignore, which would parse and cache
// the bundle's source map.
script.runInContext(context, { displayErrors: false });
} catch (_e) {
// Context has not been properly configured, so eventual errors are possible. Just ignore at this point
}

// Keep these objects from GC long enough for debugger to complete parsing the source map and reporting locations
// to the node process. Otherwise, the debugger risks source mapping resolution errors, meaning breakpoints wont work.
setTimeoutUnref(10000).then(
() => {
script = undefined;
context = undefined;
},
() => void 0
);
// Keep these objects from GC long enough for debugger to complete parsing the source map and reporting locations
// to the node process. Otherwise, the debugger risks source mapping resolution errors, meaning breakpoints wont work.
setTimeoutUnref(10000).then(
() => {
script = undefined;
context = undefined;
},
() => void 0
);
}

return { code, sourceMap, filename };
}
Expand Down