Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shared worker support #3345

Merged
merged 1 commit into from
Feb 21, 2024
Merged

Add shared worker support #3345

merged 1 commit into from
Feb 21, 2024

Conversation

beaufortfrancois
Copy link
Collaborator

@beaufortfrancois beaufortfrancois commented Jan 29, 2024

Spec PR: gpuweb/gpuweb#4465


Requirements for PR author:

  • All missing test coverage is tracked with "TODO" or .unimplemented().
  • New helpers are /** documented */ and new helper files are found in helper_index.txt.
  • Test behaves as expected in a WebGPU implementation. (If not passing, explain above.)

Requirements for reviewer sign-off:

  • Tests are properly located in the test tree.
  • Test descriptions allow a reader to "read only the test plans and evaluate coverage completeness", and accurately reflect the test code.
  • Tests provide complete coverage (including validation control cases). Missing coverage MUST be covered by TODOs.
  • Helpers and types promote readability and maintainability.

When landing this PR, be sure to make any necessary issue status updates.

@beaufortfrancois beaufortfrancois force-pushed the shared-worker branch 2 times, most recently from 223ea6f to 7beccb6 Compare February 5, 2024 16:49
@beaufortfrancois beaufortfrancois changed the title Add shared worker support Add service worker and shared worker support Feb 5, 2024
@beaufortfrancois
Copy link
Collaborator Author

@kainino0x The file src/common/runtime/helper/test_worker-worker.ts fails to load as a service worker with the following error:

file_loader.ts:101 Uncaught (in promise) TypeError: import() is disallowed on ServiceWorkerGlobalScope by the HTML specification. See https://github.com/w3c/ServiceWorker/issues/1356.
    at DefaultTestFileLoader.listing (file_loader.ts:101:36)
    at loadTreeForQuery (tree.ts:294:30)
    at DefaultTestFileLoader.loadTree (file_loader.ts:80:24)
    at DefaultTestFileLoader.loadCases (file_loader.ts:94:29)
    at WindowClient.reportTestResults (test_worker-worker.ts:43:45)
    at self.onmessage (test_worker-worker.ts:55:21)

I believe the error comes from the existing code below as dynamic import is not supported in the context of a service worker (unlike shared and dedicated worker):

export class DefaultTestFileLoader extends TestFileLoader {
  async listing(suite: string): Promise<TestSuiteListing> {
    return ((await import(`../../${suite}/listing.js`)) as ListingFile).listing;
  }

  import(path: string): Promise<SpecFile> {
    return import(`../../${path}`);
  }
}

Do you know by any chance how to solve this?

@kainino0x
Copy link
Collaborator

Ugh, that's an unfortunate limitation. CTS is heavily built around dynamic imports - it's the only way we can run a subset of tests without loading every JS file in the entire repository.

In order to work around this, proxying to ServiceWorker probably has to look something like, make one ServiceWorker for each .spec.ts that we want to run:

  • Generate a JavaScript string that statically imports the .spec.ts file and wraps it in a thing that communicates with the main page to run the test (like DedicatedWorker does now)
  • Launch a service worker using that generated JavaScript
  • Run whatever tests from that file that we need to run
  • Shut down the service worker (I have no idea how this works - can we safely do it on page unload?)

@beaufortfrancois
Copy link
Collaborator Author

Thank you @kainino0x. I have to admit I'm not sure how to apply your suggestions yet.

@beaufortfrancois
Copy link
Collaborator Author

  • Launch a service worker using that generated JavaScript

Are you thinking of postMessage() the generated JavaScript and use eval() in service worker?

  • Shut down the service worker (I have no idea how this works - can we safely do it on page unload?)

I believe* we can use https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/unregister in cleanup stages.

@kainino0x
Copy link
Collaborator

  • Launch a service worker using that generated JavaScript

Are you thinking of postMessage() the generated JavaScript and use eval() in service worker?

No, I don't think you can eval a static import. That would be a dynamic import which ServiceWorker is supposed to disallow, presumably for some good reason.

I mean create the worker from a blob from a string: https://web.dev/articles/workers-basics#inline_workers

@beaufortfrancois
Copy link
Collaborator Author

  • Launch a service worker using that generated JavaScript

Are you thinking of postMessage() the generated JavaScript and use eval() in service worker?

No, I don't think you can eval a static import. That would be a dynamic import which ServiceWorker is supposed to disallow, presumably for some good reason.

I mean create the worker from a blob from a string: https://web.dev/articles/workers-basics#inline_workers

