Skip to content

Drain the event queue before stopping the dispatcher - #134

Merged
fdlamotte merged 1 commit into
meshcore-dev:mainfrom
hamsolodev:dispatcher-stop-drain-queue
Nov 22, 2026
Merged

fdlamotte merged 1 commit into
meshcore-dev:mainfrom
hamsolodev:dispatcher-stop-drain-queue

Conversation

@hamsolodev

Copy link
Copy Markdown
Contributor

Cause
stop() sets running = False and then waits on queue.join(). The dispatch loop was while self.running:, so it returned after the event in hand. Any events still queued were never taken, task_done() was never called for them, and join() waited for ever. Details and a reproduction in #133.

Change
The loop is now while True:. It ends when stop() cancels it, which stop() already does once join() returns and the async callbacks have finished. running still guards start() and stop(); MeshCore.stop() cancels the task directly, as before.

Test
tests/unit/test_dispatcher_stop.py: two events queued while the loop waits in queue.get(), and one queued before the loop's first turn. Each checks that stop() returns within a second and that every event was handled. stop() is awaited in the test's own task with a timer to cancel it, not through asyncio.wait_for(): on 3.10 and 3.11 wait_for runs it in a new task, the loop drains the queue first, and the hang doesn't show. Both tests fail on main on 3.11 and 3.14 and pass with this change. The whole suite passes on 3.11 and 3.14, and import meshcore works.

Overlap with #126
#126 changes two other lines in events.py (asyncio.iscoroutinefunction to inspect.iscoroutinefunction) and adds a test to tests/unit/test_events.py. This PR's test is in its own file so the two don't collide. The two branches merge cleanly in either order, and the suite passes on 3.11 and 3.14 with both.

Fixes #133

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.
@hamsolodev
hamsolodev marked this pull request as ready for review November 22, 2026 00:44
@fdlamotte
fdlamotte merged commit ae5ccf3 into meshcore-dev:main Nov 22, 2026
@hamsolodev
hamsolodev deleted the dispatcher-stop-drain-queue branch November 25, 2026 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EventDispatcher.stop() hangs when events are still queued

2 participants