Repository navigation
Drain the event queue before stopping the dispatcher - #134
Merged
fdlamotte merged 1 commit intoNov 22, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cause
stop()setsrunning = Falseand then waits onqueue.join(). The dispatch loop waswhile self.running:, so it returned after the event in hand. Any events still queued were never taken,task_done()was never called for them, andjoin()waited for ever. Details and a reproduction in #133.Change
The loop is now
while True:. It ends whenstop()cancels it, whichstop()already does oncejoin()returns and the async callbacks have finished.runningstill guardsstart()andstop();MeshCore.stop()cancels the task directly, as before.Test
tests/unit/test_dispatcher_stop.py: two events queued while the loop waits inqueue.get(), and one queued before the loop's first turn. Each checks thatstop()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 throughasyncio.wait_for(): on 3.10 and 3.11wait_forruns it in a new task, the loop drains the queue first, and the hang doesn't show. Both tests fail onmainon 3.11 and 3.14 and pass with this change. The whole suite passes on 3.11 and 3.14, andimport meshcoreworks.Overlap with #126
#126 changes two other lines in
events.py(asyncio.iscoroutinefunctiontoinspect.iscoroutinefunction) and adds a test totests/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