RESOLVED FIXED188246
Add support for microtasks in workers
https://bugs.webkit.org/show_bug.cgi?id=188246
Summary Add support for microtasks in workers
Yusuke Suzuki
Reported 2018-08-01 20:34:37 PDT
Unfortunately, our worker does not support microtasks now!
Attachments
Patch (1.09 MB, patch)
2018-08-02 03:00 PDT, Yusuke Suzuki
no flags
Patch (1.10 MB, patch)
2018-08-02 03:19 PDT, Yusuke Suzuki
no flags
Patch (1.10 MB, patch)
2018-08-02 03:37 PDT, Yusuke Suzuki
no flags
Archive of layout-test-results from ews115 for mac-sierra (1.10 MB, application/zip)
2018-08-02 04:56 PDT, EWS Watchlist
no flags
Archive of layout-test-results from ews106 for mac-sierra-wk2 (2.92 MB, application/zip)
2018-08-02 04:58 PDT, EWS Watchlist
no flags
Archive of layout-test-results from ews206 for win-future (13.12 MB, application/zip)
2018-08-02 09:42 PDT, EWS Watchlist
no flags
Archive of layout-test-results from ews126 for ios-simulator-wk2 (14.81 MB, application/zip)
2018-08-02 11:27 PDT, EWS Watchlist
no flags
Patch (1.10 MB, patch)
2018-08-02 11:56 PDT, Yusuke Suzuki
no flags
Patch (1.10 MB, patch)
2018-08-02 12:12 PDT, Yusuke Suzuki
no flags
Archive of layout-test-results from ews104 for mac-sierra-wk2 (2.90 MB, application/zip)
2018-08-02 13:33 PDT, EWS Watchlist
no flags
Archive of layout-test-results from ews121 for ios-simulator-wk2 (13.46 MB, application/zip)
2018-08-02 13:58 PDT, EWS Watchlist
no flags
Archive of layout-test-results from ews200 for win-future (12.90 MB, application/zip)
2018-08-02 16:35 PDT, EWS Watchlist
no flags
Patch (1.10 MB, patch)
2018-08-03 11:32 PDT, Yusuke Suzuki
no flags
Archive of layout-test-results from ews106 for mac-sierra-wk2 (2.95 MB, application/zip)
2018-08-03 12:34 PDT, EWS Watchlist
no flags
Archive of layout-test-results from ews122 for ios-simulator-wk2 (2.25 MB, application/zip)
2018-08-03 13:32 PDT, EWS Watchlist
no flags
Patch (1.10 MB, patch)
2018-08-03 13:38 PDT, Yusuke Suzuki
no flags
Patch (1.11 MB, patch)
2018-08-03 14:27 PDT, Yusuke Suzuki
no flags
Patch (255.77 KB, patch)
2018-08-03 16:33 PDT, Yusuke Suzuki
no flags
Patch (255.96 KB, patch)
2018-08-03 16:36 PDT, Yusuke Suzuki
darin: review+
Yusuke Suzuki
Comment 1 2018-08-02 03:00:54 PDT
Created attachment 346370 [details] Patch WIP
Yusuke Suzuki
Comment 2 2018-08-02 03:19:03 PDT
Created attachment 346371 [details] Patch WIP
Yusuke Suzuki
Comment 3 2018-08-02 03:37:09 PDT
Created attachment 346372 [details] Patch WIP
EWS Watchlist
Comment 4 2018-08-02 04:56:17 PDT
Comment on attachment 346372 [details] Patch Attachment 346372 [details] did not pass mac-debug-ews (mac): Output: https://webkit-queues.webkit.org/results/8736779 Number of test failures exceeded the failure limit.
EWS Watchlist
Comment 5 2018-08-02 04:56:19 PDT
Created attachment 346376 [details] Archive of layout-test-results from ews115 for mac-sierra The attached test failures were seen while running run-webkit-tests on the mac-debug-ews. Bot: ews115 Port: mac-sierra Platform: Mac OS X 10.12.6
EWS Watchlist
Comment 6 2018-08-02 04:58:50 PDT
Comment on attachment 346372 [details] Patch Attachment 346372 [details] did not pass mac-wk2-ews (mac-wk2): Output: https://webkit-queues.webkit.org/results/8736827 New failing tests: http/tests/workers/service/install-fails.html imported/w3c/web-platform-tests/service-workers/service-worker/unregister-then-register-new-script.https.html imported/w3c/web-platform-tests/service-workers/service-worker/oninstall-script-error.https.html imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-async-waituntil.https.html imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-waituntil.https.html imported/w3c/web-platform-tests/service-workers/service-worker/register-same-scope-different-script-url.https.html
EWS Watchlist
Comment 7 2018-08-02 04:58:52 PDT
Created attachment 346377 [details] Archive of layout-test-results from ews106 for mac-sierra-wk2 The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews. Bot: ews106 Port: mac-sierra-wk2 Platform: Mac OS X 10.12.6
EWS Watchlist
Comment 8 2018-08-02 09:41:59 PDT
Comment on attachment 346372 [details] Patch Attachment 346372 [details] did not pass win-ews (win): Output: https://webkit-queues.webkit.org/results/8738599 New failing tests: fonts/monospace.html
EWS Watchlist
Comment 9 2018-08-02 09:42:10 PDT
Created attachment 346385 [details] Archive of layout-test-results from ews206 for win-future The attached test failures were seen while running run-webkit-tests on the win-ews. Bot: ews206 Port: win-future Platform: CYGWIN_NT-6.1-2.9.0-0.318-5-3-x86_64-64bit
EWS Watchlist
Comment 10 2018-08-02 11:26:59 PDT
Comment on attachment 346372 [details] Patch Attachment 346372 [details] did not pass ios-sim-ews (ios-simulator-wk2): Output: https://webkit-queues.webkit.org/results/8739358 New failing tests: imported/w3c/web-platform-tests/service-workers/service-worker/oninstall-script-error.https.html imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-waituntil.https.html http/tests/workers/service/install-fails.html imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-async-waituntil.https.html
EWS Watchlist
Comment 11 2018-08-02 11:27:01 PDT
Created attachment 346397 [details] Archive of layout-test-results from ews126 for ios-simulator-wk2 The attached test failures were seen while running run-webkit-tests on the ios-sim-ews. Bot: ews126 Port: ios-simulator-wk2 Platform: Mac OS X 10.13.4
Yusuke Suzuki
Comment 12 2018-08-02 11:56:08 PDT
Created attachment 346400 [details] Patch WIP
Yusuke Suzuki
Comment 13 2018-08-02 12:12:48 PDT
Created attachment 346405 [details] Patch WIP
EWS Watchlist
Comment 14 2018-08-02 13:33:38 PDT
Comment on attachment 346405 [details] Patch Attachment 346405 [details] did not pass mac-wk2-ews (mac-wk2): Output: https://webkit-queues.webkit.org/results/8741138 New failing tests: http/tests/workers/service/install-fails.html imported/w3c/web-platform-tests/service-workers/service-worker/unregister-then-register-new-script.https.html imported/w3c/web-platform-tests/service-workers/service-worker/oninstall-script-error.https.html imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-async-waituntil.https.html imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-waituntil.https.html imported/w3c/web-platform-tests/service-workers/service-worker/register-same-scope-different-script-url.https.html
EWS Watchlist
Comment 15 2018-08-02 13:33:40 PDT
Created attachment 346412 [details] Archive of layout-test-results from ews104 for mac-sierra-wk2 The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews. Bot: ews104 Port: mac-sierra-wk2 Platform: Mac OS X 10.12.6
EWS Watchlist
Comment 16 2018-08-02 13:58:46 PDT
Comment on attachment 346405 [details] Patch Attachment 346405 [details] did not pass ios-sim-ews (ios-simulator-wk2): Output: https://webkit-queues.webkit.org/results/8741052 New failing tests: imported/w3c/web-platform-tests/service-workers/service-worker/oninstall-script-error.https.html imported/w3c/web-platform-tests/service-workers/service-worker/unregister-then-register-new-script.https.html imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-waituntil.https.html http/tests/workers/service/install-fails.html imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-async-waituntil.https.html
EWS Watchlist
Comment 17 2018-08-02 13:58:49 PDT
Created attachment 346415 [details] Archive of layout-test-results from ews121 for ios-simulator-wk2 The attached test failures were seen while running run-webkit-tests on the ios-sim-ews. Bot: ews121 Port: ios-simulator-wk2 Platform: Mac OS X 10.13.4
EWS Watchlist
Comment 18 2018-08-02 16:35:00 PDT
Comment on attachment 346405 [details] Patch Attachment 346405 [details] did not pass win-ews (win): Output: https://webkit-queues.webkit.org/results/8742530 New failing tests: http/tests/security/canvas-remote-read-remote-video-blocked-no-crossorigin.html
EWS Watchlist
Comment 19 2018-08-02 16:35:12 PDT
Created attachment 346428 [details] Archive of layout-test-results from ews200 for win-future The attached test failures were seen while running run-webkit-tests on the win-ews. Bot: ews200 Port: win-future Platform: CYGWIN_NT-6.1-2.9.0-0.318-5-3-x86_64-64bit
Yusuke Suzuki
Comment 20 2018-08-03 11:32:52 PDT
Created attachment 346508 [details] Patch WIP
EWS Watchlist
Comment 21 2018-08-03 12:34:46 PDT
Comment on attachment 346508 [details] Patch Attachment 346508 [details] did not pass mac-wk2-ews (mac-wk2): Output: https://webkit-queues.webkit.org/results/8753608 New failing tests: imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-async-waituntil.https.html
EWS Watchlist
Comment 22 2018-08-03 12:34:48 PDT
Created attachment 346517 [details] Archive of layout-test-results from ews106 for mac-sierra-wk2 The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews. Bot: ews106 Port: mac-sierra-wk2 Platform: Mac OS X 10.12.6
Yusuke Suzuki
Comment 23 2018-08-03 13:23:42 PDT
(In reply to Build Bot from comment #21) > Comment on attachment 346508 [details] > Patch > > Attachment 346508 [details] did not pass mac-wk2-ews (mac-wk2): > Output: https://webkit-queues.webkit.org/results/8753608 > > New failing tests: > imported/w3c/web-platform-tests/service-workers/service-worker/extendable- > event-async-waituntil.https.html I looked the test failures carefully, and I think that this test checks unspecified behavior in the service worker spec. The one of the failing test is 'no-current-extension-different-microtask'. The concept of this test is the following. function eventHandlerInSW(event) { Promise.resolve().then(function () { event.waitUntil(Promise.resolve()); // The test expects this throws InvalidStateError. }); } I cannot find why this occurs in the spec. When dispatching an event, event's dispatch flag is set. This should be set while draining microtasks since this event handler is JS code, and this event handler exhausts JS stacks and microtask queue when finishing. This phase should be before than unsetting dispatch flag. But maybe I'm wrong. Do you know what spec part this test checks, Ryosuke, Chris, or Youenn?
Yusuke Suzuki
Comment 24 2018-08-03 13:31:56 PDT
(In reply to Yusuke Suzuki from comment #23) > I looked the test failures carefully, and I think that this test checks > unspecified behavior in the service worker spec. > The one of the failing test is 'no-current-extension-different-microtask'. > The concept of this test is the following. > > function eventHandlerInSW(event) { > Promise.resolve().then(function () { > event.waitUntil(Promise.resolve()); // The test expects this throws > InvalidStateError. > }); > } > > I cannot find why this occurs in the spec. When dispatching an event, > event's dispatch flag is set. > This should be set while draining microtasks since this event handler is JS > code, and this event handler exhausts JS stacks and microtask queue when > finishing. This phase should be before than unsetting dispatch flag. > But maybe I'm wrong. Do you know what spec part this test checks, Ryosuke, > Chris, or Youenn? OK, let them fail. These tests are wrong :) https://github.com/web-platform-tests/wpt/pull/8936 https://github.com/w3c/ServiceWorker/issues/1213#issuecomment-342640579
EWS Watchlist
Comment 25 2018-08-03 13:32:39 PDT
Comment on attachment 346508 [details] Patch Attachment 346508 [details] did not pass ios-sim-ews (ios-simulator-wk2): Output: https://webkit-queues.webkit.org/results/8753939 New failing tests: imported/w3c/web-platform-tests/service-workers/service-worker/extendable-event-async-waituntil.https.html
EWS Watchlist
Comment 26 2018-08-03 13:32:41 PDT
Created attachment 346543 [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.13.4
Yusuke Suzuki
Comment 27 2018-08-03 13:38:35 PDT
Created attachment 346544 [details] Patch Passing tests!
Yusuke Suzuki
Comment 28 2018-08-03 14:27:33 PDT
Yusuke Suzuki
Comment 29 2018-08-03 14:50:38 PDT
Finally it's ready :D
Yusuke Suzuki
Comment 30 2018-08-03 16:24:40 PDT
Comment on attachment 346548 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=346548&action=review > Source/WebKit/ChangeLog:8 > + Rename JSMainThreadNullState to JSNullState. Ah, I'll keep this name because, 1. This JSMainThreadNullState is only used from the main thread. 2. It includes CustomElementReactionStack, which is only effective in the main thread.
Yusuke Suzuki
Comment 31 2018-08-03 16:33:18 PDT
Yusuke Suzuki
Comment 32 2018-08-03 16:36:00 PDT
Yusuke Suzuki
Comment 33 2018-08-03 16:36:27 PDT
OK, renaming is done, ready for review.
Darin Adler
Comment 34 2018-08-05 15:42:23 PDT
Comment on attachment 346561 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=346561&action=review > Source/WebCore/ChangeLog:3 > + Add the support for micro tasks in workers Grammar error here: the word "the" should be omitted > Source/WebCore/ChangeLog:10 > + This patch adds microtask mechanism to workers. To adopt the existing microtask mechanism in main thread, > + we extends JSMainThreadExecState for non-main-threads. We rename it to JSExecState, and store stacked > + ExecState* data in thread local storage in ThreadGlobalData instead of a static variable s_mainThreadState. Some other minor grammar errors here. Should read: This patch adds the microtask mechanism to workers. To adopt the existing microtask mechanism from the main thread, we extend JSMainThreadExecState for non-main-threads. We rename it to JSExecState, and store stacked ExecState* data in thread local storage in ThreadGlobalData instead of a static variable s_mainThreadState. > Source/WebCore/bindings/js/JSMicrotaskCallback.h:42 > + Ref<JSMicrotaskCallback> protectedThis(*this); Would it be nicer to just protect m_task, not the entire JSMicrotaskCallback? Another way to write this in modern code is: auto protectedThis { makeRef(*this) }; > Source/WebCore/bindings/js/JSMicrotaskCallback.h:49 > + ExecState* exec = m_globalObject->globalExec(); > + > + JSExecState::runTask(exec, m_task); I’d be tempted to do this in a single line instead of using a local variable.
Darin Adler
Comment 35 2018-08-05 15:43:42 PDT
Comment on attachment 346561 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=346561&action=review > Source/WebCore/platform/ThreadGlobalData.h:36 > +namespace JSC { > +class ExecState; > +} // namespace JSC > namespace WebCore { I think we should have a space after this. Not sure we should comment the closing brace of this tiny thing either.
Yusuke Suzuki
Comment 36 2018-08-05 21:34:07 PDT
Comment on attachment 346561 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=346561&action=review Thank you! >> Source/WebCore/ChangeLog:3 >> + Add the support for micro tasks in workers > > Grammar error here: the word "the" should be omitted Thanks, fixed. >> Source/WebCore/ChangeLog:10 >> + ExecState* data in thread local storage in ThreadGlobalData instead of a static variable s_mainThreadState. > > Some other minor grammar errors here. Should read: > > This patch adds the microtask mechanism to workers. To adopt the existing microtask mechanism from the main thread, > we extend JSMainThreadExecState for non-main-threads. We rename it to JSExecState, and store stacked > ExecState* data in thread local storage in ThreadGlobalData instead of a static variable s_mainThreadState. Nice, fixed. >> Source/WebCore/bindings/js/JSMicrotaskCallback.h:42 >> + Ref<JSMicrotaskCallback> protectedThis(*this); > > Would it be nicer to just protect m_task, not the entire JSMicrotaskCallback? > > Another way to write this in modern code is: > > auto protectedThis { makeRef(*this) }; Nice. Since I would like to keep this JSMicrotaskCallback protected in this patch (it prevents us from removing JSDOMGlobalObject), I would like to use `auto protectedThis { makeRef(*this) };`. We should consider it in a separate patch. Opened! https://bugs.webkit.org/show_bug.cgi?id=188338 >> Source/WebCore/bindings/js/JSMicrotaskCallback.h:49 >> + JSExecState::runTask(exec, m_task); > > I’d be tempted to do this in a single line instead of using a local variable. Looks nice. Fixed. >> Source/WebCore/platform/ThreadGlobalData.h:36 >> namespace WebCore { > > I think we should have a space after this. Not sure we should comment the closing brace of this tiny thing either. Sounds nice, fixed.
Yusuke Suzuki
Comment 37 2018-08-05 21:37:18 PDT
Radar WebKit Bug Importer
Comment 38 2018-08-05 21:53:29 PDT
Truitt Savell
Comment 39 2018-08-06 14:03:22 PDT
It looks like <https://trac.webkit.org/changeset/234586> is causing a test to be flakey. imported/w3c/web-platform-tests/service-workers/service-worker/skip-waiting-installed.https.html test history: https://webkit-test-results.webkit.org/dashboards/flakiness_dashboard.html#showAllRuns=true&tests=imported%2Fw3c%2Fweb-platform-tests%2Fservice-workers%2Fservice-worker%2Fskip-waiting-installed.https.html I was unable to reproduce the flakiness on r234585 but was able to reproduce it on 234586. Out of 500 repetitions of the test around 10-20 will fail.
Yusuke Suzuki
Comment 40 2018-08-07 03:06:40 PDT
(In reply to Truitt Savell from comment #39) > It looks like <https://trac.webkit.org/changeset/234586> is causing a test > to be flakey. > > imported/w3c/web-platform-tests/service-workers/service-worker/skip-waiting- > installed.https.html > > test history: > https://webkit-test-results.webkit.org/dashboards/flakiness_dashboard. > html#showAllRuns=true&tests=imported%2Fw3c%2Fweb-platform-tests%2Fservice- > workers%2Fservice-worker%2Fskip-waiting-installed.https.html > > I was unable to reproduce the flakiness on r234585 but was able to reproduce > it on 234586. Out of 500 repetitions of the test around 10-20 will fail. I'll look into this.
Yusuke Suzuki
Comment 41 2018-08-07 07:23:59 PDT
(In reply to Yusuke Suzuki from comment #40) > I'll look into this. It seems that this patch reveals the existing flakiness more explicitly. Previously we run 2 macrotasks on waitUntil's promise fulfillment. But this was wrong. We should use microtask here. The patch above fixes this. As a result, the handler can be called faster than before. And it may be resolved before skipWaiting's macrotask. Even before this patch, the order is not deterministic. But in the old implementation, we used macrotasks and it accidentally makes this test non-flaky. But essentially, this flakiness was here before this patch.
Yusuke Suzuki
Comment 42 2018-08-07 07:26:54 PDT
(In reply to Yusuke Suzuki from comment #41) > (In reply to Yusuke Suzuki from comment #40) > > I'll look into this. > > It seems that this patch reveals the existing flakiness more explicitly. > > Previously we run 2 macrotasks on waitUntil's promise fulfillment. > But this was wrong. We should use microtask here. The patch above fixes this. > > As a result, the handler can be called faster than before. > And it may be resolved before skipWaiting's macrotask. > > Even before this patch, the order is not deterministic. But in the old > implementation, we used macrotasks and it accidentally makes this test > non-flaky. > But essentially, this flakiness was here before this patch. I guess that the order checked in this test is not specified in the spec[1]. [1]: https://github.com/w3c/ServiceWorker/issues/1187#issuecomment-326488845
Yusuke Suzuki
Comment 43 2018-08-07 07:36:43 PDT
(In reply to Yusuke Suzuki from comment #42) > (In reply to Yusuke Suzuki from comment #41) > > (In reply to Yusuke Suzuki from comment #40) > > > I'll look into this. > > > > It seems that this patch reveals the existing flakiness more explicitly. > > > > Previously we run 2 macrotasks on waitUntil's promise fulfillment. > > But this was wrong. We should use microtask here. The patch above fixes this. > > > > As a result, the handler can be called faster than before. > > And it may be resolved before skipWaiting's macrotask. > > > > Even before this patch, the order is not deterministic. But in the old > > implementation, we used macrotasks and it accidentally makes this test > > non-flaky. > > But essentially, this flakiness was here before this patch. > > I guess that the order checked in this test is not specified in the spec[1]. > > [1]: https://github.com/w3c/ServiceWorker/issues/1187#issuecomment-326488845 We should mark this test flaky. https://github.com/w3c/ServiceWorker/issues/1187#issuecomment-397940023
Yusuke Suzuki
Comment 44 2018-08-07 07:39:51 PDT
(In reply to Yusuke Suzuki from comment #43) > (In reply to Yusuke Suzuki from comment #42) > > (In reply to Yusuke Suzuki from comment #41) > > > (In reply to Yusuke Suzuki from comment #40) > > > > I'll look into this. > > > > > > It seems that this patch reveals the existing flakiness more explicitly. > > > > > > Previously we run 2 macrotasks on waitUntil's promise fulfillment. > > > But this was wrong. We should use microtask here. The patch above fixes this. > > > > > > As a result, the handler can be called faster than before. > > > And it may be resolved before skipWaiting's macrotask. > > > > > > Even before this patch, the order is not deterministic. But in the old > > > implementation, we used macrotasks and it accidentally makes this test > > > non-flaky. > > > But essentially, this flakiness was here before this patch. > > > > I guess that the order checked in this test is not specified in the spec[1]. > > > > [1]: https://github.com/w3c/ServiceWorker/issues/1187#issuecomment-326488845 > > We should mark this test flaky. > https://github.com/w3c/ServiceWorker/issues/1187#issuecomment-397940023 FYI, blink also marks it flaky. https://bugs.chromium.org/p/chromium/issues/detail?id=831509
Yusuke Suzuki
Comment 45 2018-08-07 09:29:34 PDT
Note You need to log in before you can comment on or make changes to this bug.