Repository navigation
hotkeys: don't query lua viewscreen selections on the render thread - #5908
Merged
Merged
Conversation
Hotkey guards run on DF's render thread when validating a keypress, and getAny*() would call getSelected*() on the topmost viewscreen. For a lua-backed screen that virtual enters Lua through CoreSuspender, which can never be acquired on the render thread, so pressing a hotkey while a DFHack window was open asserted or deadlocked. Skip the lua virtual on the render thread instead: a focused lua screen owns the selection so report none, while a defocused one is transparent and the native checks below still apply. Guards that don't inspect the selection, and non-hotkey callers on other threads, are unaffected. Fixes DFHack#5835
ab9rf
reviewed
Sep 15, 2026
| // still apply. | ||
| template<typename T> | ||
| static std::optional<T *> dfscreen_selection(df::viewscreen *top, | ||
| T * (dfhack_viewscreen::*method)()) |
Member
There was a problem hiding this comment.
Is there a reason why this template is restricted to returning a pointer?
Contributor
Author
There was a problem hiding this comment.
@ab9rf No good reason. It only needed the pointer return so the focused case could yield nullptr as the no selection answer, but deducing R directly does that just as well. Generalized it.
ab9rf
approved these changes
Sep 15, 2026
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 #5835.
When a keypress matches a bound hotkey,
Plugin::can_invoke_hotkeyruns the command's guard on DF's render thread before the command is queued. Guards that inspect the UI selection (Gui::any_item_hotkey,workshop_job_hotkey, etc.) callgetSelected*()on the topmost viewscreen, and when that screen is adfhack_lua_viewscreenthe virtual enters Lua throughCoreSuspender, which can never be acquired on the render thread. The result was an assertion failure in debug builds and a hang in production, e.g. withgui/revealopen but unfocused.This change routes the selection query through a helper that checks the calling thread first:
getSelected*()virtual is called as before.Also adds
Core::isRenderThread()so the check has a clear name.Guards that never inspect the selection (
anywhere_hotkey,default_hotkey, focus-based guards) are unaffected, as isGui::getFocusStrings/matchFocusString(isFocused/getFocusStringare pure C++ on lua screens).Testing
gui/reveal, defocused it (focus resolves todwarfmode/...through the screen), and pressedCtrl-K(autodump-destroy-item). No assert, no hang; the key fell through to the screen as expected.@dwarfmodestill dispatched through the defocused lua screen.CoreSuspenderBaseassertion (or hung, waiting onCoreSuspendMutexfrom the render thread).