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

WebGL and WebGPU backends should not panic if canvas context is unavailable #3017

Closed
kpreid opened this issue Sep 10, 2022 · 0 comments · Fixed by #3052
Closed

WebGL and WebGPU backends should not panic if canvas context is unavailable #3017

kpreid opened this issue Sep 10, 2022 · 0 comments · Fixed by #3052

Comments

@kpreid
Copy link
Contributor

kpreid commented Sep 10, 2022

The WebGL and WebGPU backends have code which expect()s success in obtaining a canvas:

let context: wasm_bindgen::JsValue = match canvas.get_context("webgpu") {
Ok(Some(ctx)) => ctx.into(),
_ => panic!("expected to get context from canvas"),
};

let webgl2_context = canvas
.get_context_with_context_options("webgl2", &Self::create_context_options())
.expect("Cannot create WebGL2 context")
.and_then(|context| context.dyn_into::<web_sys::WebGl2RenderingContext>().ok())
.expect("Cannot convert into WebGL2 context");

These will panic if the browser declines to provide a context, which may happen for reasons outside of the application programmer's control (browser does not support WebGPU or WebGL2, insufficient GPU memory, browser glitches) as well as ones which are (such as trying to use the same canvas for "2d" drawing). Therefore, the application may have reason to handle the error (if nothing else, to give the user an error message visible on the web page), which is best done by returning an Err rather than panicking — especially as Rust on wasm32 does not support unwinding, so every panic is fatal to the Rust module and can at best be handled from JavaScript.

This is not a solely theoretical scenario — I noticed the problem due to some Bevy Jam #2 games failing to start.

Platform
web, wgpu 0.13

kpreid added a commit to kpreid/wgpu that referenced this issue Sep 27, 2022
…ebGL 2).

Part of fixing gfx-rs#3017. This commit changes the handling of `web_sys`
errors and nulls from `expect()` to returning `Err`, but it doesn't
actually affect the public API — that will be done in the next commit.
kpreid added a commit to kpreid/wgpu that referenced this issue Sep 27, 2022
kpreid added a commit to kpreid/wgpu that referenced this issue Nov 25, 2022
…ebGL 2).

Part of fixing gfx-rs#3017. This commit changes the handling of `web_sys`
errors and nulls from `expect()` to returning `Err`, but it doesn't
actually affect the public API — that will be done in the next commit.
kpreid added a commit to kpreid/wgpu that referenced this issue Nov 25, 2022
kpreid added a commit to kpreid/wgpu that referenced this issue Dec 1, 2022
…ebGL 2).

Part of fixing gfx-rs#3017. This commit changes the handling of `web_sys`
errors and nulls from `expect()` to returning `Err`, but it doesn't
actually affect the public API — that will be done in the next commit.
kpreid added a commit to kpreid/wgpu that referenced this issue Dec 1, 2022
cwfitzgerald pushed a commit that referenced this issue Dec 1, 2022
…le (#3052)

* Low-level error handling in canvas context creation (for WebGPU and WebGL 2).

Part of fixing #3017. This commit changes the handling of `web_sys`
errors and nulls from `expect()` to returning `Err`, but it doesn't
actually affect the public API — that will be done in the next commit.

* Breaking: Change type of `create_surface()` functions to expose canvas errors.

Part of fixing #3017.
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 a pull request may close this issue.

1 participant