<!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>[186006] 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/186006">186006</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2015-06-26 12:21:02 -0700 (Fri, 26 Jun 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Network process hangs fetching disk cache entries
https://bugs.webkit.org/show_bug.cgi?id=146348
&lt;rdar://problem/21528072&gt;

Reviewed by Anders Carlsson.

RunLoop::dispatch may deadlock if invoked with std::function that captures an object that calls RunLoop::dispatch in destructor.

* wtf/RunLoop.cpp:
(WTF::RunLoop::performWork):

    Don't reuse std::function variable in loop. We may end up destroying previously held std::function in assignment
    while holding the runloop mutex. With this change std::function is always destroyed with mutex unlocked.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfRunLoopcpp">trunk/Source/WTF/wtf/RunLoop.cpp</a></li>
<li><a href="#trunkToolsTestWebKitAPITestWebKitAPIxcodeprojprojectpbxproj">trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkToolsTestWebKitAPITestsWTFRunLoopcpp">trunk/Tools/TestWebKitAPI/Tests/WTF/RunLoop.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (186005 => 186006)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2015-06-26 18:26:19 UTC (rev 186005)
+++ trunk/Source/WTF/ChangeLog        2015-06-26 19:21:02 UTC (rev 186006)
</span><span class="lines">@@ -1,3 +1,19 @@
</span><ins>+2015-06-26  Antti Koivisto  &lt;antti@apple.com&gt;
+
+        Network process hangs fetching disk cache entries
+        https://bugs.webkit.org/show_bug.cgi?id=146348
+        &lt;rdar://problem/21528072&gt;
+
+        Reviewed by Anders Carlsson.
+
+        RunLoop::dispatch may deadlock if invoked with std::function that captures an object that calls RunLoop::dispatch in destructor.
+
+        * wtf/RunLoop.cpp:
+        (WTF::RunLoop::performWork):
+
+            Don't reuse std::function variable in loop. We may end up destroying previously held std::function in assignment
+            while holding the runloop mutex. With this change std::function is always destroyed with mutex unlocked.
+
</ins><span class="cx"> 2015-06-24  Commit Queue  &lt;commit-queue@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, rolling out r185906.
</span></span></pre></div>
<a id="trunkSourceWTFwtfRunLoopcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/RunLoop.cpp (186005 => 186006)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/RunLoop.cpp        2015-06-26 18:26:19 UTC (rev 186005)
+++ trunk/Source/WTF/wtf/RunLoop.cpp        2015-06-26 19:21:02 UTC (rev 186006)
</span><span class="lines">@@ -88,22 +88,24 @@
</span><span class="cx">     // By only handling up to the number of functions that were in the queue when performWork() is called
</span><span class="cx">     // we guarantee to occasionally return from the run loop so other event sources will be allowed to spin.
</span><span class="cx"> 
</span><del>-    std::function&lt;void()&gt; function;
</del><span class="cx">     size_t functionsToHandle = 0;
</span><del>-
</del><span class="cx">     {
</span><del>-        MutexLocker locker(m_functionQueueLock);
-        functionsToHandle = m_functionQueue.size();
</del><ins>+        std::function&lt;void()&gt; function;
+        {
+            MutexLocker locker(m_functionQueueLock);
+            functionsToHandle = m_functionQueue.size();
</ins><span class="cx"> 
</span><del>-        if (m_functionQueue.isEmpty())
-            return;
</del><ins>+            if (m_functionQueue.isEmpty())
+                return;
</ins><span class="cx"> 
</span><del>-        function = m_functionQueue.takeFirst();
</del><ins>+            function = m_functionQueue.takeFirst();
+        }
+
+        function();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    function();
-
</del><span class="cx">     for (size_t functionsHandled = 1; functionsHandled &lt; functionsToHandle; ++functionsHandled) {
</span><ins>+        std::function&lt;void()&gt; function;
</ins><span class="cx">         {
</span><span class="cx">             MutexLocker locker(m_functionQueueLock);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestWebKitAPIxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (186005 => 186006)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj        2015-06-26 18:26:19 UTC (rev 186005)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj        2015-06-26 19:21:02 UTC (rev 186006)
</span><span class="lines">@@ -299,6 +299,7 @@
</span><span class="cx">                 E194E1BD177E53C7009C4D4E /* StopLoadingFromDidReceiveResponse.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = E194E1BC177E534A009C4D4E /* StopLoadingFromDidReceiveResponse.html */; };
</span><span class="cx">                 E19DB9791B32137C00DB38D4 /* NavigatorLanguage.mm in Sources */ = {isa = PBXBuildFile; fileRef = E19DB9781B32137C00DB38D4 /* NavigatorLanguage.mm */; };
</span><span class="cx">                 E40019331ACE9B88001B0A2A /* BloomFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E40019301ACE9B5C001B0A2A /* BloomFilter.cpp */; };
</span><ins>+                E4C9ABC91B3DB2C40040A987 /* RunLoop.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4C9ABC71B3DB1710040A987 /* RunLoop.cpp */; };
</ins><span class="cx">                 F660AA1115A5F631003A1243 /* GetInjectedBundleInitializationUserDataCallback_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F660AA0F15A5F624003A1243 /* GetInjectedBundleInitializationUserDataCallback_Bundle.cpp */; };
</span><span class="cx">                 F660AA1515A61ABF003A1243 /* InjectedBundleInitializationUserDataCallbackWins_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F660AA1415A61ABF003A1243 /* InjectedBundleInitializationUserDataCallbackWins_Bundle.cpp */; };
</span><span class="cx">                 F6B7BE9517469212008A3445 /* DidAssociateFormControls_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6B7BE92174691EF008A3445 /* DidAssociateFormControls_Bundle.cpp */; };
</span><span class="lines">@@ -727,6 +728,7 @@
</span><span class="cx">                 E40019301ACE9B5C001B0A2A /* BloomFilter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BloomFilter.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 E490296714E2E3A4002BEDD1 /* TypingStyleCrash.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TypingStyleCrash.mm; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 E4A757D3178AEA5B00B5D7A4 /* Deque.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Deque.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                E4C9ABC71B3DB1710040A987 /* RunLoop.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RunLoop.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 F3FC3EE213678B7300126A65 /* libgtest.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libgtest.a; sourceTree = BUILT_PRODUCTS_DIR; };
</span><span class="cx">                 F660AA0C15A5F061003A1243 /* GetInjectedBundleInitializationUserDataCallback.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GetInjectedBundleInitializationUserDataCallback.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 F660AA0F15A5F624003A1243 /* GetInjectedBundleInitializationUserDataCallback_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GetInjectedBundleInitializationUserDataCallback_Bundle.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -1096,6 +1098,7 @@
</span><span class="cx">                                 86BD19971A2DB05B006DCF0A /* RefCounter.cpp */,
</span><span class="cx">                                 93A427AD180DA60F00CD24D7 /* RefLogger.h */,
</span><span class="cx">                                 93A427A8180D9B0700CD24D7 /* RefPtr.cpp */,
</span><ins>+                                E4C9ABC71B3DB1710040A987 /* RunLoop.cpp */,
</ins><span class="cx">                                 14F3B11215E45EAB00210069 /* SaturatedArithmeticOperations.cpp */,
</span><span class="cx">                                 CD5393C91757BAC400C07123 /* SHA1.cpp */,
</span><span class="cx">                                 81B50192140F232300D9EB58 /* StringBuilder.cpp */,
</span><span class="lines">@@ -1527,6 +1530,7 @@
</span><span class="cx">                                 7CCE7F4C1A411B9800447C4C /* OSObjectPtr.cpp in Sources */,
</span><span class="cx">                                 7CCE7F071A411AE600447C4C /* PageLoadBasic.cpp in Sources */,
</span><span class="cx">                                 7CCE7F081A411AE600447C4C /* PageLoadDidChangeLocationWithinPageForFrame.cpp in Sources */,
</span><ins>+                                E4C9ABC91B3DB2C40040A987 /* RunLoop.cpp in Sources */,
</ins><span class="cx">                                 7CCE7EC71A411A7E00447C4C /* PageVisibilityStateWithWindowChanges.mm in Sources */,
</span><span class="cx">                                 7CCE7F091A411AE600447C4C /* ParentFrame.cpp in Sources */,
</span><span class="cx">                                 7CCE7F0A1A411AE600447C4C /* PasteboardNotifications.mm in Sources */,
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWTFRunLoopcpp"></a>
<div class="addfile"><h4>Added: trunk/Tools/TestWebKitAPI/Tests/WTF/RunLoop.cpp (0 => 186006)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WTF/RunLoop.cpp                                (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/RunLoop.cpp        2015-06-26 19:21:02 UTC (rev 186006)
</span><span class="lines">@@ -0,0 +1,57 @@
</span><ins>+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include &quot;config.h&quot;
+
+#include &quot;PlatformUtilities.h&quot;
+#include &lt;wtf/RunLoop.h&gt;
+
+namespace TestWebKitAPI {
+
+static bool testFinished;
+static int count = 100;
+
+TEST(WTF_RunLoop, Deadlock)
+{
+    RunLoop::initializeMainRunLoop();
+
+    struct DispatchFromDestructorTester {
+        ~DispatchFromDestructorTester() {
+            RunLoop::main().dispatch([] {
+                if (!(--count))
+                    testFinished = true;
+            });
+        }
+    };
+
+    for (int i = 0; i &lt; count; ++i) {
+        auto capture = std::make_shared&lt;DispatchFromDestructorTester&gt;();
+        RunLoop::main().dispatch([capture] { });
+    }
+
+    Util::run(&amp;testFinished);
+}
+
+} // namespace TestWebKitAPI
</ins></span></pre>
</div>
</div>

</body>
</html>