← index #15941Issue #15187
Related · high · value 0.845
QUERY · ISSUE

Unexpected asyncio code exec between exc in task and call of global asyncio crash handler

openby mirkoopened 2024-10-01updated 2024-10-01
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

asyncio: Calling .run() within a task does not behave as per CPython

openby peterhinchopened 2024-06-02updated 2026-03-25
bugextmod

Port, board and/or hardware

Unix build

MicroPython version

MicroPython v1.23.0-preview.72.g4a2e510a8.dirty on 2024-02-04; linux [GCC 11.4.0] version

Reproduction

import asyncio

async def foo(s):
    while True:
        print(f"Task {s}")
        await asyncio.sleep(1)

async def main():
    asyncio.create_task(foo(1))
    await asyncio.sleep(2)
    asyncio.run(foo(2))  # CPython throws a RuntimeError
    await asyncio.sleep(2)
    asyncio.create_task(foo(1))
    await asyncio.sleep(5)
    print("Done")  # never happens in MP

asyncio.run(main())

Expected behaviour

On CPython issuing run() in a running task throws a RuntimeError:

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rats17
Task 1
Task 1
Task 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/adminpete/rats17.py", line 17, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/adminpete/rats17.py", line 11, in main
    asyncio.run(foo(2))  # CPython throws a RuntimeError
  File "/usr/lib/python3.10/asyncio/runners.py", line 33, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop
>>> 

Observed behaviour

The run() command appears to work, starting the second foo instance. However main never terminates.

MicroPython v1.23.0-preview.72.g4a2e510a8.dirty on 2024-02-04; linux [GCC 11.4.0] version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats17
Task 1
Task 1
Task 1
Task 2
Task 1
Task 2
Task 1
Task 2
Task 1
Task 2
... continues forever

Additional Information

This arose from this issue.

A possible fix to asyncio.core.py is

def run(coro):
    if cur_task is None:
        return run_until_complete(create_task(coro))
    else:
        raise RuntimeError("asyncio.run() cannot be called from a running event loop")

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied