tauri-apps/tauri

[bug] If you set a Submenu as the root menu of a tray icon on Windows, then click it, it will crash

ReactorScram opened this issue · 3 comments

Describe the bug

Left-clicking or right-clicking on the tray icon causes the app to crash, only if a Submenu is set as the menu for the tray icon.

Reproduction

https://github.com/ReactorScram/tauri-tray-icon-repro/blob/bf153a1973730245ed29784dc842489bd54b137f/src-tauri/src/lib.rs

Use pnpm tauri dev or cargo run. Then left-click or right-click on the tray icon. The program exits with code 0xc000041d.

Reproduces for me on Windows 11, both x86_64 and aarch64.

Doesn't seem to reproduce on Ubuntu.

Using a regular Menu does not seem to crash at all.

Expected behavior

  • If Submenu is not meant to be used here, the type system should prevent compiling this.
  • If this is a runtime error, it should throw an error or panic instead of crashing.
  • The error should happen when I'm incorrectly building the menu, not later when the user clicks it.

Full tauri info output

[✔] Environment
    - OS: Windows 10.0.22631 x86_64 (X64)
    ✔ WebView2: 129.0.2792.89
    ✔ MSVC:
        - Visual Studio Build Tools 2019
        - Visual Studio Community 2022
    ✔ rustc: 1.79.0 (129f3b996 2024-06-10)
    ✔ cargo: 1.79.0 (ffa9cf99a 2024-06-03)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: 1.79-x86_64-pc-windows-msvc (default)
    - node: 20.10.0
    - pnpm: 8.14.3
    - npm: 10.2.3

[-] Packages
    - tauri 🦀: 2.0.3
    - tauri-build 🦀: 2.0.1
    - wry 🦀: 0.46.0
    - tao 🦀: 0.30.3
    - tauri-cli 🦀: 1.5.14
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli : 2.0.3

[-] Plugins
    - tauri-plugin-shell 🦀: 2.0.1
    - @tauri-apps/plugin-shell : not installed!

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - bundler: Vite

Stack trace

I didn't try getting a stack trace.

Additional context

No response

I think this is because the menu is dropped and not kept around. You will need to save it in the tauri state manager or resources table.

struct TrayMenu<R: tauri::Runtime>(tauri::Submenu<R>);

fn setup(app: &mut tauri::App) -> Result<()> {
    let mut rgba = vec![];
    for _ in 0..32 * 32 {
        rgba.push(255);
        rgba.push(0);
        rgba.push(255);
        rgba.push(255);
    }
    let image = tauri::image::Image::new_owned(rgba, 32, 32);

    let menu = tauri::menu::SubmenuBuilder::new(app, "");
    let menu = menu.item(&tauri::menu::MenuItemBuilder::new("Item").enabled(false).build(app)?);
    let menu = menu.build()?;

    tauri::tray::TrayIconBuilder::new()
        .menu(&menu)
        .icon(image)
        .tooltip("Firezone")
        .build(app)?;
        
    app.manage(TrayMenu(menu));
    Ok(())
}

It works fine if I use a Menu in the same place, though.

Could I propose a PR for this?

yeah sure