From b0a24a4458beca81e4a97a9cf505ca9d42251e8a Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 21 Feb 2022 22:12:53 -0500 Subject: [PATCH] motion_report: Ensure startstop_cb is called atomically Make sure APIDumpHelper() does not invoke the startstop callback while it is already running. Signed-off-by: Kevin O'Connor --- klippy/extras/motion_report.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/klippy/extras/motion_report.py b/klippy/extras/motion_report.py index 34fc455d..d32de43f 100644 --- a/klippy/extras/motion_report.py +++ b/klippy/extras/motion_report.py @@ -17,29 +17,37 @@ class APIDumpHelper: if startstop_cb is None: startstop_cb = (lambda is_start: None) self.startstop_cb = startstop_cb + self.is_started = False self.update_interval = update_interval self.update_timer = None self.clients = {} def _stop(self): self.clients.clear() - if self.update_timer is None: - return reactor = self.printer.get_reactor() reactor.unregister_timer(self.update_timer) self.update_timer = None + if not self.is_started: + return reactor.NEVER try: self.startstop_cb(False) except self.printer.command_error as e: logging.exception("API Dump Helper stop callback error") + self.clients.clear() + self.is_started = False + if self.clients: + # New client started while in process of stopping + self._start() return reactor.NEVER def _start(self): - if self.update_timer is not None: + if self.is_started: return + self.is_started = True try: self.startstop_cb(True) except self.printer.command_error as e: logging.exception("API Dump Helper start callback error") - self._stop() + self.is_started = False + self.clients.clear() raise reactor = self.printer.get_reactor() systime = reactor.monotonic()