<!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>[244684] 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/244684">244684</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2019-04-26 02:52:49 -0700 (Fri, 26 Apr 2019)</dd>
</dl>

<h3>Log Message</h3>
<pre>[Curl] Fix Curl Request Scheduler not to release wrong Curl handle when request is cancelled.
https://bugs.webkit.org/show_bug.cgi?id=191650

Patch by Takashi Komori <Takashi.Komori@sony.com> on 2019-04-26
Reviewed by Fujii Hironori.

Source/WebCore:

Test: http/tests/misc/repeat-open-cancel.html

* platform/network/curl/CurlRequest.cpp:
(WebCore::CurlRequest::cancel):
(WebCore::CurlRequest::isCancelled):
(WebCore::CurlRequest::isCompletedOrCancelled):
(WebCore::CurlRequest::didCompleteTransfer):
(WebCore::CurlRequest::completeDidReceiveResponse):
(WebCore::CurlRequest::pausedStatusChanged):
* platform/network/curl/CurlRequest.h:
(WebCore::CurlRequest::isCompleted const): Deleted.
(WebCore::CurlRequest::isCancelled const): Deleted.
(WebCore::CurlRequest::isCompletedOrCancelled const): Deleted.
* platform/network/curl/CurlRequestScheduler.cpp:
(WebCore::CurlRequestScheduler::cancel):
(WebCore::CurlRequestScheduler::callOnWorkerThread):
(WebCore::CurlRequestScheduler::startThreadIfNeeded):
(WebCore::CurlRequestScheduler::stopThreadIfNoMoreJobRunning):
(WebCore::CurlRequestScheduler::stopThread):
(WebCore::CurlRequestScheduler::executeTasks):
(WebCore::CurlRequestScheduler::workerThread):
(WebCore::CurlRequestScheduler::startTransfer):
(WebCore::CurlRequestScheduler::completeTransfer):
(WebCore::CurlRequestScheduler::cancelTransfer):
(WebCore::CurlRequestScheduler::finalizeTransfer):
* platform/network/curl/CurlRequestScheduler.h:

LayoutTests:

