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

Web backend crashes when initialized within a worker #1986

Closed
strax opened this issue Sep 21, 2021 · 3 comments
Closed

Web backend crashes when initialized within a worker #1986

strax opened this issue Sep 21, 2021 · 3 comments
Labels
area: correctness We're behaving incorrectly help required We need community help to make this happen. type: enhancement New feature or request

Comments

@strax
Copy link

strax commented Sep 21, 2021

Description
The web backend uses web_sys::window to obtain a handle to the global Navigator object. This does not work when called from a worker scope since there is no window global variable nor a Window interface.

Repro steps
Create a Worker that executes a Rust program compiled to WebAssembly calling wgpu::Instance::new. Unfortunately I don't have a minimal repro available at the moment.

Expected vs observed behavior
Observed behavior: the web backend tries to obtain a handle to the window object and performs an instanceof check against the Window interface, which does not exist in the worker scope. This results in an error:

Uncaught (in promise) ReferenceError: Window is not defined

Expected behavior:
In a worker scope, a web_sys::WorkerGlobalScope could be used instead to obtain the Navigator handle, but this might be tricky in practice (rustwasm/wasm-bindgen#1046). Since the program will crash and not return None when window is not available, it is not enough to use Option::or to fall back to, say, web_sys::global(). Based on rustwasm/wasm-bindgen#1982 (comment), this is a problem with other web_sys consumers too and might require some glue code like this.

Platform

  • wgpu version: master
@kvark kvark added area: correctness We're behaving incorrectly help required We need community help to make this happen. type: enhancement New feature or request labels Sep 21, 2021
@codeart1st
Copy link

codeart1st commented Mar 6, 2022

Can confirm this for me. Tried an experiment with OffscreenCanvas and run into the same problem.

Current solution for me in my worker.js is this hack:

self.Window = WorkerGlobalScope
self.window = self
self.window.document = {
  querySelectorAll: () => {
    return [canvas]
  }
}

Not great, but do the job. canvas is the transfered OffscreenCanvas instance.

Edit: #1932 create_surface_from_offscreen_canvas. So only the first line of the "hack" needs to be used.

Patryk27 pushed a commit to Patryk27/wgpu that referenced this issue Nov 23, 2022
Require at least version 0.7.1 of ron, this version changed how floating points are
serialized by forcing them to always have the decimal part, this makes it backwards
incompatible with our tests because we do a syntatic diff and not a semantic one.
@teoxoy
Copy link
Member

teoxoy commented Feb 22, 2023

I believe this was fixed by #2858, closing.

@teoxoy teoxoy closed this as completed Feb 22, 2023
@codeart1st
Copy link

codeart1st commented Feb 23, 2023

self.Window = WorkerGlobalScope is still needed for me.

Edit: Stack trace indicated that I was on my wrong branch without the updated wgpu dependency.
Edit: Tried it again on the correct branch and it works without any workarounds now. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: correctness We're behaving incorrectly help required We need community help to make this happen. type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants