From 03af3be6423821f27b916d4c15edf75e862c9081 Mon Sep 17 00:00:00 2001 From: mch <32224+hamsolodev@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:40:00 +1100 Subject: [PATCH] Drain the event queue before stopping the dispatcher EventDispatcher.stop() sets running to False and then waits on queue.join(). The dispatch loop checked running after each event, so it stopped taking events after the one in hand: with two or more events still queued, or one queued before the loop's first turn, join() waited for ever and so did MeshCore.disconnect(). The loop now runs until stop() cancels it, which stop() already does once the queue has drained. --- src/meshcore/events.py | 3 +- tests/unit/test_dispatcher_stop.py | 55 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_dispatcher_stop.py diff --git a/src/meshcore/events.py b/src/meshcore/events.py index 937d3e1..d24ea31 100644 --- a/src/meshcore/events.py +++ b/src/meshcore/events.py @@ -207,7 +207,8 @@ async def dispatch(self, event: Event): await self.queue.put(event) async def _process_events(self): - while self.running: + # Runs until stop() cancels it, once the queue has drained. + while True: event = await self.queue.get() logger.debug( f"Dispatching event: {event.type}, {event.payload}, {event.attributes}" diff --git a/tests/unit/test_dispatcher_stop.py b/tests/unit/test_dispatcher_stop.py new file mode 100644 index 0000000..0cb79c5 --- /dev/null +++ b/tests/unit/test_dispatcher_stop.py @@ -0,0 +1,55 @@ +import asyncio + +import pytest + +from meshcore.events import Event, EventDispatcher, EventType + +pytestmark = pytest.mark.asyncio + + +async def _stop_within(dispatcher, timeout=1.0): + # stop() is awaited in this task, as MeshCore.disconnect() does, with a + # timer to cancel it. asyncio.wait_for() would run it in a new task on + # Python 3.10 and 3.11, letting the loop drain the queue before stop() + # starts, and the hang would go unseen there. + timer = asyncio.get_running_loop().call_later( + timeout, asyncio.current_task().cancel + ) + try: + await dispatcher.stop() + except asyncio.CancelledError: + pytest.fail("EventDispatcher.stop() did not return") + finally: + timer.cancel() + + +async def test_stop_with_events_queued(): + """stop() drains the queue: two events queued while the loop waits in + queue.get() are both handled, and stop() returns.""" + dispatcher = EventDispatcher() + seen = [] + dispatcher.subscribe(EventType.OK, lambda event: seen.append(event.payload["n"])) + await dispatcher.start() + await asyncio.sleep(0) # the loop now waits in queue.get() + + await dispatcher.dispatch(Event(EventType.OK, {"n": 1})) + await dispatcher.dispatch(Event(EventType.OK, {"n": 2})) + await _stop_within(dispatcher) + + assert seen == [1, 2] + assert dispatcher._task is None + + +async def test_stop_before_the_loop_has_run(): + """An event dispatched straight after start(), before the loop's first + turn, is handled too.""" + dispatcher = EventDispatcher() + seen = [] + dispatcher.subscribe(EventType.OK, lambda event: seen.append(event.payload["n"])) + await dispatcher.start() + + await dispatcher.dispatch(Event(EventType.OK, {"n": 1})) + await _stop_within(dispatcher) + + assert seen == [1] + assert dispatcher._task is None