fix(core): dispatch menu commands asynchronously to avoid main-thread deadlock - #15898
Open
tachsin wants to merge 1 commit into
Open
fix(core): dispatch menu commands asynchronously to avoid main-thread deadlock#15898tachsin wants to merge 1 commit into
tachsin wants to merge 1 commit into
Conversation
… deadlock AppManager::extend_api holds the PluginStore mutex for the duration of a synchronous command dispatch. Every menu plugin command hops to the main thread via run_main_thread!/run_item_main_thread! and blocks on a channel recv() while still holding that lock. If the main thread needs the same PluginStore lock to process a queued event before it drains the queued main-thread closure, both sides wait on each other forever. Making these commands async releases the lock as soon as the command is spawned, before it reaches the main-thread hop, breaking the cycle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #15888.
AppManager::extend_apiholds theMutex<PluginStore>lock for the entire synchronous dispatch of a command. Everymenuplugin command (aside frompopup,set_as_app_menu,set_as_window_menu, which were alreadyasync) hops to the main thread viarun_main_thread!/run_item_main_thread!and blocks on a channelrecv()while still holding that lock.If a background thread dispatches a menu command while holding the plugin store lock, and the main thread later needs that same lock to process a queued window/run event before it drains the queued main-thread closure, both sides wait on each other forever — the exact lock cycle described in the issue.
Making these commands
asyncmoves execution ontocrate::async_runtime::spawn. This releases the plugin store lock as soon as the command is spawned, before it reaches the main-thread hop, which breaks the deadlock cycle. This is transparent to the JS API sinceinvoke()already returns a promise regardless of whether the Rust command is sync or async.I reproduced the underlying lock-ordering issue with a minimal standalone harness that mirrors the real
PluginStore/run_main_thread!/on_eventcall pattern 1:1 — the blocking-dispatch version reliably hangs, and the async-dispatch version completes cleanly.Test plan
cargo check -p tauri --features wrycargo clippy -p tauri --features wry— no new warningstauri-runtime-cef(not available in this environment)