You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exception and hang in Python 3.14 when using QApplication.aboutToQuit per examples #176
In Python 3.14, using QApplication.aboutToQuit as described in the examples is resulting in an unhandled exception, after which Python hangs and the process must be killed. (I assume the "hang" is actually the event loop continuing to run without any way to interact with it?)
I can reproduce this by running examples/aiohttp_fetch.py (or even the example in the README). From a fresh checkout of qasync:
i also ran into the same problem with Python 3.14.3 on Linux in an app using aboutToQuit, i guess it might have something to do with the _set_running_loop timing?
Sorry for posting AI output (GLM5) directly, but this fix seems to work and the explanation seems reasonable to me.
diff --git a/src/qasync/__init__.py b/src/qasync/__init__.py
index d35a5ee..b3554da 100644
--- a/src/qasync/__init__.py+++ b/src/qasync/__init__.py@@ -430,9 +430,15 @@ class _QEventLoop:
self.run_forever()
finally:
future.remove_done_callback(stop)
- self.__app.eventDispatcher().processEvents(- AllEvents- ) # run loop one last time to process all the events+ # Process remaining Qt events. We must temporarily restore the+ # running loop because timer callbacks invoked by processEvents+ # (e.g. task wakeups) require it to be set (Python 3.14+ enforces+ # this in the C _asyncio module's enter_task check).+ asyncio.events._set_running_loop(self)+ try:+ self.__app.eventDispatcher().processEvents(AllEvents)+ finally:+ asyncio.events._set_running_loop(None)
if not future.done():
raise RuntimeError("Event loop stopped before Future completed.")
Root Cause
In Python 3.14, the C extension _asyncio module added a strict check in enter_task() that verifies the thread-local asyncio_running_loop matches the task's loop before stepping into a task. This is called whenever a task wakes up (e.g., via task_wakeup → task_step → enter_task).
The bug was in _QEventLoop.run_until_complete(). After run_forever() returns, its finally block calls asyncio.events._set_running_loop(None), clearing the running loop reference. Then run_until_complete immediately calls self.__app.eventDispatcher().processEvents(AllEvents) to process any remaining Qt events. Some of those pending events are qasync timer callbacks that invoke asyncio handles — including task wakeups. When those callbacks run with the running loop set to None, Python 3.14's enter_task check fails with:
RuntimeError: loop <QSelectorEventLoop running=False closed=False> is not the running loop
This exception prevents tasks from completing properly during cleanup, causing asyncio.Runner.close() to hang when it tries loop.run_until_complete(loop.shutdown_asyncgens()) because the shutdown can never finish.
Good sleuthing! That explanation sure sounds plausible, though I'm not convinced that patch is the right fix; the add-remove-add-remove between run_forever and run_until_complete seems suspicious. I wonder if the error might be the result of a design issue? It seems odd to me that run_until_complete calls run_forever (which logs that the "Qt event loop ended" before returning) and then kicks off another round of event processing "after forever".
Of course, it also seems odd to me that there would be unprocessed events after the event loop ends. Based on when the error occurs, it seems that self.__app.exec() is returning beforeaboutToQuit fires, so presumably the Qt event loop hasn't actually ended at that point?
But then, I'm hardly a Qt expert and that patch seems like a good workaround, at least. Thanks for digging into it! Hopefully the maintainer finds some time to look at this soon.
Activity
DominiqueCEOS commentedon Mar 17, 2026
i also ran into the same problem with Python 3.14.3 on Linux in an app using
aboutToQuit, i guess it might have something to do with the_set_running_looptiming?martinspinler commentedon May 13, 2026
Sorry for posting AI output (GLM5) directly, but this fix seems to work and the explanation seems reasonable to me.
Root Cause
In Python 3.14, the C extension
_asynciomodule added a strict check inenter_task()that verifies the thread-localasyncio_running_loopmatches the task's loop before stepping into a task. This is called whenever a task wakes up (e.g., viatask_wakeup→task_step→enter_task).The bug was in
_QEventLoop.run_until_complete(). Afterrun_forever()returns, itsfinallyblock callsasyncio.events._set_running_loop(None), clearing the running loop reference. Thenrun_until_completeimmediately callsself.__app.eventDispatcher().processEvents(AllEvents)to process any remaining Qt events. Some of those pending events are qasync timer callbacks that invoke asyncio handles — including task wakeups. When those callbacks run with the running loop set toNone, Python 3.14'senter_taskcheck fails with:This exception prevents tasks from completing properly during cleanup, causing
asyncio.Runner.close()to hang when it triesloop.run_until_complete(loop.shutdown_asyncgens())because the shutdown can never finish.benblank commentedon May 13, 2026
Good sleuthing! That explanation sure sounds plausible, though I'm not convinced that patch is the right fix; the add-remove-add-remove between
run_foreverandrun_until_completeseems suspicious. I wonder if the error might be the result of a design issue? It seems odd to me thatrun_until_completecallsrun_forever(which logs that the "Qt event loop ended" before returning) and then kicks off another round of event processing "after forever".Of course, it also seems odd to me that there would be unprocessed events after the event loop ends. Based on when the error occurs, it seems that
self.__app.exec()is returning beforeaboutToQuitfires, so presumably the Qt event loop hasn't actually ended at that point?But then, I'm hardly a Qt expert and that patch seems like a good workaround, at least. Thanks for digging into it! Hopefully the maintainer finds some time to look at this soon.