Skip to content

Commit 14b531d

Browse files
Clean up Qt socket notifier to avoid spurious interrupt handler calls
Closes #29688 Objects without a parent are not necessarily cleaned up in PyQt when their reference count reaches zero, and must be explicitly cleaned up with `deleteLater()` This prevents the notifier firing after the signal handling was supposed to have been reset to its previous state.
1 parent 9340190 commit 14b531d

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

lib/matplotlib/backends/backend_qt.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ def _allow_interrupt_qt(qapp_or_eventloop):
151151
"""A context manager that allows terminating a plot by sending a SIGINT."""
152152

153153
# Use QSocketNotifier to read the socketpair while the Qt event loop runs.
154+
sn = None
154155

155156
def prepare_notifier(rsock):
157+
nonlocal sn
156158
sn = QtCore.QSocketNotifier(rsock.fileno(), QtCore.QSocketNotifier.Type.Read)
157159

158160
@sn.activated.connect
@@ -172,6 +174,8 @@ def _may_clear_sock():
172174
return sn # Actually keep the notifier alive.
173175

174176
def handle_sigint():
177+
# Ensure notifier does not outlive the event loop shutting down:
178+
sn.deleteLater()
175179
if hasattr(qapp_or_eventloop, 'closeAllWindows'):
176180
qapp_or_eventloop.closeAllWindows()
177181
qapp_or_eventloop.quit()

0 commit comments

Comments
 (0)