Skip to content

EventDispatcher.stop() hangs when events are still queued #133

Closed

Description

@hamsolodev

EventDispatcher.stop() never returns if two or more events are still in the queue when it's called. MeshCore.disconnect() awaits it first, so disconnect hangs too.

Reproduction

import asyncio
from meshcore.events import Event, EventDispatcher, EventType

async def main():
    d = EventDispatcher()
    d.subscribe(EventType.OK, lambda e: print("handled", e.payload))
    await d.start()
    await asyncio.sleep(0)  # the loop is now waiting in queue.get()
    await d.dispatch(Event(EventType.OK, {"n": 1}))
    await d.dispatch(Event(EventType.OK, {"n": 2}))
    await d.stop()  # prints "handled {'n': 1}", then waits for ever
    print("stopped")

asyncio.run(main())

Without the sleep(0), a single event dispatched straight after start() is enough. Same on 3.11 and 3.14. (On 3.10 and 3.11, asyncio.wait_for(d.stop(), ...) hides it, because there wait_for runs stop() in a new task and the loop gets to drain the queue first.)

Cause (src/meshcore/events.py at b45758b)

  • stop() sets self.running = False (L268) and then waits on self.queue.join() (L271).
  • _process_events() is while self.running: (L210), so after the event in hand it sees running is false and returns.
  • The events left in the queue are never taken, so task_done() (L245) is never called for them and join() never returns. The cancel() after it (L276) is never reached.

Found while working on #125 (part of #119). I'll send a fix.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions