<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[279115] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/279115">279115</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2021-06-22 06:25:03 -0700 (Tue, 22 Jun 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Functions dispatched to WorkQueue are sometimes destroyed in the calling thread due to block refcounting
https://bugs.webkit.org/show_bug.cgi?id=227160

Patch by Kimmo Kinnunen <kkinnunen@apple.com> on 2021-06-22
Reviewed by Antti Koivisto.

Source/WebKit:

As an example, remove one workaround mutability of dispatched function
and nullptr assignment that was needed for working around
WorkQueue::dispatch bug where the dispatched function was not always
destroyed in the queue.

* Shared/mac/MediaFormatReader/MediaTrackReader.cpp:
(WebKit::MediaTrackReader::finalize):

Source/WTF:

WorkQueue::dispatch Function bodies always execute in the thread of target
queue. However, sometimes the Function destruction would happen in the
dispatched-from thread. This is not what the WorkQueue::dispatch callers
expect.

Implement some of the WorkQueue::dispatch* in terms of dispatch_..._f()
variants which take a function and context pointer instead of a block.

Blocks are reference counted objects without the ability to pass the
ownership of the reference. For dispatch case, it means that caller will
need to hold the reference while dispatch_...() returns. In thread
contention cases the called block might complete and reference be
dropped in the thread of the queue before the dispatched-from thread
would drop its reference to the block. This would cause the dispatched
Function to be destroyed in the dispatched-from thread.

* wtf/cocoa/WorkQueueCocoa.cpp:
(WTF::dispatchWorkItem):
(WTF::WorkQueue::dispatch):
(WTF::WorkQueue::dispatchAfter):
(WTF::WorkQueue::dispatchSync):

Tools:

Test that the Function passed to WorkQueue::dispatch
is always destroyed in the WorkQueue. Test by using the
knowledge that WorkQueues use threads.
Start up many WorkQueues to create more thread contention
to ensure that the dispatched-from thread is sometimes not
run while the thread of the queue finishes processing
the dispatch.

