fix(plots): stop signal proxies after cleanup

This commit is contained in:
wyzula_j authored and Jan Wyzula committed 2026-10-07 18:36:26 +02:00
1 parent 54f6320991
commit 929657538a
8 files changed
+178 -1

No files matched your search

+21 -1
View File
@@ -32,6 +32,7 @@ class BECSignalProxy(SignalProxy):
super().__init__(*args, rateLimit=rateLimit, **kwargs)
self._blocking = False
self._pending = False
self._cleaned_up = False
self.old_args = None
self.new_args = None
@@ -55,6 +56,8 @@ class BECSignalProxy(SignalProxy):
def signalReceived(self, *args):
"""Receive signal, store the args and call signalReceived from the parent class if not blocked"""
if self._cleaned_up:
return
self.new_args = args
if self.blocked is True:
self._pending = True
@@ -70,6 +73,8 @@ class BECSignalProxy(SignalProxy):
"""
Unblock the proxy and replay emissions that arrived while it was blocked.
"""
if self._cleaned_up:
return
if self.blocked:
self._timer.stop()
self.blocked = False
@@ -89,8 +94,23 @@ class BECSignalProxy(SignalProxy):
def cleanup(self):
"""
Cleanup the proxy by stopping the timer and disconnecting the timeout signal.
Cleanup the proxy so that nothing is delivered to the slot afterwards.
Disconnects the proxy from its source signal and blocks it (pg.SignalProxy.disconnect),
stops the delivery timer inherited from pg.SignalProxy, drops any queued or pending
emission and stops and deletes the auto-unblock timer. Calling it again is a no-op.
"""
if self._cleaned_up:
return
self._cleaned_up = True
# Called explicitly: on subclasses of SignalProxy, PySide6 resolves self.disconnect
# to the built-in QObject.disconnect instead of SignalProxy.disconnect.
SignalProxy.disconnect(self)
self.timer.stop()
self.args = None
self._pending = False
self.old_args = None
self.new_args = None
self._timer.stop()
self._timer.timeout.disconnect(self._timeout_unblock)
self._timer.deleteLater()
@@ -1632,6 +1632,8 @@ class Heatmap(ImageBase):
self.main_image.transpose = enable
def cleanup(self):
# Detach the update proxy first, so no queued update restarts the interpolation thread.
self.proxy_update_sync.disconnect()
self._finish_interpolation_thread()
if self.scan_history_dialog is not None:
self.scan_history_dialog.reject()
@@ -747,6 +747,8 @@ class ScatterWaveform(PlotBase):
"""
Cleanup the widget and disconnect all signals.
"""
# Detach the update proxy first, so no queued update reaches the torn-down curve.
self.proxy_update_sync.disconnect()
if self.scatter_dialog is not None:
self.scatter_dialog.close()
self.scatter_dialog.deleteLater()
@@ -2719,6 +2719,10 @@ class Waveform(PlotBase):
"""
Cleanup the widget by disconnecting signals and closing dialogs.
"""
# Detach all update proxies first, so no queued update is delivered into the widget
# while (or after) it is torn down. disconnect() also blocks pending emissions.
self.proxy_update_sync.disconnect()
self.proxy_update_async.disconnect()
self.proxy_dap_request.cleanup()
if self._alignment_controller is not None:
self._alignment_controller.cleanup()
+21
View File
@@ -1172,3 +1172,24 @@ def test_heatmap_settings_scan_index_syncs_with_widget(heatmap_widget, qtbot, sc
heatmap_widget.heatmap_dialog.reject()
qtbot.waitUntil(lambda: heatmap_widget.heatmap_dialog is None)
def test_heatmap_cleanup_detaches_update_proxy(qtbot, mocked_client):
"""
A plot update still queued in proxy_update_sync when the Heatmap is cleaned up, or
requested afterwards, must not reach update_plot, which could otherwise restart the
interpolation thread that cleanup() has just stopped.
"""
with mock.patch.object(Heatmap, "update_plot") as update_plot:
widget = Heatmap(client=mocked_client)
qtbot.addWidget(widget)
qtbot.waitExposed(widget)
qtbot.wait(100) # let anything queued during construction drain
update_plot.reset_mock()
widget.sync_signal_update.emit() # queued in the proxy
widget.close() # closeEvent -> cleanup()
widget.sync_signal_update.emit() # requested after cleanup
qtbot.wait(300)
update_plot.assert_not_called()
+19
View File
@@ -663,3 +663,22 @@ def test_z_gradient_flat_and_empty_z(qtbot, mocked_client):
assert curve._make_z_gradient(float("nan"), "plasma") is None
mixed = curve._make_z_gradient([float("nan"), 1.0, 2.0], "plasma")
assert mixed is not None and len(mixed) == 3
def test_scatter_waveform_cleanup_detaches_update_proxy(qtbot, mocked_client):
"""
A curve update still queued in proxy_update_sync when the ScatterWaveform is cleaned
up, or requested afterwards, must not reach update_sync_curves after cleanup() has
removed the main curve.
"""
with patch.object(ScatterWaveform, "update_sync_curves") as update_sync_curves:
swf = create_widget(qtbot, ScatterWaveform, client=mocked_client)
qtbot.wait(100) # let anything queued during construction drain
update_sync_curves.reset_mock()
swf.sync_signal_update.emit() # queued in the proxy
swf.close() # closeEvent -> cleanup()
swf.sync_signal_update.emit() # requested after cleanup
qtbot.wait(200)
update_sync_curves.assert_not_called()
@@ -179,3 +179,45 @@ def test_bec_signal_proxy_still_drops_identical_args_on_unblock(qtbot):
assert len(calls) == 1 # unchanged args are not replayed
assert proxy.blocked is False
proxy.cleanup()
def test_bec_signal_proxy_cleanup_drops_queued_emission(qtbot):
"""An emission that is still queued in the delivery timer inherited from
pg.SignalProxy when cleanup() runs must never reach the slot."""
from qtpy.QtCore import QObject, Signal
class _Src(QObject):
sig = Signal()
src = _Src()
calls = []
proxy = BECSignalProxy(src.sig, rateLimit=25, slot=lambda *_: calls.append(1), timeout=10.0)
src.sig.emit() # queued: the inherited delivery timer forwards it on the next tick
assert calls == []
proxy.cleanup()
qtbot.wait(150)
assert calls == []
def test_bec_signal_proxy_ignores_source_after_cleanup(qtbot):
"""After cleanup() the proxy is detached from its source signal: later emissions are
neither forwarded to the slot nor touch the deleted auto-unblock watchdog."""
from qtpy.QtCore import QObject, Signal
class _Src(QObject):
sig = Signal()
src = _Src()
calls = []
proxy = BECSignalProxy(src.sig, rateLimit=25, slot=lambda *_: calls.append(1), timeout=10.0)
proxy.cleanup()
qtbot.wait(20) # let the watchdog's deleteLater run
src.sig.emit()
proxy.unblock_proxy()
qtbot.wait(150)
assert calls == []
assert proxy.blocked is False
proxy.cleanup() # a second cleanup is a no-op
+67
View File
@@ -611,6 +611,73 @@ def test_request_dap_releases_proxy_when_nothing_published(
qtbot.waitUntil(lambda: wf.proxy_dap_request.blocked is False, timeout=3000)
@pytest.fixture
def waveform_with_proxy_slot_spies(qtbot, mocked_client):
"""
Waveform whose three proxy-driven slots are replaced by spies before construction, so
the signal proxies connect to the spies. Yields the widget and a dict of the spies.
"""
with (
mock.patch.object(Waveform, "update_sync_curves") as sync_slot,
mock.patch.object(Waveform, "update_async_curves") as async_slot,
mock.patch.object(Waveform, "request_dap") as dap_slot,
):
wf = create_widget(qtbot, Waveform, client=mocked_client)
qtbot.wait(100) # let anything queued during construction drain
slots = {"sync": sync_slot, "async": async_slot, "dap": dap_slot}
for slot in slots.values():
slot.reset_mock()
assert wf.proxy_dap_request.blocked is False
yield wf, slots
def test_waveform_cleanup_drops_queued_proxy_updates(qtbot, waveform_with_proxy_slot_spies):
"""
Updates still queued in the sync, async and DAP signal proxies when the Waveform is
cleaned up must not be delivered into the torn-down widget afterwards.
"""
wf, slots = waveform_with_proxy_slot_spies
wf.sync_signal_update.emit()
wf.async_signal_update.emit()
wf.request_dap_update.emit()
assert all(slot.call_count == 0 for slot in slots.values()) # all three are queued
wf.close() # closeEvent -> cleanup() while the updates are pending
qtbot.wait(200)
assert {name: slot.call_count for name, slot in slots.items()} == {
"sync": 0,
"async": 0,
"dap": 0,
}
def test_waveform_cleanup_detaches_proxies_from_update_signals(
qtbot, waveform_with_proxy_slot_spies
):
"""
After cleanup() the update signals must no longer reach the widget through the signal
proxies.
"""
wf, slots = waveform_with_proxy_slot_spies
wf.close() # closeEvent -> cleanup()
qtbot.wait(50)
wf.sync_signal_update.emit()
wf.async_signal_update.emit()
wf.request_dap_update.emit()
wf.unblock_dap_proxy.emit()
qtbot.wait(200)
assert {name: slot.call_count for name, slot in slots.items()} == {
"sync": 0,
"async": 0,
"dap": 0,
}
def test_request_dap_always_resubmits_device_parent(qtbot, mocked_client_with_dap, monkeypatch):
"""
DAP curves attached to device curves keep the resubmit-on-every-update behavior,