Fix selectors when the onselect callback raises by twelfthlabor · Pull Request #32349 · matplotlib/matplotlib · GitHub
Skip to content

Fix selectors when the onselect callback raises - #32349

Open
twelfthlabor wants to merge 1 commit into
matplotlib:mainfrom
twelfthlabor:fix/spanselector-callback-exception-state
Open

twelfthlabor wants to merge 1 commit into
matplotlib:mainfrom
twelfthlabor:fix/spanselector-callback-exception-state

Conversation

@twelfthlabor

@twelfthlabor twelfthlabor commented Sep 13, 2026

Copy link
Copy Markdown

_SelectorWidget.release cleared _eventpress and _eventrelease only after self._release(event) returned, but _release is what calls the user's onselect callback. If that callback raised, the cleanup was skipped, and the stale _eventpress made ignore() stop rejecting events outside the selector's Axes. The failed selector then kept reacting to later interactions, including events meant for another selector.

Move the cleanup into a finally block so the exception still propagates while the selector stays usable afterwards. This covers all _SelectorWidget subclasses, which all call onselect from _release.

Fixes #32327.

AI Disclosure

I used an AI coding assistant to help implement this change and write the tests. I reviewed the diff and verified the behavior locally.

@github-actions

Copy link
Copy Markdown

`_SelectorWidget.release` cleared `_eventpress` and `_eventrelease` only
after `self._release(event)` returned, but `_release` is what calls the
user's `onselect` callback.  If that callback raised, the cleanup was
skipped, and the stale `_eventpress` made `ignore` stop rejecting events
outside the selector's Axes.  The failed selector then kept reacting to
later interactions, including events meant for another selector.

Move the cleanup into a `finally` block so the exception still propagates
while the selector stays usable afterwards.  This covers all
`_SelectorWidget` subclasses, which all call `onselect` from `_release`.

Fixes matplotlib#32327.
@twelfthlabor
twelfthlabor force-pushed the fix/spanselector-callback-exception-state branch from 8d69460 to 9b9dacf Compare September 13, 2026 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: exception raised in SpanSelector callback puts the selector in an invalid state for later interactions

1 participant