Skip to content

Commit c419e08

Browse files
committed
fix(worker): Avoid crash on Bun due to stub promiseHooks (backport of 5d5bda)
1 parent 525090a commit c419e08

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

packages/worker/src/workflow/vm-shared.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import { UnhandledRejectionError } from '../errors';
1313
import { Workflow } from './interface';
1414
import { WorkflowBundleWithSourceMapAndFilename } from './workflow-worker-thread/input';
1515

16-
// Not present in @types/node for some reason
17-
const { promiseHooks } = v8 as any;
18-
1916
// Best effort to catch unhandled rejections from workflow code.
2017
// We crash the thread if we cannot find the culprit.
2118
export function setUnhandledRejectionHandler(getWorkflowByRunId: (runId: string) => BaseVMWorkflow | undefined): void {
@@ -106,9 +103,7 @@ export class GlobalHandlers {
106103
currentStackTrace: StackTraceFileLocation[] | undefined = undefined;
107104
bundleFilenameToSourceMapConsumer = new Map<string, SourceMapConsumer>();
108105
origPrepareStackTrace = Error.prepareStackTrace;
109-
private stopPromiseHook = () => {
110-
// noop
111-
};
106+
private stopPromiseHook = () => {};
112107
installed = false;
113108

114109
async addWorkflowBundle(workflowBundle: WorkflowBundleWithSourceMapAndFilename): Promise<void> {
@@ -200,9 +195,8 @@ export class GlobalHandlers {
200195
let currentAggregation: Promise<unknown> | undefined = undefined;
201196

202197
// This also is set globally for the isolate (worker thread), which is insignificant unless the worker is run in debug mode
203-
if (promiseHooks) {
204-
// Node >=16.14 only
205-
this.stopPromiseHook = promiseHooks.createHook({
198+
try {
199+
this.stopPromiseHook = v8.promiseHooks.createHook({
206200
init: (promise: Promise<unknown>, parent: Promise<unknown>) => {
207201
// Only run in workflow context
208202
const activator = getActivator(promise);
@@ -265,7 +259,13 @@ export class GlobalHandlers {
265259
store.childToParent.delete(promise);
266260
store.promiseToStack.delete(promise);
267261
},
268-
});
262+
}) as () => void;
263+
} catch (_) {
264+
// v8.promiseHooks.createHook is not available in bun and Node.js < 16.14.0.
265+
// That's ok, collecting stack trace is an optional feature anyway.
266+
//
267+
// FIXME: This should be sent to logs, not the console… but we don't have access to it here.
268+
console.warn('v8.promiseHooks.createHook is not available; stack trace collection will be disabled.');
269269
}
270270
}
271271
}

0 commit comments

Comments
 (0)