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

Running wgpu in a secondary thread segfaults when main thread ends. #246

Closed
rukai opened this issue Jul 7, 2019 · 5 comments
Closed

Running wgpu in a secondary thread segfaults when main thread ends. #246

rukai opened this issue Jul 7, 2019 · 5 comments
Labels
type: bug Something isn't working

Comments

@rukai
Copy link
Contributor

rukai commented Jul 7, 2019

OS: Arch Linux
GPU: GTX 960
Driver: Nvidia proprietary driver

fn main() {
    std::thread::spawn(move || {
        triangle_example();
    });

    std::thread::sleep(std::time::Duration::from_millis(500));
}

Running the triangle example in a separate thread and then ending the main thread causes the wgpu thread to segfault.

repro: https://github.com/rukai/wgpu-rs/blob/segfault_repro/examples/repro/main.rs

@kvark kvark added the type: bug Something isn't working label Jul 7, 2019
@Aaron1011
Copy link
Contributor

Minimized reproduction: https://github.com/Aaron1011/wgpu-segfault

The issue occurs in gfx_backend_gl, which uses libloading to dynamically load the OpenGL shared library: https://github.com/gfx-rs/gfx/blob/07043744b4b05893363066ca0e50c5e38348f20c/src/backend/gl/src/window/egl.rs#L20-L23

When the libloading::Instance gets dropped when the thread function returns, it unloads the shared library. However, it appears that a TLS destructor remains registered for the shared library, despite the code being unloaded. When libc attempts to run the registered TLS destrutors on thread exit (after the Rust code has run), we get a segfault.

See nagisa/rust_libloading#41 for more details

@kvark
Copy link
Member

kvark commented Feb 22, 2021

@Aaron1011 any idea on what we should be doing?

@Aaron1011
Copy link
Contributor

@kvark: There's talk about changing libloading to only call dlopen when explicitly requested (instead of automatically on Drop): nagisa/rust_libloading#41 (comment)

@Aaron1011
Copy link
Contributor

For now, the best solution is probably to do something similar to this workaround: nagisa/rust_libloading#41 (comment)

Specifically, gfx_backend_gl should open the graphics driver shared library with RTLD_NODELETE.

@Aaron1011
Copy link
Contributor

From my testing, it looks like unloading libX11.so doesn't cause any problems - it's unloading libEGL.so that causes problems. For example, NVIDIA's libglvnd registers a pthread key destructor

Aaron1011 added a commit to Aaron1011/gfx that referenced this issue Feb 25, 2021
This pulls in a fix for timothee-haudebourg/khronos-egl#14,
which prevents segfaults when using this crate from another thread
(e.g. gfx-rs/wgpu#246)
bors bot added a commit to gfx-rs/gfx that referenced this issue Feb 25, 2021
3653: Bump `khronos-egl` dependency to version 4 r=kvark a=Aaron1011

This pulls in a fix for timothee-haudebourg/khronos-egl#14,
which prevents segfaults when using this crate from another thread
(e.g. gfx-rs/wgpu#246)


PR checklist:
- [X] `make` succeeds (on *nix)
- [X] `make reftests` succeeds
- [X] tested examples with the following backends:
  - GL


Co-authored-by: Aaron Hill <aa1ronham@gmail.com>
bors bot added a commit that referenced this issue Feb 26, 2021
1236: Update gfx to 0a201d1c406b5119ec11068293a40e50ec0be4c8 r=kvark a=Aaron1011

**Connections**
wgpu issue: #246
GFX PR: gfx-rs/gfx#3653
Underlying libloading issue: nagisa/rust_libloading#41

**Description**

Pulls in gfx-rs/gfx#3653,
which fixes a segfault when using wgpu from a non-main thread.

**Testing**

The example in #246 should run successfully. I'm not certain how to add an integration test to the repository.

Co-authored-by: Aaron Hill <aa1ronham@gmail.com>
@bors bors bot closed this as completed in 8b4bec5 Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants