galileo icon indicating copy to clipboard operation
galileo copied to clipboard

[macOS/iOS] Crash when calling UI APIs from non-UI thread

Open spencer741 opened this issue 11 months ago • 2 comments

Problem Description

When attempting to create a new surface on macOS/iOS platforms using <Your Project> which internally uses wgpu, the application crashes if the call is made from a non-UI thread. This issue specifically arises in the context of wgpu-hal when attempting to interact with Metal's get_metal_layer from a thread other than the main UI thread.

Running the many_layers demo on MacOS yields this crash.

[2024-03-04T20:28:25Z INFO  galileo::render::wgpu] Creating new surface
thread 'tokio-runtime-worker' panicked at ../.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-hal-0.19.3/src/metal/surface.rs:113:13:
get_metal_layer cannot be called in non-ui thread.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected Behavior

Don't crash on MacOS. Haven't tested on IOS, but assuming this is a problem? Internally ensure that UI API calls are dispatched to the main thread to prevent such crashes. This is where gfx-rs prevents create_surface calls from happening if not on the UI thread. https://github.com/gfx-rs/wgpu/pull/2736

Possible Fixes

Looks like we spawn a new thread here which makes an otherwise synchronous event loop be asynchronous in nature, would there be a problem making this synchronous? Was looking through this commit:

spencer741 avatar Mar 04 '24 21:03 spencer741