{"id":965,"hash":"9f013f4dcf622ec37d95838f2cd529dcf5aebd444a82e064adf3e67b5c8be650","pattern":"Asyncio: Weirdness of Task exception was never retrieved","full_message":"Lets assume I have a simple code:\n\nimport asyncio\n\nasync def exc():\n    print(1 / 0)\n\nloop = asyncio.get_event_loop()\n\nloop.create_task(exc())\n\ntry:\n    loop.run_forever()\nexcept KeyboardInterrupt:\n    loop.stop()\n    loop.close()\n\nIf I run it, I get error message immediately \n\nTask exception was never retrieved\nfuture: <Task finished coro=<exc() done, defined at qq.py:4> exception=ZeroDivisionError('division by zero',)>\nTraceback (most recent call last):\n  File \"qq.py\", line 5, in exc\n    print(1 / 0)\nZeroDivisionError: division by zero\n\nBut, if I change loop.create_task(exc()) to task = loop.create_task(exc()) \n\nI'll get the same error message after click ctrl+c\n\nWhy does task assignment change the time of output of error?","ecosystem":"pypi","package_name":"exception","package_version":null,"solution":"A Exception in the Task (of underlying asyncio.Future to be precise) can be retrieved with Future.exception(). If it's not retrieved, the exception will be handled at release of the Future object with eventloop's call_exception_handler.\n\nSo, as @dirn pointed, while the Task has reference (assigned to variable in your case) it's not going be freed, therefore del task_future won't be called, loop's handler won't be executed either.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/46890646/asyncio-weirdness-of-task-exception-was-never-retrieved","votes":45,"created_at":"2026-04-19T04:52:05.813462+00:00","updated_at":"2026-04-19T04:52:05.813462+00:00"}