Skip to content

[bug] app_handle.tray_handle().set_menu() does not appear to update menu item IDs, leading to a crash when new items are selected #3608

@rscarson

Description

@rscarson

Describe the bug

app_handle.tray_handle().set_menu() does not appear to update menu item IDs, leading to a crash when new items are selected.

Crash occurs at `tauri-1.0.0-rc.3\src\app.rs:1257:33', on unwrap() - since the ID being looked up is not in the ids HashMap

Menu appears updated, but new items to the menu cause a panic when selected

Reproduction

Add a system tray to app:

[....]
let tray = SystemTray::new().with_menu(
    SystemTrayMenu::new()
        .add_item(CustomMenuItem::new("exit".to_string(), "Exit"))
);
let mut app  = tauri::Builder::default()
    .system_tray(tray)
[....]

Update the menu later:

[....]
app.run(move |app_handle, e| match e {
    RunEvent::Ready => {
        let app_handle = app_handle.clone();
        app_handle.tray_handle().set_menu(
            SystemTrayMenu::new()
                .add_item(CustomMenuItem::new("test".to_string(), "Test"))
                .add_item(CustomMenuItem::new("exit".to_string(), "Exit"))
        );
[....]

Then start the application, and select 'Test' from the tray icon's context menu.

Panic will occur at tauri-1.0.0-rc.3\src\app.rs:1257:33

Expected behavior

Updated menu will work as displayed, with new items being selectable when changing the context menu

Platform and versions

Operating System - Windows, version 10.0.19042 X64
Webview2 - 98.0.1108.62
Visual Studio Build Tools:
   - Visual Studio Community 2022
   - Visual Studio Enterprise 2019

Node.js environment
  Node.js - 16.13.0
  @tauri-apps/cli - 1.0.0-rc.5
  @tauri-apps/api - 1.0.0-rc.1

Global packages
  npm - 8.1.0
  pnpm - Not installed
  yarn - Not installed

Rust environment
  rustup - 1.24.3
  rustc - 1.58.1
  cargo - 1.58.0
  toolchain - stable-x86_64-pc-windows-msvc

App directory structure
/.git
/.vscode
/build
/node_modules
/public
/src
/src-tauri

App
  tauri - 1.0.0-rc.3
  tauri-build - 1.0.0-rc.3
  tao - 0.6.2
  wry - 0.13.2
  build-type - bundle
  CSP - unset
  distDir - ../build
  devPath - http://localhost:3000/
  framework - React

Stack trace

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tauri-1.0.0-rc.3\src\app.rs:1257:33
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\panicking.rs:498
   1: core::panicking::panic_fmt
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\core\src\panicking.rs:107
   2: core::panicking::panic
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\core\src\panicking.rs:48
   3: enum$<core::option::Option<ref$<alloc::string::String> >, 1, 18446744073709551615, Some>::unwrap<ref$<alloc::string::String> >
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\option.rs:746
   4: tauri::app::impl$15::build::closure$0<tauri_runtime_wry::Wry,tauri_utils::assets::EmbeddedAssets>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tauri-1.0.0-rc.3\src\app.rs:1257
   5: alloc::boxed::impl$46::call<tuple$<ref$<enum$<tauri_runtime::SystemTrayEvent> > >,dyn$<core::ops::function::Fn<tuple$<ref$<enum$<tauri_runtime::SystemTrayEvent> > >,assoc$<Output,tuple$<> > >,core::marker::Send>,alloc::alloc::Global>
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\alloc\src\boxed.rs:1708
   6: tauri_runtime_wry::handle_event_loop
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tauri-runtime-wry-0.3.2\src\lib.rs:2343
   7: tauri_runtime_wry::impl$45::run::closure$0<tauri::app::impl$14::run::closure$0>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tauri-runtime-wry-0.3.2\src\lib.rs:1894
   8: tao::platform_impl::platform::event_loop::impl$2::run_return::closure$0<enum$<tauri_runtime_wry::Message>,tauri_runtime_wry::impl$45::run::closure$0>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\event_loop.rs:225
   9: alloc::boxed::impl$45::call_mut<tuple$<enum$<tao::event::Event<enum$<tauri_runtime_wry::Message> > >,ref_mut$<enum$<tao::event_loop::ControlFlow> > >,dyn$<core::ops::function::FnMut<tuple$<enum$<tao::event::Event<enum$<tauri_runtime_wry::Message> > >,ref_
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\alloc\src\boxed.rs:1701
  10: tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0<enum$<tauri_runtime_wry::Message> >
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\event_loop\runner.rs:249
  11: core::panic::unwind_safe::impl$23::call_once<tuple$<>,tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\panic\unwind_safe.rs:271
  12: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>,tuple$<> >
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\std\src\panicking.rs:406
  13: tauri_runtime_wry::impl$24::from<f64>
  14: std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0> >
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\std\src\panicking.rs:370
  15: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>,tuple$<> >
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\std\src\panic.rs:133
  16: tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum$<tauri_runtime_wry::Message> >::catch_unwind<enum$<tauri_runtime_wry::Message>,tuple$<>,tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\event_loop\runner.rs:155
  17: tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum$<tauri_runtime_wry::Message> >::call_event_handler<enum$<tauri_runtime_wry::Message> >
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\event_loop\runner.rs:241
  18: tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum$<tauri_runtime_wry::Message> >::send_event<enum$<tauri_runtime_wry::Message> >
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\event_loop\runner.rs:223
  19: tao::platform_impl::platform::system_tray::impl$0::build::closure$2<enum$<tauri_runtime_wry::Message> >
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\system_tray.rs:127
  20: alloc::boxed::impl$46::call<tuple$<enum$<tao::event::Event<tuple$<> > > >,dyn$<core::ops::function::Fn<tuple$<enum$<tao::event::Event<tuple$<> > > >,assoc$<Output,tuple$<> > > >,alloc::alloc::Global>
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\alloc\src\boxed.rs:1708
  21: tao::platform_impl::platform::menu::MenuHandler::send_menu_event
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\menu.rs:65
  22: tao::platform_impl::platform::menu::subclass_proc
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\menu.rs:352
  23: DefSubclassProc
  24: DefSubclassProc
  25: CallWindowProcW
  26: DispatchMessageW
  27: windows::Windows::Win32::UI::WindowsAndMessaging::DispatchMessageW
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\windows-0.30.0\src\Windows\Win32\UI\WindowsAndMessaging\mod.rs:2637
  28: tao::platform_impl::platform::event_loop::EventLoop<enum$<tauri_runtime_wry::Message> >::run_return<enum$<tauri_runtime_wry::Message>,tauri_runtime_wry::impl$45::run::closure$0>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\event_loop.rs:255
  29: tao::platform_impl::platform::event_loop::EventLoop<enum$<tauri_runtime_wry::Message> >::run<enum$<tauri_runtime_wry::Message>,tauri_runtime_wry::impl$45::run::closure$0>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\platform_impl\windows\event_loop.rs:209
  30: tao::event_loop::EventLoop<enum$<tauri_runtime_wry::Message> >::run<enum$<tauri_runtime_wry::Message>,tauri_runtime_wry::impl$45::run::closure$0>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.6.2\src\event_loop.rs:177
  31: tauri_runtime_wry::impl$45::run<tauri::app::impl$14::run::closure$0>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tauri-runtime-wry-0.3.2\src\lib.rs:1893
  32: tauri::app::App<tauri_runtime_wry::Wry>::run<tauri_runtime_wry::Wry,app::main::closure$0>
             at C:\Users\redacted\.cargo\registry\src\github.com-1ecc6299db9ec823\tauri-1.0.0-rc.3\src\app.rs:486
  33: app::main
             at .\src\main.rs:65
  34: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
[0303/143840.008:ERROR:window_impl.cc(114)] Failed to unregister class Chrome_WidgetWin_0. Error = 0
error: process didn't exit successfully: `target\debug\app.exe` (exit code: 101)

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions