Bug 181698 - 'fetch' event may be sent to a service worker before its state is set to 'activated'
Summary: 'fetch' event may be sent to a service worker before its state is set to 'act...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Service Workers (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-01-16 12:42 PST by Chris Dumez
Modified: 2022-06-30 23:36 PDT (History)
6 users (show)

See Also:


Attachments
Patch (8.46 KB, patch)
2018-01-16 12:58 PST, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (8.86 KB, patch)
2018-01-17 09:11 PST, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2018-01-16 12:42:39 PST
'fetch' event may be sent to a service worker before its state is set to 'activated'.
Comment 1 Radar WebKit Bug Importer 2018-01-16 12:56:46 PST
<rdar://problem/36554856>
Comment 2 Chris Dumez 2018-01-16 12:58:30 PST
Created attachment 331418 [details]
Patch
Comment 3 youenn fablet 2018-01-17 01:35:27 PST
Comment on attachment 331418 [details]
Patch

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

> Source/WebCore/ChangeLog:32
> +        Add assertions to make sure that the we dispatch the fetch event on a the right worker and

s/a the/the/

> Source/WebCore/ChangeLog:40
> +        order.

Can you detail which things will happen in the wrong order? Is it causing some flakiness in the current tests?
We are already queuing a task when going from main thread to worker thread loop so we are already respecting what the spec says.
We are doing this queue-and-requeue for install/activate events but I  do not think they are racy with fetch event.
Ideally, I think we would like to not need this queue-and-requeue for activate/install events.

> Source/WebCore/workers/service/server/SWServerRegistration.cpp:-108
> -    });

The other approach is probably to put worker.setState(state) after the forEach.
That would probably remove the need for the registration null check but the intent is probably clearer.
Or maybe a comment in this method would be sufficiently clear.

> Source/WebCore/workers/service/server/SWServerWorker.cpp:178
> +    if (auto* registration = m_server.getRegistration(m_registrationKey)) {

I guess we could write something like ASSERT(registration || state == terminatingOrRedundant)?
Comment 4 Chris Dumez 2018-01-17 08:53:37 PST
Comment on attachment 331418 [details]
Patch

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

>> Source/WebCore/ChangeLog:40
>> +        order.
> 
> Can you detail which things will happen in the wrong order? Is it causing some flakiness in the current tests?
> We are already queuing a task when going from main thread to worker thread loop so we are already respecting what the spec says.
> We are doing this queue-and-requeue for install/activate events but I  do not think they are racy with fetch event.
> Ideally, I think we would like to not need this queue-and-requeue for activate/install events.

The issue is with the "update registration state" [1] and "update worker state" [2]. Both say to "queue a task" in the spec. The IPC I moved *before* calling the handlers is to update the worker state. Moving the IPC to update the worker state before calling the activation handlers (which fire the fetch event) would make things flaky if dispatching the fetch event would not "queue a task", because the code to update the worker state (and fire the state change event) already queued a task, as per spec.

[1] https://w3c.github.io/ServiceWorker/#update-registration-state-algorithm
[2] https://w3c.github.io/ServiceWorker/#update-state-algorithm
Comment 5 Chris Dumez 2018-01-17 09:11:18 PST
Created attachment 331501 [details]
Patch
Comment 6 WebKit Commit Bot 2018-01-17 10:07:37 PST
Comment on attachment 331501 [details]
Patch

Clearing flags on attachment: 331501

Committed r227070: <https://trac.webkit.org/changeset/227070>
Comment 7 WebKit Commit Bot 2018-01-17 10:07:39 PST
All reviewed patches have been landed.  Closing bug.
Comment 8 Fujii Hironori 2022-06-30 23:36:24 PDT
Bug 242229 – [mac] ASSERTION FAILED: globalScope.registration().active()->state() == ServiceWorkerState::Activated: imported/w3c/web-platform-tests/service-workers/service-worker/claim-with-redirect.https.html is randomly crashing