I wish I could do that but Chrome throws this error when I create a service worker from a blob URL:

Uncaught (in promise) TypeError: Failed to register a ServiceWorker: The URL protocol of the script ('blob:http://localhost:8080/a3ad9992-eb30-487c-818b-c9304f926f14') is not supported.

@beaufortfrancois
Copy link
Collaborator Author

@kainino0x Can you think of other ways to properly test service worker support in the CTS?

@kainino0x
Copy link
Collaborator

We could:

  • build-time generate one file which statically imports every test file. It would be slow to load though so it wouldn't be suitable for running many tests in separate page loads (as is done in all automated test environments).
  • build-time generate a worker wrapper foreach .spec.ts file which statically imports the test file and provides the message interface to run it. This would generate a lot of files in the output and need custom support in the dev-server, but is otherwise much better.

@beaufortfrancois
Copy link
Collaborator Author

build-time generate a worker wrapper foreach .spec.ts file which statically imports the test file and provides the message interface to run it. This would generate a lot of files in the output and need custom support in the dev-server, but is otherwise much better.

I don't know how to apply this suggestion sadly. Could you provide detailed instructions? Or a similar example in the CTS?

@kainino0x
Copy link
Collaborator

Sorry, I haven't had time this week because I'll be out next week.
Can we add SharedWorker first and solve ServiceWorker later? Or does SharedWorker have the same constraints?
It would be great to land this PR first and the complicated ServiceWorker one later.
(Though of course I won't be available to review until I'm back. You can get someone else to review.)

I have opened a draft PR which hopefully does the annoying build configuration stuff for you. #3415

@beaufortfrancois beaufortfrancois force-pushed the shared-worker branch 7 times, most recently from 5a708a6 to edd088f Compare February 19, 2024 11:56
@beaufortfrancois beaufortfrancois changed the title Add service worker and shared worker support Add shared worker support Feb 19, 2024
@beaufortfrancois
Copy link
Collaborator Author

beaufortfrancois commented Feb 19, 2024

Can we add SharedWorker first and solve ServiceWorker later? Or does SharedWorker have the same constraints?
It would be great to land this PR first and the complicated ServiceWorker one later.

Got it. I've updated this PR for Shared Worker support only.
I've opened a new one for ServiceWorker at #3419. Thank you @kainino0x

@beaufortfrancois beaufortfrancois force-pushed the shared-worker branch 3 times, most recently from a2041b0 to d4a925c Compare February 19, 2024 13:51
@greggman
Copy link
Contributor

Do we need all the tests to run in a ServiceWorker? I feel like maybe we only need to test that you can create a device and you can do a few basic things (run a render pass, run a compute pass, create a buffer, texture, ....) We have a basic test for regular workers that runs in the main suite when running in the main thread. Maybe we should just add the same for shared and service workers?

src/webgpu/web_platform/worker/worker.spec.ts

Copy link
Contributor

@greggman greggman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@beaufortfrancois
Copy link
Collaborator Author

Do we need all the tests to run in a ServiceWorker? I feel like maybe we only need to test that you can create a device and you can do a few basic things (run a render pass, run a compute pass, create a buffer, texture, ....) We have a basic test for regular workers that runs in the main suite when running in the main thread. Maybe we should just add the same for shared and service workers?

Basic tests for shared workers (in this PR) and service workers are available in http://localhost:8080/standalone/?q=webgpu:web_platform,worker,worker:*

image

@beaufortfrancois
Copy link
Collaborator Author

@greggman Please merge if it looks good to you.

@greggman greggman enabled auto-merge (squash) February 21, 2024 15:32
@greggman greggman merged commit cdb6b22 into main Feb 21, 2024
1 check passed
@greggman greggman deleted the shared-worker branch February 21, 2024 15:35
dj2 added a commit to dj2/gpuweb-cts that referenced this pull request Feb 26, 2024
This reverts commit cdb6b22.

This is an attempt to fix issues we're seeing when rolling the CTS into
Dawn.
dj2 added a commit that referenced this pull request Feb 26, 2024
This reverts commit cdb6b22.

This is an attempt to fix issues we're seeing when rolling the CTS into
Dawn.
dj2 added a commit to dj2/gpuweb-cts that referenced this pull request Feb 27, 2024
This reverts commit 55e6eea.

Attempting to land this again to see if it causes any infra issues when
rolled into Chromium.
dj2 added a commit that referenced this pull request Feb 27, 2024
This reverts commit 55e6eea.

Attempting to land this again to see if it causes any infra issues when
rolled into Chromium.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants