Bug 189619 - [EME] Queue a "message" Event algorithm should enqueue a task
Summary: [EME] Queue a "message" Event algorithm should enqueue a task
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: Other
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Xabier Rodríguez Calvar
URL:
Keywords: WPTImpact
Depends on:
Blocks:
 
Reported: 2018-09-14 07:47 PDT by Xabier Rodríguez Calvar
Modified: 2023-07-27 23:53 PDT (History)
6 users (show)

See Also:


Attachments
Patch (1.96 KB, patch)
2018-09-14 07:51 PDT, Xabier Rodríguez Calvar
no flags Details | Formatted Diff | Diff
Patch (9.45 KB, patch)
2018-09-18 04:12 PDT, Xabier Rodríguez Calvar
no flags Details | Formatted Diff | Diff
Patch (12.33 KB, patch)
2018-09-20 08:58 PDT, Xabier Rodríguez Calvar
no flags Details | Formatted Diff | Diff
Patch (16.41 KB, patch)
2018-09-21 03:49 PDT, Xabier Rodríguez Calvar
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Xabier Rodríguez Calvar 2018-09-14 07:47:43 PDT
This will a remaining problem of fixing bug 185590. Got a fix for it already.
Comment 1 Xabier Rodríguez Calvar 2018-09-14 07:51:34 PDT
Created attachment 349759 [details]
Patch
Comment 2 Xabier Rodríguez Calvar 2018-09-18 04:12:36 PDT
Created attachment 350012 [details]
Patch
Comment 3 Philippe Normand 2018-09-18 04:59:24 PDT
Comment on attachment 350012 [details]
Patch

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

> LayoutTests/media/encrypted-media/mock-MediaKeySession-remove-expected.txt:40
>  Promise resolved OK
>  RUN(promise = mediaKeySession.remove())
> +EXPECTED (event.messageType == 'license-request') OK

This looks odd.
Comment 4 Xabier Rodríguez Calvar 2018-09-18 05:49:35 PDT
(In reply to Philippe Normand from comment #3)
> Comment on attachment 350012 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=350012&action=review
> 
> > LayoutTests/media/encrypted-media/mock-MediaKeySession-remove-expected.txt:40
> >  Promise resolved OK
> >  RUN(promise = mediaKeySession.remove())
> > +EXPECTED (event.messageType == 'license-request') OK
> 
> This looks odd.

Yes, it did to me too when I debugged it but the spec says that promise callbacks are run before any other event handler as they are scheduled as microtasks. Making this change (to make it conformant with the spec) has this side effect in this custom test. generateRequest returns a promise and an fires an license-request event so if you subscribe to the event handler in the promise callback, you're going to get it. If you think (unlike me) that it is a bad idea to have that check there, I can remove that check but the test has to be modified to expect that event before the other.
Comment 5 Xabier Rodríguez Calvar 2018-09-18 09:42:19 PDT
Phil, after our discussion, I think I managed to fix the issues and regressions we talked about. Please, test this if you can. I won't be able to land it until tomorrow unless you want to watch it.
Comment 6 Xabier Rodríguez Calvar 2018-09-18 09:43:06 PDT
(In reply to Xabier Rodríguez Calvar from comment #5)
> Phil, after our discussion, I think I managed to fix the issues and
> regressions we talked about. Please, test this if you can. I won't be able
> to land it until tomorrow unless you want to watch it.

Oops, wrong bug.
Comment 7 Xabier Rodríguez Calvar 2018-09-20 08:58:13 PDT
Created attachment 350209 [details]
Patch

This patch is still WIP, but I accept comments already
Comment 8 Xabier Rodríguez Calvar 2018-09-20 09:00:44 PDT
The problem here for the waiting-for-key test is that the event is arriving before the promise is resolved. Zan thinks we should have a queue for events and tasks and another for "run in parallel" steps but it looks like it blocks everything if I do that. This is the only thing, before any other investigation, that makes things work and besides that, it files a couple more tests.
Comment 9 Jer Noble 2018-09-20 09:51:12 PDT
Comment on attachment 350209 [details]
Patch

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

> Source/WebCore/Modules/encryptedmedia/MediaKeySession.cpp:601
> -    m_eventQueue.enqueueEvent(WTFMove(messageEvent));
> +    m_eventQueue.enqueueTask([this, messageEvent = WTFMove(messageEvent), pendingActivity = makePendingActivity(*this)] {
> +        dispatchEvent(messageEvent);
> +    });

This really shouldn't be necessary. GenericEventQueue is implemented with a GenericTaskQueue; this just duplicates that work, without implementing things like ensuring the event's target is unset and without implementing event dispatch suspension. I suspect that some of the other parts of this patch are what's actually fixing this test.

> Source/WebCore/Modules/encryptedmedia/MediaKeySession.cpp:712
> +    return m_eventQueue.hasPendingTasks() || ActiveDOMObject::hasPendingActivity();

This could just be m_eventQueue.hasPendingEvents()
Comment 10 Jer Noble 2018-09-20 09:54:17 PDT
(In reply to Xabier Rodríguez Calvar from comment #8)
> The problem here for the waiting-for-key test is that the event is arriving
> before the promise is resolved. Zan thinks we should have a queue for events
> and tasks and another for "run in parallel" steps but it looks like it
> blocks everything if I do that. This is the only thing, before any other
> investigation, that makes things work and besides that, it files a couple
> more tests.

Yes, we have a general problem with event and promise ordering. We would need to fully implement the HTML Event Loop spec <https://html.spec.whatwg.org/multipage/webappapis.html#event-loops> to fix this generally.
Comment 11 Xabier Rodríguez Calvar 2018-09-21 03:49:48 PDT
Created attachment 350347 [details]
Patch
Comment 12 Alex Christensen 2021-11-01 12:56:09 PDT
Comment on attachment 350347 [details]
Patch

This has been requesting review for more than one year.  If this is still needed, please rebase and re-request review.