G5: F07 — await in-flight async callbacks before stop() returns

Why: EventDispatcher._process_events() calls task_done() on the queue
immediately after spawning async callback tasks. await queue.join() in
stop() therefore returns as soon as all items are marked done, even if
their async callbacks are still executing. Any caller that does
"await dispatcher.stop(); cleanup()" could race with still-running
callbacks. Fix: after queue.join(), gather all tracked background tasks
before cancelling the dispatch loop.

Refs: Forensics report finding F07
This commit is contained in:
Matthew Wolter 2026-04-12 03:56:28 -07:00
parent 26141d0353
commit d4581a8e13

View file

@ -236,6 +236,10 @@ class EventDispatcher:
self.running = False
if self._task:
await self.queue.join()
# Wait for any in-flight async callbacks to complete before
# tearing down (F07: task_done fires before callbacks finish).
if self._background_tasks:
await asyncio.gather(*self._background_tasks, return_exceptions=True)
self._task.cancel()
try:
await self._task