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

Instant crash. Something to do with my std::thread implementation #2546

Closed
NotAF0e opened this issue Jan 5, 2023 · 5 comments · Fixed by #2625
Closed

Instant crash. Something to do with my std::thread implementation #2546

NotAF0e opened this issue Jan 5, 2023 · 5 comments · Fixed by #2625
Labels
bug Something is broken

Comments

@NotAF0e
Copy link

NotAF0e commented Jan 5, 2023

Describe the bug
Application crashes instantly with no errors

To Reproduce
Open application

Expected behaviour
Application does not crash and runs fine

I'm on win11

This has something to do with my threading code as the app was just ok before I added threading to decouple my program's calculations and rendering.

@NotAF0e NotAF0e added the bug Something is broken label Jan 5, 2023
@parasyte
Copy link
Contributor

parasyte commented Jan 5, 2023

There doesn't appear to be enough information for this report to be actionable. A minimal reproduction is the best way for anyone to figure out how to fix it.

Since I'm left with no other options, I could take a wild guess that you might be running into issues with reentrancy on the internal lock within egui::Context. This issue is trivial to create and leads to deadlock. It isn't a "crash" in any sense other than the application freezes when this condition occurs.

@NotAF0e
Copy link
Author

NotAF0e commented Jan 6, 2023

I'm not sure I would be able to create a minimal repo as just refactoring the code to work with a second thread had me contacting many people and failing twice.

@NotAF0e
Copy link
Author

NotAF0e commented Jan 6, 2023

I will attempt to show what part may be causing issues later without minimal repo

@NotAF0e
Copy link
Author

NotAF0e commented Jan 6, 2023

let sim_data_arc = Arc::clone(&app.sim_data);
    let checks_arc = Arc::clone(&app.checks);

    // Does simulation calculations
    thread::spawn(move || {
        let mut sim_data_lock = sim_data_arc.lock().unwrap();
        let mut checks_arc_lock = checks_arc.lock().unwrap();

        //loop {
        // Creates Adam and Eve
        if *&checks_arc_lock.data[0] == 0 {
            for _ in 0..1 { // Manually change amount of people at start here
                let john: Person = sim_data_lock.create_person(Sex::Male);
                let john2: Person = sim_data_lock.create_person(Sex::Female);
                sim_data_lock.people.push(john);
                sim_data_lock.people.push(john2);
            }

            checks_arc_lock.data[0] = 1;
        }

        if *&checks_arc_lock.data[1] != 0 {
            sim_data_lock.update_sim();
            sim_data_lock.update_details();
            let pop = &mut sim_data_lock;
            let pop_len = &mut pop.people.len();
            // Graph data pushing
            pop.graph_data.push([
                *&checks_arc_lock.start_months as f64 - *&checks_arc_lock.data[1] as f64,
                *pop_len as f64]);

            sim_data_lock.people.retain(|person| person.age != -1);
            checks_arc_lock.data[1] -= 1;
        }
        thread::sleep(Duration::from_secs(1));
        //}
    });

This is what i believe is causing problems

@parasyte
Copy link
Contributor

parasyte commented Jan 6, 2023

I can't see all of the types in that code, but which part is relevant to egui?

I speculate the locks that this thread holds reference state that the GUI will draw. If that is the case, you need to drop the lock guards before you sleep the thread. With the structure of your code, the locks will be held over the sleep and that will starve the GUI thread while it attempts to acquire the lock.

But this is again just speculation, and I do not know exactly how this code relates to egui.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants