From 505d1be9bf464eeff05251adcf145d1aa2bcf312 Mon Sep 17 00:00:00 2001 From: Alistair Smith Date: Wed, 4 Jun 2025 02:45:24 -0700 Subject: [PATCH] seems to work --- src/bun.js/bindings/webcore/Worker.cpp | 10 +++++----- src/bun.js/web_worker.zig | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/bun.js/bindings/webcore/Worker.cpp b/src/bun.js/bindings/webcore/Worker.cpp index 1e7626ce1b..a5767393c4 100644 --- a/src/bun.js/bindings/webcore/Worker.cpp +++ b/src/bun.js/bindings/webcore/Worker.cpp @@ -272,11 +272,11 @@ void Worker::terminate() // m_contextProxy.terminateWorkerGlobalScope(); m_terminationFlags.fetch_or(TerminateRequestedFlag); - if (ScriptExecutionContext::getScriptExecutionContext(m_clientIdentifier)) { - auto* impl = lifecycleHandle_; - lifecycleHandle_ = nullptr; - WebWorkerLifecycleHandle__requestTermination(impl); - } + // if (ScriptExecutionContext::getScriptExecutionContext(m_clientIdentifier)) { + // auto* impl = lifecycleHandle_; + // lifecycleHandle_ = nullptr; + // WebWorkerLifecycleHandle__requestTermination(impl); + // } } // const char* Worker::activeDOMObjectName() const diff --git a/src/bun.js/web_worker.zig b/src/bun.js/web_worker.zig index d9202314dd..87982931a9 100644 --- a/src/bun.js/web_worker.zig +++ b/src/bun.js/web_worker.zig @@ -448,6 +448,8 @@ fn unhandledError(this: *WebWorker, _: anyerror) void { } pub export fn WebWorkerLifecycleHandle__requestTermination(handle: *WebWorkerLifecycleHandle) void { + if (@intFromPtr(handle) == 0) return; + handle.requestTermination(); } @@ -700,24 +702,25 @@ const WebWorkerLifecycleHandle = struct { } else { self.mutex.unlock(); self.deref(); - self.deinit(); } self.deref(); } pub fn onTermination(self: *WebWorkerLifecycleHandle) void { + self.ref(); self.mutex.lock(); if (self.requested_terminate.swap(false, .acquire)) { // we already requested to terminate, therefore this handle has // already been consumed on the other thread and we are able to free // it. self.mutex.unlock(); - self.deinit(); + self.deref(); return; } self.worker = null; self.mutex.unlock(); + self.deref(); } };