<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>185483</bug_id>
          
          <creation_ts>2018-05-09 12:02:34 -0700</creation_ts>
          <short_desc>iOS 11.3 delays creation of WKWebsiteDataStore and queues setCookie completion handler</short_desc>
          <delta_ts>2022-02-09 10:14:08 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKit API</component>
          <version>Other</version>
          <rep_platform>iPhone / iPad</rep_platform>
          <op_sys>iOS 11</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Harry Shamansky">hshamansky</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>beidson</cc>
    
    <cc>cdumez</cc>
    
    <cc>sihui_liu</cc>
    
    <cc>thorton</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1422389</commentid>
    <comment_count>0</comment_count>
    <who name="Harry Shamansky">hshamansky</who>
    <bug_when>2018-05-09 12:02:34 -0700</bug_when>
    <thetext>We have the following flow in our app: If navigating to a particular URL in a WKWebView that requires a cookie, cancel the navigation, set a cookie, and in the cookie setter&apos;s completion block, load the original URL in the web view.

However, new behavior added by https://github.com/WebKit/webkit/commit/d9d6e5c82c4a74cd573f3f119f166ffcee477b04 delays the creation of the WKWebsiteDataStore such that the completion block is queued until &quot;necessary&quot;, and WebKit never actually considers its creation &quot;necessary&quot; since we only load the URL in the completion block. It&apos;s essentially a deadlock.

Here&apos;s sample code:
```
if (@available(iOS 11.0, *)) {
        [webView.configuration.websiteDataStore.httpCookieStore setCookie:cookie
                                                        completionHandler:^{
            [webView loadRequest:request];
        }];
}
```

This was caused by rdar://problem/33164453 (see also https://bugs.webkit.org/show_bug.cgi?id=176551)

Steps to Reproduce:
Cancel web view navigation, set a cookie with a completion handler that loads the original (canceled) URL. Take no other action on the web view.

Expected Results:
The cookie is set and the completion handler is called.

Actual Results:
The cookie is not set (and the completion handler is not called) until another action is taken on the WKWebView that requires the WKWebsiteDataStore to be initialized.

Version/Build:
Issue affects iOS 11.3+ only. Rarely reproduces on simulator; frequently but not always reproduces on device.

Configuration:
iPhone X 11.3

Filed as rdar://40100673</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1445113</commentid>
    <comment_count>1</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2018-07-25 12:27:50 -0700</bug_when>
    <thetext>Would you be able to provide a minimal test app? Looking at our code, we always call the completion handler right away:
void HTTPCookieStore::setCookie(const WebCore::Cookie&amp; cookie, Function&lt;void ()&gt;&amp;&amp; completionHandler)
{
    auto* pool = m_owningDataStore-&gt;processPoolForCookieStorageOperations();
    if (!pool) {
        // FIXME: pendingCookies used for defaultSession because session cookies cannot be propagated to Network Process with uiProcessCookieStorageIdentifier.
        if (m_owningDataStore-&gt;sessionID() == PAL::SessionID::defaultSessionID() &amp;&amp; !cookie.session)
            WebCore::NetworkStorageSession::defaultStorageSession().setCookie(cookie);
        else
            m_owningDataStore-&gt;addPendingCookie(cookie);

        // Calls the completion handler right away if we do not have a pool yet.
        callOnMainThread([completionHandler = WTFMove(completionHandler)]() {
            completionHandler();
        });
        return;
    }

    // This sends an IPC to the network process (starting it if it is not running) and we call the completion handler when the network process has processed the IPC.
    auto* cookieManager = pool-&gt;supplement&lt;WebKit::WebCookieManagerProxy&gt;();
    cookieManager-&gt;setCookie(m_owningDataStore-&gt;sessionID(), cookie, [pool = WTFMove(pool), completionHandler = WTFMove(completionHandler)](CallbackBase::Error error) {
        completionHandler();
    });
}

The code you mention that appends cookies does not delay the call of the completion handler.

Also note that we&apos;ve made fixes in iOS 12 beta, did you test it?</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>