tauri: [bug] `run_iteration` example busy-loops on Windows

Describe the bug

I’m getting this output indicating a busy loop:

image

Ran Tauri iteration at 4.7094867s
Ran Tauri iteration at 4.7097679s
Ran Tauri iteration at 4.7099947s
Ran Tauri iteration at 4.710248s
Ran Tauri iteration at 4.7104644s
Ran Tauri iteration at 4.7106774s
Ran Tauri iteration at 4.7108921s
Ran Tauri iteration at 4.7111063s
Ran Tauri iteration at 4.7113324s
Ran Tauri iteration at 4.7115689s
Ran Tauri iteration at 4.7118283s
Ran Tauri iteration at 4.7120621s
Ran Tauri iteration at 4.7122877s
Ran Tauri iteration at 4.712554s
Ran Tauri iteration at 4.8543264s

Reproduction

https://github.com/ReactorScram/tauri_repro

Run cargo tauri dev or cd src-tauri; cargo run. stdout will be filled with constant print statements.

fn main() {
    let mut app = tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .build(tauri::generate_context!())
        .expect("should be able to build the Tauri app");

    let start_time = std::time::Instant::now();

    loop {
        let iteration = app.run_iteration();
        println!("Ran Tauri iteration at {:?}", start_time.elapsed());
        if iteration.window_count == 0 {
            break;
        }
    }
}

Expected behavior

I expected from #5532 that run_iteration would block until the main event loop receives an event, then process it, then return. Instead it doesn’t seem to block, at least on Windows.

This is part of a larger yak shave for me. I’m trying to join a worker thread after Tauri exits, but all these methods have failed:

  • app.exit doesn’t trigger RunEvent::Exit (intentional behavior per #4662), so I can’t clean up in there
  • run_iteration busy loops
  • No way to inject my own RunEvent into the main loop to wake up run and check whether we need to clean up
  • No known way to just stop the Tauri event loop and let things Drop naturally, per #7358
  • No way to register something like on_exit with Builder https://docs.rs/tauri/latest/tauri/struct.Builder.html

My fallback plan is to write my own cleanup function and call that, and have it call app.exit at the end. It’ll work, but it’ll require some comments explaining it.

Full tauri info output

(I'm on Windows 11 Home. I'm not sure why it says Windows 10.)

WARNING: no lock files found, defaulting to npm

[✔] Environment
    - OS: Windows 10.0.22621 X64
    ✔ WebView2: 120.0.2210.133
    ✔ MSVC:
        - Visual Studio Build Tools 2019
        - Visual Studio Community 2022
    ✔ rustc: 1.75.0 (82e1608df 2023-12-21)
    ✔ cargo: 1.75.0 (1d8b05cdd 2023-11-20)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (environment override by RUSTUP_TOOLCHAIN)
    - node: 20.10.0
    - npm: 10.2.3

[-] Packages
    - tauri [RUST]: 1.5.4
    - tauri-build [RUST]: 1.5.1
    - wry [RUST]: 0.24.7
    - tao [RUST]: 0.16.6
    - tauri-cli [RUST]: 1.5.6
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli [NPM]: 1.5.6 (outdated, latest: 1.5.9)

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../src
    - devPath: ../src

Stack trace

N/A

Additional context

If this is a platform limitation, like maybe it’s just impossible to gracefully stop a GUI event loop on Windows, I would understand.

But if it’s not… can I just have an app.stop that stops the event loop, so everything can Drop?

Seems to be a tao thing, see later comment

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Having just spent a couple days trying to gracefully clean up a background future, this whole situation needs to be better documented if nothing else. Exiting with std::process::exit means a lot of normal Rust assumptions don’t hold and the reason why is not immediately obvious.

This can be closed now that #12668 has been shipped.

@arialpew I have a similar case, I have a worker thread for COM that I’d like to join gracefully. I intercept the exit events and do my own graceful shutdown within a controller task, before allowing Tauri to crash the process: https://github.com/firezone/firezone/blob/main/rust/windows-client/src-tauri/src/client/gui.rs#L608

I didn’t end up needing unsafe for this.

Edit: I dug this up - It’s a limitation of tao, not Tauri itself, and tao has some justification for it that I don’t fully understand 😕 https://docs.rs/tao/latest/x86_64-pc-windows-msvc/tao/event_loop/struct.EventLoop.html#method.run

I would have thought after more than 20 years of GUI paradigms, the major OSes would have made GUIs as nice to program as CLIs. Oh well.

I thought so too at one point, until i got into working on (instead of “with”) a gui framework. Now i cry myself to sleep every night 🤷

That said, not having an easier way to do custom clean up is on us and our api design. (No idea about the event loop stuff thi issue was about though)

iirc on_drop only runs if you use remove_plugin, it’s not part of the cleanup that app.exit() runs (it only cleans up the tray and Commands i think)