Skip to content

Commit

Permalink
Allow workers to continue execution if onerror is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarbs committed Jan 22, 2025
1 parent b0c5a76 commit 89c48f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/bun.js/bindings/webcore/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ extern "C" void WebWorker__dispatchOnline(Worker* worker, Zig::GlobalObject* glo
worker->dispatchOnline(globalObject);
}

extern "C" void WebWorker__dispatchError(Zig::GlobalObject* globalObject, Worker* worker, BunString message, JSC::EncodedJSValue errorValue)
extern "C" bool WebWorker__dispatchError(Zig::GlobalObject* globalObject, Worker* worker, BunString message, JSC::EncodedJSValue errorValue)
{
JSValue error = JSC::JSValue::decode(errorValue);
ErrorEvent::Init init;
Expand All @@ -454,8 +454,13 @@ extern "C" void WebWorker__dispatchError(Zig::GlobalObject* globalObject, Worker
init.cancelable = false;
init.bubbles = false;

globalObject->globalEventScope.dispatchEvent(ErrorEvent::create(eventNames().errorEvent, init, EventIsTrusted::Yes));
bool hasListeners = globalObject->globalEventScope.hasEventListeners(eventNames().errorEvent);
if (hasListeners) {
globalObject->globalEventScope.dispatchEvent(ErrorEvent::create(eventNames().errorEvent, init, EventIsTrusted::Yes));
return true;
}
worker->dispatchError(message.toWTFString(BunString::ZeroCopy));
return false;
}

extern "C" WebCore::Worker* WebWorker__getParentWorker(void*);
Expand Down
10 changes: 7 additions & 3 deletions src/bun.js/web_worker.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub const WebWorker = struct {

extern fn WebWorker__dispatchExit(?*JSC.JSGlobalObject, *anyopaque, i32) void;
extern fn WebWorker__dispatchOnline(this: *anyopaque, *JSC.JSGlobalObject) void;
extern fn WebWorker__dispatchError(*JSC.JSGlobalObject, *anyopaque, bun.String, JSValue) void;
extern fn WebWorker__dispatchError(*JSC.JSGlobalObject, *anyopaque, bun.String, JSValue) bool;

export fn WebWorker__getParentWorker(vm: *JSC.VirtualMachine) ?*anyopaque {
const worker = vm.worker orelse return null;
Expand Down Expand Up @@ -330,7 +330,7 @@ pub const WebWorker = struct {
const err = vm.log.toJS(vm.global, bun.default_allocator, "Error in worker");
const str = err.toBunString(vm.global);
defer str.deref();
WebWorker__dispatchError(vm.global, this.cpp_worker, str, err);
_ = WebWorker__dispatchError(vm.global, this.cpp_worker, str, err);
}

fn onUnhandledRejection(vm: *JSC.VirtualMachine, globalObject: *JSC.JSGlobalObject, error_instance_or_exception: JSC.JSValue) void {
Expand Down Expand Up @@ -375,7 +375,11 @@ pub const WebWorker = struct {
bun.outOfMemory();
};
JSC.markBinding(@src());
WebWorker__dispatchError(globalObject, worker.cpp_worker, bun.String.createUTF8(array.slice()), error_instance);
const handled = WebWorker__dispatchError(globalObject, worker.cpp_worker, bun.String.createUTF8(array.slice()), error_instance);
if (handled) {
vm.unhandled_error_counter -= 1;
return;
}
if (vm.worker) |worker_| {
_ = worker.setRequestedTerminate();
worker.parent_poll_ref.unrefConcurrently(worker.parent);
Expand Down

0 comments on commit 89c48f2

Please sign in to comment.