Bug 185041

Summary: [WKHTTPCookieStore getAllCookies] returns inconsistent creation time
Product: WebKit Reporter: Sihui Liu <sihui_liu>
Component: WebKit APIAssignee: Sihui Liu <sihui_liu>
Status: RESOLVED FIXED    
Severity: Normal CC: ap, cdumez, commit-queue, dbates, ggaren, sihui_liu, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on: 185434, 185435    
Bug Blocks:    
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch
none
Patch none

Description Sihui Liu 2018-04-26 13:01:08 PDT
Calling getAllCookies multiple times would return different creation time/expiration time for the same cookie.
Comment 1 Sihui Liu 2018-04-26 13:02:40 PDT
<rdar://problem/34684214>
Comment 2 Sihui Liu 2018-04-30 20:01:30 PDT
Created attachment 339171 [details]
Patch
Comment 3 Chris Dumez 2018-05-01 09:30:30 PDT
Comment on attachment 339171 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=339171&action=review

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:335
> +    NSMutableDictionary *cookieProperties = [[NSMutableDictionary alloc] init];

Doesn't this leak? Should probably use RetainPtr and adoptNS().

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:357
> +    [cookies release];

Should probably use RetainPtr instead of manual release.

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:359
> +    sleep(5_s);

Do we need to sleep for that long? We do not like slow tests in general.

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:367
> +    ASSERT_EQ(cookies.count, 1u);

I believe this is reversed:
ASSERT_EQ(1U, cookies.count);

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:371
> +    [cookies release];

Should probably use RetainPtr instead of manual release.
Comment 4 Geoffrey Garen 2018-05-01 09:44:40 PDT
Comment on attachment 339171 [details]
Patch

r- I agree with Chris. Let's discuss Cocoa retain/release in more detail when we have a chance.
Comment 5 Sihui Liu 2018-05-01 12:02:53 PDT
Created attachment 339212 [details]
Patch
Comment 6 Geoffrey Garen 2018-05-01 15:21:09 PDT
Comment on attachment 339212 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=339212&action=review

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:348
> +        *cookiesPtr = [nsCookies retain];

RetainPtr is a smart pointer <https://en.wikipedia.org/wiki/Smart_pointer> that automatically retains and releases the pointer it holds. The act of assignment is an implicit retain of the new value and release of the old value, so please remove this explicit retain, which is an extra retain, and therefore a memory leak.

Also, you can just lambda capture &cookies and use "cookies" by name, rather than capturing cookiesPtr = &cookies. I think that's simpler.

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:356
> +    sleep(1_s);

What is the resolution of creation time? If it's milliseconds, then you can reduce this sleep to something like 10ms.

It's good to make tests as fast as possible because we have tens of thousands of them.

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:359
> +        *cookiesPtr = [nsCookies retain];

Ditto.
Comment 7 Sihui Liu 2018-05-01 16:29:32 PDT
Created attachment 339240 [details]
Patch
Comment 8 Sihui Liu 2018-05-01 16:37:29 PDT
(In reply to Geoffrey Garen from comment #6)
> Comment on attachment 339212 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=339212&action=review
> 
> > Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:348
> > +        *cookiesPtr = [nsCookies retain];
> 
> RetainPtr is a smart pointer <https://en.wikipedia.org/wiki/Smart_pointer>
> that automatically retains and releases the pointer it holds. The act of
> assignment is an implicit retain of the new value and release of the old
> value, so please remove this explicit retain, which is an extra retain, and
> therefore a memory leak.
> 
> Also, you can just lambda capture &cookies and use "cookies" by name, rather
> than capturing cookiesPtr = &cookies. I think that's simpler.
> 
Done. Lambda capture seems to pass non-static params by value. But I realized we don't need a cookies variable here...

> > Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:356
> > +    sleep(1_s);
> 
> What is the resolution of creation time? If it's milliseconds, then you can
> reduce this sleep to something like 10ms.
> 
> It's good to make tests as fast as possible because we have tens of
> thousands of them.
> 
It's measured in seconds.

> > Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:359
> > +        *cookiesPtr = [nsCookies retain];
> 
> Ditto.
Done.
Comment 9 Geoffrey Garen 2018-05-07 19:49:25 PDT
Comment on attachment 339240 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=339240&action=review

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:351
> +    RetainPtr<NSNumber> creationTime = nil;
> +    [globalCookieStore getAllCookies:[creationTimePtr = &creationTime](NSArray<NSHTTPCookie *> *cookies) {
> +        ASSERT_EQ(1u, cookies.count);
> +        *creationTimePtr = [cookies objectAtIndex:0].properties[@"Created"];
> +        gotFlag = true;
> +    }];

I think it should work to do "[globalCookieStore getAllCookies:[&](...) { ... creationTime = ... }". Then you capture the creationTime RetainPtr by reference, and assign to it.

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:359
> +        RetainPtr<NSNumber> creationTime2 = [cookies objectAtIndex:0].properties[@"Created"];

You can use NSNumber * here instead of RetainPtr<NSNumber> because objectAtIndex returns an autoreleased value, which remains live within this scope. 

<https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI>
Comment 10 Sihui Liu 2018-05-07 23:06:30 PDT
Created attachment 339803 [details]
Patch
Comment 11 Geoffrey Garen 2018-05-08 10:17:44 PDT
Comment on attachment 339803 [details]
Patch

r=me
Comment 12 WebKit Commit Bot 2018-05-08 10:44:08 PDT
Comment on attachment 339803 [details]
Patch

Clearing flags on attachment: 339803

Committed r231491: <https://trac.webkit.org/changeset/231491>
Comment 13 WebKit Commit Bot 2018-05-08 10:44:09 PDT
All reviewed patches have been landed.  Closing bug.
Comment 14 WebKit Commit Bot 2018-05-08 11:22:45 PDT
Re-opened since this is blocked by bug 185434
Comment 15 Geoffrey Garen 2018-05-24 13:12:42 PDT
If we need a CF change to get the behavior we want, then we should conditionalize this new behavior and exclude it from OS's that lack the change we need.
Comment 16 Sihui Liu 2018-05-25 11:01:46 PDT
Created attachment 341302 [details]
Patch
Comment 17 Geoffrey Garen 2018-05-25 11:08:31 PDT
Comment on attachment 341302 [details]
Patch

r=me
Comment 18 WebKit Commit Bot 2018-05-25 11:35:18 PDT
Comment on attachment 341302 [details]
Patch

Clearing flags on attachment: 341302

Committed r232191: <https://trac.webkit.org/changeset/232191>
Comment 19 WebKit Commit Bot 2018-05-25 11:35:20 PDT
All reviewed patches have been landed.  Closing bug.