Fix deadlock for plugins that create widgets #7845
Open
+25
−7
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.
Problem
Originally reported by @emilk in #7652 (comment)
When a plugin creates a widget (e.g. during on_pass_end) and that widget gets hovered, egui will call
on_widget_under_cursor, causing a deadlock because the plugin is still locked on the stack.Fix
Added
Mutex::try_lockwhich returnsNoneimmediately if the mutex can't be acquired.This is only used for
on_widget_under_cursor, which will now get skipped if its mutex is locked.Since plugins can technically be locked from other threads (via
TypedPluginHandle), thetry_lockfunction is not used for the other plugin hooks so that they execute reliably (as they used to before this change). Whereason_widget_under_cursorwill be skipped if it happens that the plugin is locked externally. Small caveat but probably not an issue?For example, a plugin does some background work and uses
TypedPluginHandleto set the result back in the plugin state object. To set the result, it locks the plugin. If it happens that egui tries to runon_widget_under_cursor, it will be skipped.