QUERY · ISSUE
Unexpected asyncio code exec between exc in task and call of global asyncio crash handler
bug
Port, board and/or hardware
unix and esp32
MicroPython version
MicroPython v1.23.0-preview.436.gc8021842c
Reproduction
import asyncio
class Loop():
async def run(self):
print("---", self.__class__.__name__, "---")
async def run_forever(self):
while True:
try:
await self.run()
await asyncio.sleep(0.1)
except Exception as exc:
print("EEE", exc)
raise
class Loop_1(Loop):
pass
class Loop_2(Loop):
async def run(self):
await super().run()
raise Exception("MOEP!")
class Loop_3(Loop):
pass
def async_crash_hdlr(loop, ctx):
# called when unhandled exceptions thrown in asyncio tasks.
# this function is called from within a task running in the mainloop.
print("Global asyncio exception handler called -> FREEZE")
while True:
pass
async def main():
asyncio.get_event_loop().set_exception_handler(async_crash_hdlr)
loop1 = Loop_1()
loop2 = Loop_2()
loop3 = Loop_3()
print("Starting loop1")
asyncio.create_task(loop1.run_forever())
print("Starting loop2")
asyncio.create_task(loop2.run_forever())
print("Starting loop3")
asyncio.create_task(loop3.run_forever())
while True:
print("--- MAINLOOP ---")
await asyncio.sleep(1)
print("Entering main program")
asyncio.run(main())
Expected behaviour
Expected the defined global asyncio exception handler (async_crash_hdlr) being called immediately.
Expected output from above code (as per CPython3):
Entering main program
Starting loop1
Starting loop2
Starting loop3
--- MAINLOOP ---
--- Loop_1 ---
--- Loop_2 ---
EEE MOEP!
Global asyncio exception handler called -> FREEZE
Observed behaviour
Unexpected to me, between the exception being raised in Loop2 and async_crash_hdlr() being called, there is still asyncio code being executed, despite no fruther await or alike in between.
In this very case (code from) Loop3 is being run:
Entering main program
Starting loop1
Starting loop2
Starting loop3
--- MAINLOOP ---
--- Loop_1 ---
--- Loop_2 ---
EEE MOEP!
--- Loop_3 --- # <<<<<-------------------------
Global asyncio exception handler called -> FREEZE
Additional Information
No, I've provided everything above.
Code of Conduct
Yes, I agree
CANDIDATE · ISSUE
uasyncio gather() return_exceptions behaviour differs from CPython
extmod
I think this is a regression. The following script produces an unexpected result in MicroPython:
try:
import uasyncio as asyncio
except ImportError:
import asyncio
async def will_stop(n):
print('Runs to completion')
for _ in range(6):
await asyncio.sleep(1)
return 2 * n
async def forever(n):
print('forever() will be cancelled')
while True:
await asyncio.sleep(1)
n += 1
return n
async def do_cancel(task):
await asyncio.sleep(3)
print('About to cancel forever')
task.cancel()
async def main():
tasks = [asyncio.create_task(forever(70))]
tasks.append(will_stop(21))
asyncio.create_task(do_cancel(tasks[0]))
res = None
try:
res = await asyncio.gather(*tasks, return_exceptions=True)
except asyncio.CancelledError:
print('Cancelled')
print('Result: ', res)
asyncio.run(main())
CPython (expected behaviour):
adminpete@debian8:/mnt/qnap2/temp$ python3
Python 3.8.0 (default, Dec 6 2019, 16:20:13)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rats40
forever() will be cancelled
Runs to completion
About to cancel forever
Result: [CancelledError(), 42]
>>>
MicroPython (behaves as if return_exceptions==False):
MicroPython v1.17 on 2021-09-02; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats40
forever() will be cancelled
Runs to completion
About to cancel forever
Cancelled
Result: None
>>>