SharedWorkerGlobalScope::close should not directly stop its thread
https://bugs.webkit.org/show_bug.cgi?id=241677
rdar://95010255

Patch by Youenn Fablet <youennf@gmail.com> on 2022-06-16
Reviewed by Alex Christensen.

We were directly stopping the worker thread when calling SharedWorkerGlobalScope::close.
This can trigger the case of stopping the worker thread concurrently from main thread and shared worker thread.
Instead, we reuse the existing Worker infastructure to tell its SharedWorkerThreadProxy to terminate.

* Source/WebCore/workers/WorkerOrWorkletThread.cpp:
(WebCore::WorkerOrWorkletThread::stop):
* Source/WebCore/workers/shared/SharedWorkerGlobalScope.cpp:
(WebCore::SharedWorkerGlobalScope::close): Deleted.
* Source/WebCore/workers/shared/SharedWorkerGlobalScope.h:
* Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.cpp:
(WebCore::SharedWorkerThreadProxy::workerGlobalScopeClosed):
* Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.h:

Canonical link: https://commits.webkit.org/251608@main

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@295603 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/workers/WorkerOrWorkletThread.cpp b/Source/WebCore/workers/WorkerOrWorkletThread.cpp
index 0a926c0..714ea1e0 100644
--- a/Source/WebCore/workers/WorkerOrWorkletThread.cpp
+++ b/Source/WebCore/workers/WorkerOrWorkletThread.cpp
@@ -239,6 +239,8 @@
 
 void WorkerOrWorkletThread::stop(Function<void()>&& stoppedCallback)
 {
+    ASSERT(isMainThread());
+
     // Mutex protection is necessary to ensure that m_workerGlobalScope isn't changed by
     // WorkerThread::workerThread() while we're accessing it. Note also that stop() can
     // be called before m_workerGlobalScope is fully created.
diff --git a/Source/WebCore/workers/shared/SharedWorkerGlobalScope.cpp b/Source/WebCore/workers/shared/SharedWorkerGlobalScope.cpp
index 9fe8db3..80676ca 100644
--- a/Source/WebCore/workers/shared/SharedWorkerGlobalScope.cpp
+++ b/Source/WebCore/workers/shared/SharedWorkerGlobalScope.cpp
@@ -55,12 +55,6 @@
     return static_cast<SharedWorkerThread&>(WorkerGlobalScope::thread());
 }
 
-void SharedWorkerGlobalScope::close()
-{
-    SCOPE_RELEASE_LOG("close:");
-    thread().stop(nullptr);
-}
-
 // https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker step 11.5
 void SharedWorkerGlobalScope::postConnectEvent(TransferredMessagePort&& transferredPort, const String& sourceOrigin)
 {
diff --git a/Source/WebCore/workers/shared/SharedWorkerGlobalScope.h b/Source/WebCore/workers/shared/SharedWorkerGlobalScope.h
index 03125ab..a353161 100644
--- a/Source/WebCore/workers/shared/SharedWorkerGlobalScope.h
+++ b/Source/WebCore/workers/shared/SharedWorkerGlobalScope.h
@@ -44,7 +44,6 @@
     SharedWorkerThread& thread();
 
     void postConnectEvent(TransferredMessagePort&&, const String& sourceOrigin);
-    void close();
 
 private:
     SharedWorkerGlobalScope(const String& name, const WorkerParameters&, Ref<SecurityOrigin>&&, SharedWorkerThread&, Ref<SecurityOrigin>&& topOrigin, IDBClient::IDBConnectionProxy*, SocketProvider*);
diff --git a/Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.cpp b/Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.cpp
index a17cea2..2e2fc22 100644
--- a/Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.cpp
+++ b/Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.cpp
@@ -192,4 +192,11 @@
         proxy->notifyNetworkStateChange(isOnLine);
 }
 
+void SharedWorkerThreadProxy::workerGlobalScopeClosed()
+{
+    callOnMainThread([identifier = thread().identifier()] {
+        SharedWorkerContextManager::singleton().stopSharedWorker(identifier);
+    });
+}
+
 } // namespace WebCore
diff --git a/Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.h b/Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.h
index ddef7be..c540157 100644
--- a/Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.h
+++ b/Source/WebCore/workers/shared/context/SharedWorkerThreadProxy.h
@@ -66,6 +66,7 @@
     void postExceptionToWorkerObject(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL) final;
     void postMessageToWorkerObject(MessageWithMessagePorts&&) final { }
     void workerGlobalScopeDestroyed() final { }
+    void workerGlobalScopeClosed() final;
     void confirmMessageFromWorkerObject(bool) final { }
     void reportPendingActivity(bool) final { }