#16129·tauri

TrayIconBuilder::on_menu_event is global, not tray-scoped: registering on both the app builder and the tray invokes the handler twice for every menu event

Author: iksnaeCreated Sep 23, 2026Updated Sep 23, 2026

Summary

TrayIconBuilder::on_menu_event is global, not tray-scoped. An app that registers a handler on both the app Builder and the tray builder gets that handler invoked twice for every menu event in the application — including events from the app menu that have nothing to do with the tray.

The naming and the receiver both imply tray scope, and a correctly tray-scoped vector exists three lines away in the same struct literal, so the incorrect reading is the one a careful reader arrives at.

Version

tauri 2.11.3, macOS. Read from the vendored source in the cargo registry.

The mechanism

Both registrations land in the same Vec:

  • src/tray/mod.rs:427-434 — TrayIcon::register pushes its on_menu_event handler into app_handle.manager.menu.global_event_listeners.
  • src/app.rs:2004 — Builder::on_menu_event pushes into menu_event_listeners, which becomes that same vector via src/app.rs:2261 → src/manager/mod.rs:311.
  • src/app.rs:2588-2596 — on EventLoopMessage::MenuEvent, every listener in that vector is invoked, for every menu event.

Why the wrong reading is the reasonable one

src/manager/mod.rs:301-313 constructs TrayManager and MenuManager adjacently, and both carry a field named global_event_listeners (plus one named event_listeners):

rust
tray: tray::TrayManager {
    icon: context.tray_icon,
    icons: Default::default(),
    global_event_listeners: Mutex::new(tray_icon_event_listeners),   // tray ICON events
    event_listeners: Default::default(),
},
menu: menu::MenuManager {
    menus: Default::default(),
    menu: Default::default(),
    global_event_listeners: Mutex::new(menu_event_listener),          // <-- tray MENU handler goes here
    event_listeners: Mutex::new(window_menu_event_listeners),
},

A reader checking whether a handler registered on a tray builder is tray-scoped finds a tray-specific global_event_listeners immediately above, and reasonably concludes it is. Nothing at the call site signals otherwise — the method name, the receiver type, and the adjacent correctly-scoped vector all point the wrong way.

Reproduction

rust
fn main() {
    tauri::Builder::default()
        .setup(|app| {
            TrayIconBuilder::with_id("tray")
                .menu(&menu)
                .on_menu_event(handle_event)   // global
                .build(app)?;
            Ok(())
        })
        .on_menu_event(handle_event)           // also global — same vector
        .run(ctx)
}

handle_event runs twice for every menu event, from the app menu and from the tray alike.

How this presented in a real app

It was invisible for a release, because the verbs that doubled were idempotent — window-opening actions that focus-or-create a fixed label, so a doubled dispatch appeared to produce one window. The codebase had reasoned about duplicate delivery, concluded it was harmless, and was correct for every case that existed at the time.

Two things were then found:

  1. The first non-idempotent verb to route through it drew two stacked native dialogs, one behind the other. The user dismissed one and another appeared — which is how it was finally noticed.
  2. The "idempotent" verbs were not idempotent either. Focus-or-create was implemented as a check-then-act across an await, so two dispatches inside that window both saw "no window" and both created one. Measured on a debug build: 2 duplicate windows in 7 single clicks. That is a bug in the consuming app, not in Tauri — but the duplicate dispatch is what made it reachable, and the doubling is what turned a latent race into an observed one.

Suggested resolutions — either would have prevented this

  1. Scope it. Make TrayIconBuilder::on_menu_event deliver only events originating from that tray's menu. This matches what the name and receiver imply. It is a behaviour change and would need a release note, since apps currently relying on the global behaviour would stop receiving app-menu events there.
  2. Document it. If global delivery is intentional, say so on TrayIconBuilder::on_menu_event explicitly — that it is equivalent to Builder::on_menu_event and that registering the same handler on both invokes it twice. A single sentence in the rustdoc would close this.

Resolution 2 alone would be sufficient. I have no view on which is right for the project and would defer to the maintainers.

Provenance

Found in a desktop app from the user-visible symptom (two stacked dialogs), traced to the vendored source, and then independently verified from source by a second team before this report was written, including the two-adjacent-vectors detail above. The line numbers are from tauri-2.11.3 as published on crates.io.