asyncio.exceptions.CancelledError #3268
-
QuestionWith NiceGUI 1.4.26 on Python 3.11.9 (x64) on Windows 11, I'm often getting an I'm not sure if it's because I'm misusing async somehow (I'm still fairly new to it) or if something else is going on. I've set an In case any of this is relevant:
I saw another discussion here with the same title asyncio.exceptions.CancelledError, but that was for a PyInstaller-bundled application and they were using Ctrl-C to exit, so I'm posting this separately. I don't have a minimal reproducible example at this point, because I'm not sure where in my code the error is originating from. Here's the traceback, as it appears in my log file: 2024-06-22 09:26:42 ERROR base_events default_exception_handler 1785 Exception in callback page.__call__.<locals>.check_for_late_return_value(<Task cancell...\page.py:110>>) at C:\dev\source\project\.venv\Lib\site-packages\nicegui\page.py:96
handle: <Handle page.__call__.<locals>.check_for_late_return_value(<Task cancell...\page.py:110>>) at C:\dev\source\project\.venv\Lib\site-packages\nicegui\page.py:96>
Traceback (most recent call last):
File "C:\Users\catamountjack\.rye\py\cpython@3.11.9\Lib\asyncio\events.py", line 84, in _run
self._context.run(self._callback, *self._args)
File "C:\dev\source\project\.venv\Lib\site-packages\nicegui\page.py", line 97, in check_for_late_return_value
if task.result() is not None:
^^^^^^^^^^^^^
asyncio.exceptions.CancelledError Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
I removed the calls to |
Beta Was this translation helpful? Give feedback.
-
Hi @CatamountJack, In some of our projects we also get occasional CancelledErrors, but we haven't narrowed it down to the root cause. We guess the problem lies within user code, i.e. not the NiceGUI library itself. Does anyone else get these errors? |
Beta Was this translation helpful? Give feedback.
-
Your comment made me think once again and I actually found a reproduction: import asyncio
import logging
from nicegui import app, ui
logging.basicConfig(level=logging.INFO, filename='test.log')
async def task():
try:
while True:
await asyncio.sleep(1)
logging.info('Tick')
except:
logging.exception('Error')
app.on_startup(task)
ui.button('Shutdown', on_click=app.shutdown)
ui.run(reload=False) What is happening here?
Of course this example doesn't rule out any bug in NiceGUI causing a similar result. But there's reason to assume that the (base-)exception is accidently caught and logged in user code. |
Beta Was this translation helpful? Give feedback.
-
Nice investigative work! :-) I just searched through my code base and I don't have any bare exceptions. I found that there are a lot of bare exceptions in other packages though... is it possible it's being caught in one of them and propagated up through? |
Beta Was this translation helpful? Give feedback.
-
I don't know if this is the only thing that causes it because I think I get it elsewhere where I'm not awaiting a button click (still investigating that), but I can reproduce it like this... from nicegui import ui
@ui.page('/')
async def main():
await ui.button("Button").clicked()
if __name__ in {"__main__", "__mp_main__"}:
ui.run(native=True, reload=False, ) The error shows up if you close the window while awaiting the button click. Which kinda makes me to blame, since I was the one that requested awaitable button clicks in the first place. My apologies! |
Beta Was this translation helpful? Give feedback.
I think I fixed it in PR #3296.