Title: [287679] trunk/Source/WebCore
Revision
287679
Author
[email protected]
Date
2022-01-06 02:50:00 -0800 (Thu, 06 Jan 2022)

Log Message

WorkerMessagePortChannelProvider::takeAllMessagesForPort should guarantee execution of the takeMessagePort callback
https://bugs.webkit.org/show_bug.cgi?id=234883
<rdar://86708232>

Reviewed by Alex Christensen.

In case we enqueue a worker task, and before the task is executed, the worker is terminated,
the task will not be executed and will be destroyed in worker thread.
Instead, we should make sure to execute the callback in main thread.
Introduce MainThreadCompletionHandler for that purpose.
Covered by existing tests.

* dom/messageports/WorkerMessagePortChannelProvider.cpp:
(WebCore::MainThreadCompletionHandler::MainThreadCompletionHandler):
(WebCore::MainThreadCompletionHandler::~MainThreadCompletionHandler):
(WebCore::MainThreadCompletionHandler::complete):
(WebCore::WorkerMessagePortChannelProvider::takeAllMessagesForPort):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (287678 => 287679)


--- trunk/Source/WebCore/ChangeLog	2022-01-06 10:46:20 UTC (rev 287678)
+++ trunk/Source/WebCore/ChangeLog	2022-01-06 10:50:00 UTC (rev 287679)
@@ -1,3 +1,23 @@
+2022-01-06  Youenn Fablet  <[email protected]>
+
+        WorkerMessagePortChannelProvider::takeAllMessagesForPort should guarantee execution of the takeMessagePort callback
+        https://bugs.webkit.org/show_bug.cgi?id=234883
+        <rdar://86708232>
+
+        Reviewed by Alex Christensen.
+
+        In case we enqueue a worker task, and before the task is executed, the worker is terminated,
+        the task will not be executed and will be destroyed in worker thread.
+        Instead, we should make sure to execute the callback in main thread.
+        Introduce MainThreadCompletionHandler for that purpose.
+        Covered by existing tests.
+
+        * dom/messageports/WorkerMessagePortChannelProvider.cpp:
+        (WebCore::MainThreadCompletionHandler::MainThreadCompletionHandler):
+        (WebCore::MainThreadCompletionHandler::~MainThreadCompletionHandler):
+        (WebCore::MainThreadCompletionHandler::complete):
+        (WebCore::WorkerMessagePortChannelProvider::takeAllMessagesForPort):
+
 2022-01-05  Antoine Quint  <[email protected]>
 
         computed style for transition longhand properties is wrong

Modified: trunk/Source/WebCore/dom/messageports/WorkerMessagePortChannelProvider.cpp (287678 => 287679)


--- trunk/Source/WebCore/dom/messageports/WorkerMessagePortChannelProvider.cpp	2022-01-06 10:46:20 UTC (rev 287678)
+++ trunk/Source/WebCore/dom/messageports/WorkerMessagePortChannelProvider.cpp	2022-01-06 10:50:00 UTC (rev 287679)
@@ -86,6 +86,30 @@
     });
 }
 
+class MainThreadCompletionHandler {
+public:
+    explicit MainThreadCompletionHandler(CompletionHandler<void()>&& completionHandler)
+        : m_completionHandler(WTFMove(completionHandler))
+    {
+    }
+    MainThreadCompletionHandler(MainThreadCompletionHandler&&) = default;
+    MainThreadCompletionHandler& operator=(MainThreadCompletionHandler&&) = default;
+
+    ~MainThreadCompletionHandler()
+    {
+        if (m_completionHandler)
+            complete();
+    }
+
+    void complete()
+    {
+        callOnMainThread(WTFMove(m_completionHandler));
+    }
+
+private:
+    CompletionHandler<void()> m_completionHandler;
+};
+
 void WorkerMessagePortChannelProvider::takeAllMessagesForPort(const MessagePortIdentifier& identifier, CompletionHandler<void(Vector<MessageWithMessagePorts>&&, Function<void()>&&)>&& callback)
 {
     uint64_t callbackIdentifier = ++m_lastCallbackIdentifier;
@@ -93,9 +117,9 @@
 
     callOnMainThread([this, workerThread = RefPtr { m_scope.workerOrWorkletThread() }, callbackIdentifier, identifier]() mutable {
         MessagePortChannelProvider::singleton().takeAllMessagesForPort(identifier, [this, workerThread = WTFMove(workerThread), callbackIdentifier](Vector<MessageWithMessagePorts>&& messages, Function<void()>&& completionHandler) {
-            workerThread->runLoop().postTaskForMode([this, callbackIdentifier, messages = WTFMove(messages), completionHandler = WTFMove(completionHandler)](auto&) mutable {
+            workerThread->runLoop().postTaskForMode([this, callbackIdentifier, messages = WTFMove(messages), completionHandler = MainThreadCompletionHandler(WTFMove(completionHandler))](auto&) mutable {
                 m_takeAllMessagesCallbacks.take(callbackIdentifier)(WTFMove(messages), [completionHandler = WTFMove(completionHandler)]() mutable {
-                    callOnMainThread(WTFMove(completionHandler));
+                    completionHandler.complete();
                 });
             }, WorkerRunLoop::defaultMode());
         });
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to