From b736d6b364421c4ee6fdf3bca4bc7c935259ea06 Mon Sep 17 00:00:00 2001 From: Alistair Smith Date: Tue, 27 May 2025 15:48:07 -0700 Subject: [PATCH] this back --- src/bun.js/web_worker.zig | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/bun.js/web_worker.zig b/src/bun.js/web_worker.zig index 6f98c9bc4a..4343a9ed81 100644 --- a/src/bun.js/web_worker.zig +++ b/src/bun.js/web_worker.zig @@ -570,6 +570,30 @@ pub fn notifyNeedTermination(this: *WebWorker) callconv(.c) void { } log("[{d}] notifyNeedTermination", .{this.execution_context_id}); + const current_status = this.status.load(.acquire); + + // If the worker hasn't started yet or is still starting, we need to handle termination differently + // This can happen when: + // 1. terminate() is called immediately after creating the worker + // 2. The worker thread hasn't been spawned yet + // 3. The worker thread is spawned but hasn't reached the event loop + // In these cases, we need to dispatch the exit event immediately because the worker + // thread might not be able to do it itself. + if (current_status == .start or current_status == .starting) { + log("[{d}] notifyNeedTermination - worker not fully started, dispatching exit immediately", .{this.execution_context_id}); + + this.setStatus(.terminated); + + // Dispatch the exit event immediately since the worker thread might not be running yet + // or might not have reached the event loop + WebWorker__dispatchExit(null, this.cpp_worker, 0); + this.parent_poll_ref.unrefConcurrently(this.parent); + + if (this.vm) |vm| vm.eventLoop().wakeup(); + + return; + } + if (this.vm) |vm| { vm.eventLoop().wakeup(); // TODO(@190n) notifyNeedTermination