* TestWebKitAPI/Tests/WTF/WorkQueue.cpp:
(TestWebKitAPI::TEST):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfcocoaWorkQueueCocoacpp">trunk/Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp</a></li>
<li><a href="#trunkSourceWebKitChangeLog">trunk/Source/WebKit/ChangeLog</a></li>
<li><a href="#trunkSourceWebKitSharedmacMediaFormatReaderMediaTrackReadercpp">trunk/Source/WebKit/Shared/mac/MediaFormatReader/MediaTrackReader.cpp</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWTFWorkQueuecpp">trunk/Tools/TestWebKitAPI/Tests/WTF/WorkQueue.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (279114 => 279115)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog       2021-06-22 13:01:51 UTC (rev 279114)
+++ trunk/Source/WTF/ChangeLog  2021-06-22 13:25:03 UTC (rev 279115)
</span><span class="lines">@@ -1,3 +1,32 @@
</span><ins>+2021-06-22  Kimmo Kinnunen  <kkinnunen@apple.com>
+
+        Functions dispatched to WorkQueue are sometimes destroyed in the calling thread due to block refcounting
+        https://bugs.webkit.org/show_bug.cgi?id=227160
+
+        Reviewed by Antti Koivisto.
+
+        WorkQueue::dispatch Function bodies always execute in the thread of target
+        queue. However, sometimes the Function destruction would happen in the
+        dispatched-from thread. This is not what the WorkQueue::dispatch callers
+        expect.
+
+        Implement some of the WorkQueue::dispatch* in terms of dispatch_..._f()
+        variants which take a function and context pointer instead of a block.
+
+        Blocks are reference counted objects without the ability to pass the
+        ownership of the reference. For dispatch case, it means that caller will
+        need to hold the reference while dispatch_...() returns. In thread
+        contention cases the called block might complete and reference be
+        dropped in the thread of the queue before the dispatched-from thread
+        would drop its reference to the block. This would cause the dispatched
+        Function to be destroyed in the dispatched-from thread.
+
+        * wtf/cocoa/WorkQueueCocoa.cpp:
+        (WTF::dispatchWorkItem):
+        (WTF::WorkQueue::dispatch):
+        (WTF::WorkQueue::dispatchAfter):
+        (WTF::WorkQueue::dispatchSync):
+
</ins><span class="cx"> 2021-06-21  Chris Dumez  <cdumez@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Adjust `fetch` port blocking for ports 990, 989
</span></span></pre></div>
<a id="trunkSourceWTFwtfcocoaWorkQueueCocoacpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp (279114 => 279115)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp    2021-06-22 13:01:51 UTC (rev 279114)
+++ trunk/Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp       2021-06-22 13:25:03 UTC (rev 279115)
</span><span class="lines">@@ -31,23 +31,37 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WTF {
</span><span class="cx"> 
</span><ins>+namespace {
+
+struct DispatchWorkItem {
+    WTF_MAKE_STRUCT_FAST_ALLOCATED;
+    Ref<WorkQueue> m_workQueue;
+    Function<void()> m_function;
+    void operator()() { m_function(); }
+};
+
+}
+
+template<typename T> static void dispatchWorkItem(void* dispatchContext)
+{
+    T* item = reinterpret_cast<T*>(dispatchContext);
+    (*item)();
+    delete item;
+}
+
</ins><span class="cx"> void WorkQueue::dispatch(Function<void()>&& function)
</span><span class="cx"> {
</span><del>-    dispatch_async(m_dispatchQueue.get(), makeBlockPtr([protectedThis = makeRef(*this), function = WTFMove(function)] {
-        function();
-    }).get());
</del><ins>+    dispatch_async_f(m_dispatchQueue.get(), new DispatchWorkItem { makeRef(*this), WTFMove(function) }, dispatchWorkItem<DispatchWorkItem>);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WorkQueue::dispatchAfter(Seconds duration, Function<void()>&& function)
</span><span class="cx"> {
</span><del>-    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration.nanosecondsAs<int64_t>()), m_dispatchQueue.get(), makeBlockPtr([protectedThis = makeRef(*this), function = WTFMove(function)] {
-        function();
-    }).get());
</del><ins>+    dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, duration.nanosecondsAs<int64_t>()), m_dispatchQueue.get(), new DispatchWorkItem { makeRef(*this),  WTFMove(function) }, dispatchWorkItem<DispatchWorkItem>);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WorkQueue::dispatchSync(Function<void()>&& function)
</span><span class="cx"> {
</span><del>-    dispatch_sync(m_dispatchQueue.get(), makeBlockPtr(WTFMove(function)).get());
</del><ins>+    dispatch_sync_f(m_dispatchQueue.get(), new Function<void()> { WTFMove(function) }, dispatchWorkItem<Function<void()>>);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> Ref<WorkQueue> WorkQueue::constructMainWorkQueue()
</span></span></pre></div>
<a id="trunkSourceWebKitChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/ChangeLog (279114 => 279115)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/ChangeLog    2021-06-22 13:01:51 UTC (rev 279114)
+++ trunk/Source/WebKit/ChangeLog       2021-06-22 13:25:03 UTC (rev 279115)
</span><span class="lines">@@ -1,3 +1,18 @@
</span><ins>+2021-06-22  Kimmo Kinnunen  <kkinnunen@apple.com>
+
+        Functions dispatched to WorkQueue are sometimes destroyed in the calling thread due to block refcounting
+        https://bugs.webkit.org/show_bug.cgi?id=227160
+
+        Reviewed by Antti Koivisto.
+
+        As an example, remove one workaround mutability of dispatched function
+        and nullptr assignment that was needed for working around
+        WorkQueue::dispatch bug where the dispatched function was not always
+        destroyed in the queue.
+
+        * Shared/mac/MediaFormatReader/MediaTrackReader.cpp:
+        (WebKit::MediaTrackReader::finalize):
+
</ins><span class="cx"> 2021-06-22  Myles C. Maxfield  <mmaxfield@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [Cocoa] Force a copy of font data when receiving it from the untrusted web process
</span></span></pre></div>
<a id="trunkSourceWebKitSharedmacMediaFormatReaderMediaTrackReadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/Shared/mac/MediaFormatReader/MediaTrackReader.cpp (279114 => 279115)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/Shared/mac/MediaFormatReader/MediaTrackReader.cpp    2021-06-22 13:01:51 UTC (rev 279114)
+++ trunk/Source/WebKit/Shared/mac/MediaFormatReader/MediaTrackReader.cpp       2021-06-22 13:25:03 UTC (rev 279115)
</span><span class="lines">@@ -203,9 +203,7 @@
</span><span class="cx"> void MediaTrackReader::finalize()
</span><span class="cx"> {
</span><span class="cx">     Locker locker { m_sampleStorageLock };
</span><del>-    storageQueue().dispatch([sampleStorage = std::exchange(m_sampleStorage, nullptr)]() mutable {
-        sampleStorage = nullptr;
-    });
</del><ins>+    storageQueue().dispatch([sampleStorage = std::exchange(m_sampleStorage, nullptr)] { });
</ins><span class="cx">     CoreMediaWrapped<MediaTrackReader>::finalize();
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (279114 => 279115)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2021-06-22 13:01:51 UTC (rev 279114)
+++ trunk/Tools/ChangeLog       2021-06-22 13:25:03 UTC (rev 279115)
</span><span class="lines">@@ -1,5 +1,23 @@
</span><span class="cx"> 2021-06-22  Kimmo Kinnunen  <kkinnunen@apple.com>
</span><span class="cx"> 
</span><ins>+        Functions dispatched to WorkQueue are sometimes destroyed in the calling thread due to block refcounting
+        https://bugs.webkit.org/show_bug.cgi?id=227160
+
+        Reviewed by Antti Koivisto.
+
+        Test that the Function passed to WorkQueue::dispatch
+        is always destroyed in the WorkQueue. Test by using the
+        knowledge that WorkQueues use threads.
+        Start up many WorkQueues to create more thread contention
+        to ensure that the dispatched-from thread is sometimes not
+        run while the thread of the queue finishes processing
+        the dispatch.
+
+        * TestWebKitAPI/Tests/WTF/WorkQueue.cpp:
+        (TestWebKitAPI::TEST):
+
+2021-06-22  Kimmo Kinnunen  <kkinnunen@apple.com>
+
</ins><span class="cx">         Test runner parses the names of value parametrised GTEST tests wrong
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=227207
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWTFWorkQueuecpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/WorkQueue.cpp (279114 => 279115)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WTF/WorkQueue.cpp        2021-06-22 13:01:51 UTC (rev 279114)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/WorkQueue.cpp   2021-06-22 13:25:03 UTC (rev 279115)
</span><span class="lines">@@ -30,6 +30,7 @@
</span><span class="cx"> #include <wtf/Lock.h>
</span><span class="cx"> #include <wtf/Vector.h>
</span><span class="cx"> #include <wtf/WorkQueue.h>
</span><ins>+#include <memory>
</ins><span class="cx"> #include <string>
</span><span class="cx"> #include <thread>
</span><span class="cx"> 
</span><span class="lines">@@ -41,7 +42,7 @@
</span><span class="cx"> static const char* longTestLabel = "longTest";
</span><span class="cx"> static const char* thirdTestLabel = "thirdTest";
</span><span class="cx"> static const char* dispatchAfterLabel = "dispatchAfter";
</span><del>-    
</del><ins>+
</ins><span class="cx"> TEST(WTF_WorkQueue, Simple)
</span><span class="cx"> {
</span><span class="cx">     Lock m_lock;
</span><span class="lines">@@ -80,7 +81,7 @@
</span><span class="cx">         EXPECT_TRUE(calledSimpleTest);
</span><span class="cx">         EXPECT_TRUE(calledLongTest);
</span><span class="cx">         EXPECT_TRUE(calledThirdTest);
</span><del>-        
</del><ins>+
</ins><span class="cx">         m_testCompleted.notifyOne();
</span><span class="cx">     });
</span><span class="cx"> 
</span><span class="lines">@@ -107,7 +108,7 @@
</span><span class="cx">     bool calledSimpleTest = false;
</span><span class="cx">     bool calledLongTest = false;
</span><span class="cx">     bool calledThirdTest = false;
</span><del>-    
</del><ins>+
</ins><span class="cx">     auto queue1 = WorkQueue::create("com.apple.WebKit.Test.twoQueues1");
</span><span class="cx">     auto queue2 = WorkQueue::create("com.apple.WebKit.Test.twoQueues2");
</span><span class="cx"> 
</span><span class="lines">@@ -115,7 +116,7 @@
</span><span class="cx">     EXPECT_EQ(1U, queue2->refCount());
</span><span class="cx"> 
</span><span class="cx">     Locker locker { m_lock };
</span><del>-    
</del><ins>+
</ins><span class="cx">     queue1->dispatch([&](void) {
</span><span class="cx">         m_functionCallOrder.append(simpleTestLabel);
</span><span class="cx">         calledSimpleTest = true;
</span><span class="lines">@@ -138,7 +139,7 @@
</span><span class="cx">         Locker locker { m_lock };
</span><span class="cx">         m_functionCallOrder.append(thirdTestLabel);
</span><span class="cx">         calledThirdTest = true;
</span><del>-        
</del><ins>+
</ins><span class="cx">         m_testQueue1Completed.notifyOne();
</span><span class="cx">     });
</span><span class="cx"> 
</span><span class="lines">@@ -191,7 +192,7 @@
</span><span class="cx"> 
</span><span class="cx">     EXPECT_TRUE(calledSimpleTest);
</span><span class="cx">     EXPECT_FALSE(calledDispatchAfterTest);
</span><del>-    
</del><ins>+
</ins><span class="cx">     m_dispatchAfterTestCompleted.wait(m_lock);
</span><span class="cx"> 
</span><span class="cx">     EXPECT_TRUE(calledSimpleTest);
</span><span class="lines">@@ -274,4 +275,40 @@
</span><span class="cx">     EXPECT_TRUE(secondSyncTaskTaskRan);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+// Tests that the Function passed to WorkQueue::dispatch is destructed on the thread that
+// runs the Function. It is a common pattern to capture a owning reference into a Function
+// and dispatch that to a queue to ensure ordering (or thread affinity) of the object destruction.
+TEST(WTF_WorkQueue, DestroyDispatchedOnDispatchQueue)
+{
+    std::atomic<size_t> counter = 0;
+    class DestructionWorkQueueTester {
+    public:
+        DestructionWorkQueueTester(std::atomic<size_t>& counter)
+            : m_counter(counter)
+        {
+        }
+        ~DestructionWorkQueueTester()
+        {
+            EXPECT_NE(m_createdInThread, Thread::current().uid());
+            m_counter++;
+        }
+    private:
+        uint32_t m_createdInThread = Thread::current().uid();
+        std::atomic<size_t>& m_counter;
+    };
+    constexpr size_t queueCount = 50;
+    constexpr size_t iterationCount = 10000;
+    RefPtr<WorkQueue> queue[queueCount];
+    for (size_t i = 0; i < queueCount; ++i)
+        queue[i] = WorkQueue::create("com.apple.WebKit.Test.destroyDispatchedOnDispatchQueue", WorkQueue::Type::Serial, WorkQueue::QOS::UserInteractive);
+
+    for (size_t i = 0; i < iterationCount; ++i) {
+        for (size_t j = 0; j < queueCount; ++j)
+            queue[j]->dispatch([instance = std::make_unique<DestructionWorkQueueTester>(counter)]() { }); // NOLINT
+    }
+    for (size_t j = 0; j < queueCount; ++j)
+        queue[j]->dispatchSync([] { });
+    EXPECT_EQ(queueCount * iterationCount, counter);
+
+}
</ins><span class="cx"> } // namespace TestWebKitAPI
</span></span></pre>
</div>
</div>

</body>
</html>