Bug 179791

Summary: [Service Worker] Implement "Try Clear Registration" algorithm
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: WebCore Misc.Assignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Normal CC: beidson, commit-queue, dbates, esprehn+autocc, ews-watchlist, ggaren, kangil.han, rniwa, webkit-bug-importer, youennf
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Archive of layout-test-results from ews106 for mac-elcapitan-wk2
none
Archive of layout-test-results from ews122 for ios-simulator-wk2
none
Patch
none
Patch none

Description Chris Dumez 2017-11-16 12:57:58 PST
Implement "Try Clear Registration" algorithm:
- https://w3c.github.io/ServiceWorker/#try-clear-registration-algorithm
Comment 1 Chris Dumez 2017-11-16 14:01:43 PST
Created attachment 327104 [details]
Patch
Comment 2 Chris Dumez 2017-11-16 14:31:08 PST
Created attachment 327109 [details]
Patch
Comment 3 EWS Watchlist 2017-11-16 15:43:16 PST
Comment on attachment 327109 [details]
Patch

Attachment 327109 [details] did not pass mac-wk2-ews (mac-wk2):
Output: http://webkit-queues.webkit.org/results/5264453

New failing tests:
imported/w3c/web-platform-tests/fetch/api/abort/serviceworker-intercepted.https.html
Comment 4 EWS Watchlist 2017-11-16 15:43:17 PST
Created attachment 327115 [details]
Archive of layout-test-results from ews106 for mac-elcapitan-wk2

The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews.
Bot: ews106  Port: mac-elcapitan-wk2  Platform: Mac OS X 10.11.6
Comment 5 youenn fablet 2017-11-16 15:51:22 PST
Comment on attachment 327109 [details]
Patch

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

> Source/WebCore/dom/ScriptExecutionContext.cpp:550
> +    uint64_t contextIdentifier = is<Document>(*this) ? downcast<Document>(*this).identifier() : 0;

It might be better to exit early if we are not a Document.
I am not sure calling ServiceWorkerProvider::singleton() will work properly on worker thread.

> Source/WebCore/workers/service/server/SWClientConnection.h:75
> +    virtual void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, uint64_t scriptExecutionContextIdentifier) = 0;

Would be nice to have a ScriptExcutionContextIdentifier at some point :)

> Source/WebCore/workers/service/server/SWServer.cpp:333
> +    auto* registration = m_registrations.get(serviceWorker->registrationKey());

Are there cases where we have a worker but no longer a registration?

> Source/WebCore/workers/service/server/SWServerRegistration.cpp:156
> +    auto it = m_clientsUsingRegistration.find(clientIdentifier.serverConnectionIdentifier);

iterator

> Source/WebCore/workers/service/server/SWServerRegistration.cpp:158
> +        return;

Should we assert that it is not the end?

> Source/WebCore/workers/service/server/SWServerRegistration.cpp:160
> +    it->value.remove(clientIdentifier.scriptExecutionContextIdentifier);

Should we assert remove returned true?
We could also do below test only if remove returned true.

> LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/clients-matchall-client-types.https-expected.txt:4
> +FAIL Verify matchAll() with {window, sharedworker, worker} client types promise_test: Unhandled rejection with value: object "Error: wait_for_state must be passed a ServiceWorker"

Test probably flaky or maybe regressing?
Comment 6 EWS Watchlist 2017-11-16 15:54:54 PST
Comment on attachment 327109 [details]
Patch

Attachment 327109 [details] did not pass ios-sim-ews (ios-simulator-wk2):
Output: http://webkit-queues.webkit.org/results/5264461

New failing tests:
imported/w3c/web-platform-tests/fetch/api/abort/serviceworker-intercepted.https.html
Comment 7 EWS Watchlist 2017-11-16 15:54:55 PST
Created attachment 327118 [details]
Archive of layout-test-results from ews122 for ios-simulator-wk2

The attached test failures were seen while running run-webkit-tests on the ios-sim-ews.
Bot: ews122  Port: ios-simulator-wk2  Platform: Mac OS X 10.12.6
Comment 8 Chris Dumez 2017-11-16 15:56:49 PST
Comment on attachment 327109 [details]
Patch

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

>> Source/WebCore/workers/service/server/SWServer.cpp:333
>> +    auto* registration = m_registrations.get(serviceWorker->registrationKey());
> 
> Are there cases where we have a worker but no longer a registration?

Yes, I saw it in the tests. This can happen when a document that had an active service worker gets destroyed but the registrations got cleared on storage process side.
Comment 9 Chris Dumez 2017-11-16 15:59:36 PST
Comment on attachment 327109 [details]
Patch

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

>> LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/clients-matchall-client-types.https-expected.txt:4
>> +FAIL Verify matchAll() with {window, sharedworker, worker} client types promise_test: Unhandled rejection with value: object "Error: wait_for_state must be passed a ServiceWorker"
> 
> Test probably flaky or maybe regressing?

Will take a look.
Comment 10 Chris Dumez 2017-11-16 16:13:53 PST
Created attachment 327121 [details]
Patch
Comment 11 Chris Dumez 2017-11-16 16:19:08 PST
Comment on attachment 327109 [details]
Patch

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

>> Source/WebCore/workers/service/server/SWServerRegistration.cpp:158
>> +        return;
> 
> Should we assert that it is not the end?

Done.

>>> LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/clients-matchall-client-types.https-expected.txt:4
>>> +FAIL Verify matchAll() with {window, sharedworker, worker} client types promise_test: Unhandled rejection with value: object "Error: wait_for_state must be passed a ServiceWorker"
>> 
>> Test probably flaky or maybe regressing?
> 
> Will take a look.

What happens is that when the test unregisters, the registration no longer gets cleared because it still has a user. The reason it has a user is because the previous subtest (Verify matchAll() with window client type) creates an iframe controlled by the registration. That iframe is supposed to be removed at the end of the subtest in question but we do not get there because we abort early (due to not supporting passing MessagePort objects to postMessage). If I comment out the previous subtest, then this last subtest fails with "ReferenceError: Can't find variable: SharedWorker", as expected.
Comment 12 Chris Dumez 2017-11-16 16:26:26 PST
Created attachment 327127 [details]
Patch
Comment 13 WebKit Commit Bot 2017-11-16 16:59:14 PST
Comment on attachment 327127 [details]
Patch

Clearing flags on attachment: 327127

Committed r224947: <https://trac.webkit.org/changeset/224947>
Comment 14 WebKit Commit Bot 2017-11-16 16:59:16 PST
All reviewed patches have been landed.  Closing bug.
Comment 15 Radar WebKit Bug Importer 2017-11-16 17:03:52 PST
<rdar://problem/35603950>