Giter Club home page Giter Club logo

Comments (5)

white-axe avatar white-axe commented on June 8, 2024 1

It's happening because your application clones an egui::Context from the main thread and sends it to the worker thread, and then sometimes calls .request_repaint() on the egui::Context from the worker thread. An egui::Context is actually a std::sync::Arc<parking_lot::RwLock<ContextImpl>> and calling .request_repaint() or any other operation on the context locks it, so your application probably did any operation on the main thread that requires the context at the exact same time the worker thread was in the middle of calling .request_repaint().

One easy way around this is to not send the context to the worker thread at all, and instead use an asynchronous task running on the main thread to listen for repaint requests from the worker thread.

For example, using tokio::sync::mpsc:

let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
wasm_bindgen_futures::spawn_local(async move {
    loop {
        let Some(message) = rx.recv().await else {
            break;
        };
        do_something_with_message(message);
        ctx.request_repaint();
    }
});

Now send the tx to the worker thread, and whenever the worker thread wants to repaint it can call tx.send(message).

from egui.

white-axe avatar white-axe commented on June 8, 2024

You need to enable the nightly feature of parking_lot

from egui.

zdimension avatar zdimension commented on June 8, 2024

You need to enable the nightly feature of parking_lot

Thanks, this fixed it (though, was this documented anywhere in egui?), though since the stdsimd feature was renamed to stdarch_wasm_atomic_wait, the latest crates.io release of parking_lot doesn't work as is, I had to manually patch parking_lot_core to use the Git repo which contains the fix:

[patch.crates-io]
# TODO: update this when parking_lot_core > 0.9.9 is released
# https://github.com/Amanieu/parking_lot/pull/435#issuecomment-2072218790
parking_lot_core = { git = "https://github.com/Amanieu/parking_lot" }

And, then run cargo update to fetch it of course.

This patch will be necessary until the next release of parking_lot_core.

Keeping this open since @emilk kept my other issue open as a reminder to add to docs or add the feature in eframe. Feel free to close it if it's not relevant

from egui.

zdimension avatar zdimension commented on June 8, 2024

@white-axe pinging you since you seem to have a lot of knowledge in the arcane bits of parking_lot, I'm having another issue with egui and threads that I'm having trouble to pinpoint the origin of: (same issue, triggered from multiple points randomly each time I refresh)

image

image

Seems like it's trying to lock the context data to access it, but if it's already locked by someone else (a thread in the middle of calling request_repaint, maybe?) then it just crashes since Atomics.wait can't be used to block the main thread.

(should I open another issue for this?)

from egui.

zdimension avatar zdimension commented on June 8, 2024

Ah, thanks, I hadn't thought of using a local future for that. I replaced pretty much all my cross-thread request_repaint calls with that for my WASM build (still use request_repaint on desktop since it works) and it works flawlessly.

from egui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.