From 9db62bf94a6577f8142cab42d714aae34d7f3a5e Mon Sep 17 00:00:00 2001 From: "Bernd (Hermes)" Date: Fri, 24 Jul 2026 17:08:05 +0200 Subject: [PATCH] fix(B6): cancel_pending thread-safe via threading.Event --- plugin/archivar/presence_engine.py | 11 ++++++----- tests/test_ddp_await.py | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/plugin/archivar/presence_engine.py b/plugin/archivar/presence_engine.py index d402c82..ee14de4 100644 --- a/plugin/archivar/presence_engine.py +++ b/plugin/archivar/presence_engine.py @@ -654,7 +654,7 @@ class PulseController: self._phase: bool = False # False=online, True=away self._last_sent_phase: Optional[bool] = None self._background_count: int = 0 - self._cancel_pending: bool = False # P0-2: skip next tick flag + self._cancel_pending_event = threading.Event() # B6: thread-safe via Event statt bool # P1-2 O-2: Async-await bridge self._loop: Optional[asyncio.AbstractEventLoop] = None self._await_timeout: float = 2.0 @@ -765,7 +765,7 @@ class PulseController: status fires while the PulseController is running. Sets a flag that the next tick will skip. """ - self._cancel_pending = True + self._cancel_pending_event.set() logger.debug("[pulse] cancel_pending: next tick will be skipped") # ── Internal: Pulse Loop ───────────────────────────────────────────── @@ -814,13 +814,14 @@ class PulseController: self._phase, self._ddp is not None, self._paused.is_set(), - self._cancel_pending, + self._cancel_pending_event.is_set(), self._loop is not None, ) # P0-2: Check cancel_pending flag (set by PresenceEngine._set_presence) - if self._cancel_pending: - self._cancel_pending = False + # B6: thread-safe via threading.Event statt bool + if self._cancel_pending_event.is_set(): + self._cancel_pending_event.clear() logger.debug("[pulse] _pulse_tick skip: cancel_pending was set") return diff --git a/tests/test_ddp_await.py b/tests/test_ddp_await.py index 35f7fdb..fc7f6f7 100644 --- a/tests/test_ddp_await.py +++ b/tests/test_ddp_await.py @@ -413,7 +413,7 @@ class TestPulseControllerPauseInAsyncMode: # Setze cancel_pending pc.cancel_pending() - assert pc._cancel_pending is True + assert pc._cancel_pending_event.is_set() # _pulse_tick sollte übersprungen werden pc._pulse_tick() @@ -421,7 +421,7 @@ class TestPulseControllerPauseInAsyncMode: # set_status sollte NICHT aufgerufen worden sein mock_ddp.set_status.assert_not_called() # cancel_pending sollte zurückgesetzt sein - assert pc._cancel_pending is False + assert not pc._cancel_pending_event.is_set() # ═══════════════════════════════════════════════════════════════════════════