* http/tests/misc/repeat-open-cancel-expected.txt: Added.
* http/tests/misc/repeat-open-cancel.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkcurlCurlRequestcpp">trunk/Source/WebCore/platform/network/curl/CurlRequest.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkcurlCurlRequesth">trunk/Source/WebCore/platform/network/curl/CurlRequest.h</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkcurlCurlRequestSchedulercpp">trunk/Source/WebCore/platform/network/curl/CurlRequestScheduler.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkcurlCurlRequestSchedulerh">trunk/Source/WebCore/platform/network/curl/CurlRequestScheduler.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestshttptestsmiscrepeatopencancelexpectedtxt">trunk/LayoutTests/http/tests/misc/repeat-open-cancel-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestsmiscrepeatopencancelhtml">trunk/LayoutTests/http/tests/misc/repeat-open-cancel.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (244683 => 244684)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog      2019-04-26 06:39:30 UTC (rev 244683)
+++ trunk/LayoutTests/ChangeLog 2019-04-26 09:52:49 UTC (rev 244684)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2019-04-26  Takashi Komori  <Takashi.Komori@sony.com>
+
+        [Curl] Fix Curl Request Scheduler not to release wrong Curl handle when request is cancelled.
+        https://bugs.webkit.org/show_bug.cgi?id=191650
+
+        Reviewed by Fujii Hironori.
+
+        * http/tests/misc/repeat-open-cancel-expected.txt: Added.
+        * http/tests/misc/repeat-open-cancel.html: Added.
+
</ins><span class="cx"> 2019-04-25  Myles C. Maxfield  <mmaxfield@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [iOS] Implement idempotent mode for text autosizing
</span></span></pre></div>
<a id="trunkLayoutTestshttptestsmiscrepeatopencancelexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/misc/repeat-open-cancel-expected.txt (0 => 244684)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/misc/repeat-open-cancel-expected.txt                                (rev 0)
+++ trunk/LayoutTests/http/tests/misc/repeat-open-cancel-expected.txt   2019-04-26 09:52:49 UTC (rev 244684)
</span><span class="lines">@@ -0,0 +1,5 @@
</span><ins>+This is a test for https://bugs.webkit.org/show_bug.cgi?id=191650.
+
+In case that several requests are closed and created pretty fast, a result of old request sometimes goes to a wrong request.
+
+PASS
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestsmiscrepeatopencancelhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/misc/repeat-open-cancel.html (0 => 244684)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/misc/repeat-open-cancel.html                                (rev 0)
+++ trunk/LayoutTests/http/tests/misc/repeat-open-cancel.html   2019-04-26 09:52:49 UTC (rev 244684)
</span><span class="lines">@@ -0,0 +1,52 @@
</span><ins>+<html>
+<head>
+<script>
+var repeatCount = 0;
+
+function sendRequest() {
+    var firstReq = new XMLHttpRequest();
+    firstReq.open("GET", "/resources/download-json-with-delay.php?iteration=100&delay=1");
+
+    firstReq.onreadystatechange = function() {
+        if (firstReq.readyState == firstReq.HEADERS_RECEIVED) {
+            var secondReq = new XMLHttpRequest();
+            secondReq.open("GET", "/resources/download-json-with-delay.php?iteration=10&delay=0");
+
+            secondReq.onload = function() {
+                if (repeatCount++ < 40)
+                    sendRequest();
+                else {
+                    document.getElementById('result').innerText = "PASS";
+                    testRunner.notifyDone();
+                }
+            }
+
+            secondReq.onerror = function() {
+                document.getElementById('result').innerText = "FAIL";
+                testRunner.notifyDone();
+            }
+
+            firstReq.abort();
+            secondReq.send();
+        }
+    }
+
+    firstReq.send();
+}
+
+function runTest() {
+    if (window.testRunner) {
+        testRunner.waitUntilDone();
+        testRunner.dumpAsText();
+    }
+
+    sendRequest();
+}
+</script>
+</head>
+<body onload="runTest()">
+<p>This is a test for https://bugs.webkit.org/show_bug.cgi?id=191650.</p>
+<p>In case that several requests are closed and created pretty fast, a result of old request sometimes goes to a wrong request.</p>
+<p id="result">RUNNING</p>
+</body>
+</html>
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (244683 => 244684)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2019-04-26 06:39:30 UTC (rev 244683)
+++ trunk/Source/WebCore/ChangeLog      2019-04-26 09:52:49 UTC (rev 244684)
</span><span class="lines">@@ -1,3 +1,37 @@
</span><ins>+2019-04-26  Takashi Komori  <Takashi.Komori@sony.com>
+
+        [Curl] Fix Curl Request Scheduler not to release wrong Curl handle when request is cancelled.
+        https://bugs.webkit.org/show_bug.cgi?id=191650
+
+        Reviewed by Fujii Hironori.
+
+        Test: http/tests/misc/repeat-open-cancel.html
+
+        * platform/network/curl/CurlRequest.cpp:
+        (WebCore::CurlRequest::cancel):
+        (WebCore::CurlRequest::isCancelled):
+        (WebCore::CurlRequest::isCompletedOrCancelled):
+        (WebCore::CurlRequest::didCompleteTransfer):
+        (WebCore::CurlRequest::completeDidReceiveResponse):
+        (WebCore::CurlRequest::pausedStatusChanged):
+        * platform/network/curl/CurlRequest.h:
+        (WebCore::CurlRequest::isCompleted const): Deleted.
+        (WebCore::CurlRequest::isCancelled const): Deleted.
+        (WebCore::CurlRequest::isCompletedOrCancelled const): Deleted.
+        * platform/network/curl/CurlRequestScheduler.cpp:
+        (WebCore::CurlRequestScheduler::cancel):
+        (WebCore::CurlRequestScheduler::callOnWorkerThread):
+        (WebCore::CurlRequestScheduler::startThreadIfNeeded):
+        (WebCore::CurlRequestScheduler::stopThreadIfNoMoreJobRunning):
+        (WebCore::CurlRequestScheduler::stopThread):
+        (WebCore::CurlRequestScheduler::executeTasks):
+        (WebCore::CurlRequestScheduler::workerThread):
+        (WebCore::CurlRequestScheduler::startTransfer):
+        (WebCore::CurlRequestScheduler::completeTransfer):
+        (WebCore::CurlRequestScheduler::cancelTransfer):
+        (WebCore::CurlRequestScheduler::finalizeTransfer):
+        * platform/network/curl/CurlRequestScheduler.h:
+
</ins><span class="cx"> 2019-04-25  Myles C. Maxfield  <mmaxfield@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [iOS] Add internal setting to force -webkit-text-size-adjust to "auto"
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkcurlCurlRequestcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/curl/CurlRequest.cpp (244683 => 244684)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/curl/CurlRequest.cpp       2019-04-26 06:39:30 UTC (rev 244683)
+++ trunk/Source/WebCore/platform/network/curl/CurlRequest.cpp  2019-04-26 09:52:49 UTC (rev 244684)
</span><span class="lines">@@ -126,10 +126,13 @@
</span><span class="cx"> {
</span><span class="cx">     ASSERT(isMainThread());
</span><span class="cx"> 
</span><del>-    if (isCompletedOrCancelled())
-        return;
</del><ins>+    {
+        auto locker = holdLock(m_statusMutex);
+        if (m_cancelled)
+            return;
</ins><span class="cx"> 
</span><del>-    m_cancelled = true;
</del><ins>+        m_cancelled = true;
+    }
</ins><span class="cx"> 
</span><span class="cx">     auto& scheduler = CurlContext::singleton().scheduler();
</span><span class="cx"> 
</span><span class="lines">@@ -143,6 +146,18 @@
</span><span class="cx">     invalidateClient();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool CurlRequest::isCancelled()
+{
+    auto locker = holdLock(m_statusMutex);
+    return m_cancelled;
+}
+
+bool CurlRequest::isCompletedOrCancelled()
+{
+    auto locker = holdLock(m_statusMutex);
+    return m_completed || m_cancelled;
+}
+
</ins><span class="cx"> void CurlRequest::suspend()
</span><span class="cx"> {
</span><span class="cx">     ASSERT(isMainThread());
</span><span class="lines">@@ -415,8 +430,8 @@
</span><span class="cx"> 
</span><span class="cx"> void CurlRequest::didCompleteTransfer(CURLcode result)
</span><span class="cx"> {
</span><del>-    if (m_cancelled) {
-        m_curlHandle = nullptr;
</del><ins>+    if (isCancelled()) {
+        didCancelTransfer();
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -455,6 +470,11 @@
</span><span class="cx">             client.curlDidFailWithError(request, error);
</span><span class="cx">         });
</span><span class="cx">     }
</span><ins>+
+    {
+        auto locker = holdLock(m_statusMutex);
+        m_completed = true;
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void CurlRequest::didCancelTransfer()
</span><span class="lines">@@ -567,12 +587,9 @@
</span><span class="cx">     ASSERT(m_didNotifyResponse);
</span><span class="cx">     ASSERT(!m_didReturnFromNotify || m_multipartHandle);
</span><span class="cx"> 
</span><del>-    if (isCancelled())
</del><ins>+    if (isCompletedOrCancelled())
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    if (m_actionAfterInvoke != Action::StartTransfer && isCompleted())
-        return;
-
</del><span class="cx">     m_didReturnFromNotify = true;
</span><span class="cx"> 
</span><span class="cx">     if (m_actionAfterInvoke == Action::ReceiveData) {
</span><span class="lines">@@ -635,7 +652,7 @@
</span><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     runOnWorkerThreadIfRequired([this, protectedThis = makeRef(*this)]() {
</span><del>-        if (isCompletedOrCancelled())
</del><ins>+        if (isCompletedOrCancelled() || !m_curlHandle)
</ins><span class="cx">             return;
</span><span class="cx"> 
</span><span class="cx">         bool needCancel { false };
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkcurlCurlRequesth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/curl/CurlRequest.h (244683 => 244684)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/curl/CurlRequest.h 2019-04-26 06:39:30 UTC (rev 244683)
+++ trunk/Source/WebCore/platform/network/curl/CurlRequest.h    2019-04-26 09:52:49 UTC (rev 244684)
</span><span class="lines">@@ -83,9 +83,8 @@
</span><span class="cx">     WEBCORE_EXPORT void resume();
</span><span class="cx"> 
</span><span class="cx">     const ResourceRequest& resourceRequest() const { return m_request; }
</span><del>-    bool isCompleted() const { return !m_curlHandle; }
-    bool isCancelled() const { return m_cancelled; }
-    bool isCompletedOrCancelled() const { return isCompleted() || isCancelled(); }
</del><ins>+    bool isCancelled();
+    bool isCompletedOrCancelled();
</ins><span class="cx">     Seconds timeoutInterval() const;
</span><span class="cx"> 
</span><span class="cx">     const String& user() const { return m_user; }
</span><span class="lines">@@ -166,7 +165,9 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     CurlRequestClient* m_client { };
</span><ins>+    Lock m_statusMutex;
</ins><span class="cx">     bool m_cancelled { false };
</span><ins>+    bool m_completed { false };
</ins><span class="cx">     MessageQueue<Function<void()>>* m_messageQueue { };
</span><span class="cx"> 
</span><span class="cx">     ResourceRequest m_request;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkcurlCurlRequestSchedulercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/curl/CurlRequestScheduler.cpp (244683 => 244684)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/curl/CurlRequestScheduler.cpp      2019-04-26 06:39:30 UTC (rev 244683)
+++ trunk/Source/WebCore/platform/network/curl/CurlRequestScheduler.cpp 2019-04-26 09:52:49 UTC (rev 244684)
</span><span class="lines">@@ -58,16 +58,16 @@
</span><span class="cx"> {
</span><span class="cx">     ASSERT(isMainThread());
</span><span class="cx"> 
</span><del>-    if (!client || !client->handle())
</del><ins>+    if (!client)
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    cancelTransfer(client->handle());
</del><ins>+    cancelTransfer(client);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void CurlRequestScheduler::callOnWorkerThread(WTF::Function<void()>&& task)
</span><span class="cx"> {
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_mutex);
</del><ins>+        auto locker = holdLock(m_mutex);
</ins><span class="cx">         m_taskQueue.append(WTFMove(task));
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -78,17 +78,26 @@
</span><span class="cx"> {
</span><span class="cx">     ASSERT(isMainThread());
</span><span class="cx"> 
</span><del>-    LockHolder locker(m_mutex);
-    if (!m_runThread) {
-        if (m_thread)
-            m_thread->waitForCompletion();
</del><ins>+    {
+        auto locker = holdLock(m_mutex);
+        if (m_runThread)
+            return;
+    }
</ins><span class="cx"> 
</span><ins>+    if (m_thread)
+        m_thread->waitForCompletion();
+
+    {
+        auto locker = holdLock(m_mutex);
</ins><span class="cx">         m_runThread = true;
</span><del>-        m_thread = Thread::create("curlThread", [this] {
-            workerThread();
-            m_runThread = false;
-        });
</del><span class="cx">     }
</span><ins>+
+    m_thread = Thread::create("curlThread", [this] {
+        workerThread();
+
+        auto locker = holdLock(m_mutex);
+        m_runThread = false;
+    });
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void CurlRequestScheduler::stopThreadIfNoMoreJobRunning()
</span><span class="lines">@@ -95,19 +104,19 @@
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!isMainThread());
</span><span class="cx"> 
</span><del>-    if (m_activeJobs.size())
</del><ins>+    auto locker = holdLock(m_mutex);
+    if (m_activeJobs.size() || m_taskQueue.size())
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    LockHolder locker(m_mutex);
-    if (m_taskQueue.size())
-        return;
-
</del><span class="cx">     m_runThread = false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void CurlRequestScheduler::stopThread()
</span><span class="cx"> {
</span><del>-    m_runThread = false;
</del><ins>+    {
+        auto locker = holdLock(m_mutex);
+        m_runThread = false;
+    }
</ins><span class="cx"> 
</span><span class="cx">     if (m_thread) {
</span><span class="cx">         m_thread->waitForCompletion();
</span><span class="lines">@@ -122,7 +131,7 @@
</span><span class="cx">     Vector<WTF::Function<void()>> taskQueue;
</span><span class="cx"> 
</span><span class="cx">     {
</span><del>-        LockHolder locker(m_mutex);
</del><ins>+        auto locker = holdLock(m_mutex);
</ins><span class="cx">         taskQueue = WTFMove(m_taskQueue);
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -139,7 +148,13 @@
</span><span class="cx">     m_curlMultiHandle->setMaxTotalConnections(m_maxTotalConnections);
</span><span class="cx">     m_curlMultiHandle->setMaxHostConnections(m_maxHostConnections);
</span><span class="cx"> 
</span><del>-    while (m_runThread) {
</del><ins>+    while (true) {
+        {
+            auto locker = holdLock(m_mutex);
+            if (!m_runThread)
+                break;
+        }
+
</ins><span class="cx">         executeTasks();
</span><span class="cx"> 
</span><span class="cx">         // Retry 'select' if it was interrupted by a process signal.
</span><span class="lines">@@ -177,7 +192,8 @@
</span><span class="cx">                 break;
</span><span class="cx"> 
</span><span class="cx">             ASSERT(msg->msg == CURLMSG_DONE);
</span><del>-            completeTransfer(msg->easy_handle, msg->data.result);
</del><ins>+            if (auto client = m_clientMaps.inlineGet(msg->easy_handle))
+                completeTransfer(client, msg->data.result);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         stopThreadIfNoMoreJobRunning();
</span><span class="lines">@@ -192,49 +208,59 @@
</span><span class="cx"> 
</span><span class="cx">     auto task = [this, client]() {
</span><span class="cx">         CURL* handle = client->setupTransfer();
</span><del>-        if (!handle)
</del><ins>+        if (!handle) {
+            completeTransfer(client, CURLE_FAILED_INIT);
</ins><span class="cx">             return;
</span><ins>+        }
</ins><span class="cx"> 
</span><del>-        m_activeJobs.add(handle, client);
</del><span class="cx">         m_curlMultiHandle->addHandle(handle);
</span><ins>+
+        ASSERT(!m_clientMaps.contains(handle));
+        m_clientMaps.set(handle, client);
</ins><span class="cx">     };
</span><span class="cx"> 
</span><del>-    LockHolder locker(m_mutex);
</del><ins>+    auto locker = holdLock(m_mutex);
+    m_activeJobs.add(client);
</ins><span class="cx">     m_taskQueue.append(WTFMove(task));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void CurlRequestScheduler::completeTransfer(CURL* handle, CURLcode result)
</del><ins>+void CurlRequestScheduler::completeTransfer(CurlRequestSchedulerClient* client, CURLcode result)
</ins><span class="cx"> {
</span><del>-    finalizeTransfer(handle, [result](CurlRequestSchedulerClient* client) {
</del><ins>+    finalizeTransfer(client, [client, result]() {
</ins><span class="cx">         client->didCompleteTransfer(result);
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void CurlRequestScheduler::cancelTransfer(CURL* handle)
</del><ins>+void CurlRequestScheduler::cancelTransfer(CurlRequestSchedulerClient* client)
</ins><span class="cx"> {
</span><del>-    finalizeTransfer(handle, [](CurlRequestSchedulerClient* client) {
</del><ins>+    finalizeTransfer(client, [client]() {
</ins><span class="cx">         client->didCancelTransfer();
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void CurlRequestScheduler::finalizeTransfer(CURL* handle, Function<void(CurlRequestSchedulerClient*)> completionHandler)
</del><ins>+void CurlRequestScheduler::finalizeTransfer(CurlRequestSchedulerClient* client, Function<void()> completionHandler)
</ins><span class="cx"> {
</span><del>-    auto task = [this, handle, completion = WTFMove(completionHandler)]() {
-        if (!m_activeJobs.contains(handle))
-            return;
</del><ins>+    auto locker = holdLock(m_mutex);
</ins><span class="cx"> 
</span><del>-        CurlRequestSchedulerClient* client = m_activeJobs.inlineGet(handle);
</del><ins>+    if (!m_activeJobs.contains(client))
+        return;
</ins><span class="cx"> 
</span><del>-        m_curlMultiHandle->removeHandle(handle);
-        m_activeJobs.remove(handle);
-        completion(client);
</del><ins>+    m_activeJobs.remove(client);
</ins><span class="cx"> 
</span><ins>+    auto task = [this, client, completionHandler = WTFMove(completionHandler)]() {
+        if (client->handle()) {
+            ASSERT(m_clientMaps.contains(client->handle()));
+            m_clientMaps.remove(client->handle());
+            m_curlMultiHandle->removeHandle(client->handle());
+        }
+
+        completionHandler();
+
</ins><span class="cx">         callOnMainThread([client]() {
</span><span class="cx">             client->release();
</span><span class="cx">         });
</span><span class="cx">     };
</span><span class="cx"> 
</span><del>-    LockHolder locker(m_mutex);
</del><span class="cx">     m_taskQueue.append(WTFMove(task));
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkcurlCurlRequestSchedulerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/curl/CurlRequestScheduler.h (244683 => 244684)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/curl/CurlRequestScheduler.h        2019-04-26 06:39:30 UTC (rev 244683)
+++ trunk/Source/WebCore/platform/network/curl/CurlRequestScheduler.h   2019-04-26 09:52:49 UTC (rev 244684)
</span><span class="lines">@@ -59,16 +59,17 @@
</span><span class="cx">     void workerThread();
</span><span class="cx"> 
</span><span class="cx">     void startTransfer(CurlRequestSchedulerClient*);
</span><del>-    void completeTransfer(CURL*, CURLcode);
-    void cancelTransfer(CURL*);
-    void finalizeTransfer(CURL*, Function<void(CurlRequestSchedulerClient*)>);
</del><ins>+    void completeTransfer(CurlRequestSchedulerClient*, CURLcode);
+    void cancelTransfer(CurlRequestSchedulerClient*);
+    void finalizeTransfer(CurlRequestSchedulerClient*, Function<void()>);
</ins><span class="cx"> 
</span><del>-    mutable Lock m_mutex;
</del><ins>+    Lock m_mutex;
</ins><span class="cx">     RefPtr<Thread> m_thread;
</span><span class="cx">     bool m_runThread { false };
</span><span class="cx"> 
</span><span class="cx">     Vector<Function<void()>> m_taskQueue;
</span><del>-    HashMap<CURL*, CurlRequestSchedulerClient*> m_activeJobs;
</del><ins>+    HashSet<CurlRequestSchedulerClient*> m_activeJobs;
+    HashMap<CURL*, CurlRequestSchedulerClient*> m_clientMaps;
</ins><span class="cx"> 
</span><span class="cx">     std::unique_ptr<CurlMultiHandle> m_curlMultiHandle;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>