adafruit / Adafruit_CircuitPython_asyncio

CIrcuitPython subset of CPython asyncio library
MIT License
26 stars 17 forks source link

Exception within a task can cause other tasks to run before the exceptioned-task completes #36

Open anecdata opened 1 year ago

anecdata commented 1 year ago

Placeholder for now, until I can come up with a minimum reproducible example.

Not sure if this belongs here, or in circuitpython.

Occurs in 7.3.3 and 8.0.0-beta.5.

Basic scenario:

async def foo(interval1):
     print("A")
     # some_statement_that_produces_a_caught_exception()
     print("B")
await asyncio.sleep(interval1)

async def bar(interval2):
     print("C")
     print("D")
     await asyncio.sleep(interval2)

Output:

A
C
D
# traceback.print_exception(None, exc, exc.__traceback__) from library
B (??)

It seems to occur when the exception is caught by the library-internal exception handler, rather than when the exception is caught by user code.

edited to reflect additional testing

So maybe this is intended behavior? It was confusing that the exceptioned-task was interrupted, but the exception wasn't printed until after other tasks had run. I thought I had a case where print("B") would show up later after the library traceback, but now I'm not sure.

Addendum: When the library catches routine development bugs like this (syntax errors, type errors), but everything continues to operate, it can mask problems with the user code.