Skip to content

fix(menu): return error instead of panicking on unexpected ItemKind#15691

Open
krishpranav wants to merge 1 commit into
tauri-apps:devfrom
krishpranav:fix/menu-plugin-panic-15686
Open

fix(menu): return error instead of panicking on unexpected ItemKind#15691
krishpranav wants to merge 1 commit into
tauri-apps:devfrom
krishpranav:fix/menu-plugin-panic-15686

Conversation

@krishpranav

@krishpranav krishpranav commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

fix(menu): return error instead of panicking on unexpected ItemKind

Problem

Menu plugin commands take kind straight from the IPC payload and dispatch on it through the do_menu_item! macro, whose accepted kind set never includes the root Menu variant (and some call sites narrow it further). Anything outside that set fell through to _ => unreachable!() and took the whole process down.

Reachable from plain JS — the TS types don't stop it, and raw IPC ignores them:

await Menu.new({ items: [await Menu.new()] })

Fix

  • Changed the do_menu_item! fallback arm from unreachable!() to a recoverable error (anyhow::anyhow!("unexpected menu item kind").into()), matching the hand-written _ => return Err(...) arms already in menu/plugin.rs. One macro change covers all nine call sites.
  • Added a regression test that drives the affected commands with mismatched kinds and asserts they return Err instead of panicking.
  • Added .changes/fix-menu-plugin-panic.md with valid covector front matter ("tauri": patch:bug, "tauri-macros": patch:bug).

Testing

cargo fmt --check
cargo clippy -p tauri -p tauri-macros --all-features -- -D warnings
cargo test -p tauri --lib menu::plugin::tests

The regression test panics on dev and passes with this change.

Before (fallback arm still unreachable!()):

test menu::plugin::tests::unexpected_item_kind_errors_instead_of_panicking ... FAILED

thread '...' panicked at crates/tauri/src/menu/plugin.rs:751:3:
internal error: entered unreachable code

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 57 filtered out

After (this PR):

test menu::plugin::tests::unexpected_item_kind_errors_instead_of_panicking ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 57 filtered out; finished in 0.01s

Visual Verification

App survives the repro (examples/api)

Ran the exact repro from the issue (Menu.new({ items: [await Menu.new()] })). On dev the process aborts; with this change the call rejects with unexpected menu item kind and the window stays alive.

demo

Fixes #15686

@krishpranav
krishpranav requested a review from a team as a code owner July 9, 2026 16:42
Comment thread crates/tauri-macros/src/menu.rs Outdated
// `kind` comes straight from the (untrusted) IPC payload, so a kind
// outside the accepted set is reachable and must be a recoverable error
// rather than a panic. See tauri-apps/tauri#15686.
_ => Err(anyhow::anyhow!("unexpected menu item kind").into()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, we'll need to either add a new macro or add an optional parameter to this macro

@Legend-Master Legend-Master added the ai-slop Low effort content, see https://github.com/tauri-apps/tauri?tab=contributing-ov-file#ai-tool-policy label Jul 10, 2026
…auri-apps#15686)

Menu plugin command handlers receive `kind` straight from the untrusted
IPC payload and dispatch through the `do_menu_item!` macro. The macro's
`defaults` kind set omits the root `Menu` variant, and several call sites
further restrict the accepted kinds, so a `kind` outside the accepted set
was reachable and hit `_ => unreachable!()`, aborting the whole process
(e.g. `Menu.new({ items: [await Menu.new()] })` or a raw IPC call).

Make the macro's fallback arm return a recoverable error
(`anyhow::anyhow!("unexpected menu item kind").into()`), matching the
manual `_ => return Err(...)` pattern already used elsewhere in the menu
plugin. This fixes all nine `do_menu_item!` call sites at once.

Add a regression test that drives the affected commands with mismatched
kinds and asserts they return `Err` instead of panicking.
@krishpranav
krishpranav force-pushed the fix/menu-plugin-panic-15686 branch from f2c9c66 to 8e7a5a9 Compare July 10, 2026 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-slop Low effort content, see https://github.com/tauri-apps/tauri?tab=contributing-ov-file#ai-tool-policy

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] menu plugin can panic

2 participants