Skip to content

Commit eb80b04

Browse files
projectgusdpgeorge
authored andcommitted
tests/extmod: Workaround CPython warning in asyncio_new_event_loop test.
This started failing in CI on the mingw build, after CPython updated to 3.12.7. The test prints two warnings during interpreter shutdown of "Task was destroyed but it is pending!". This didn't happen on other CPython builds, and I think that's because of finalizer order in CPython interpreter shutdown but not certain (the loop finalizer calls loop.close() if not already closed). Adding explicit calls to loop.close() causes the warning to be printed on every run with CPython 3.12.7 on Linux. Next, added the workaround exception handler to swallow this exception as MicroPython doesn't produce an equivalent. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 8ec0672 commit eb80b04

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tests/extmod/asyncio_new_event_loop.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
asyncio.set_event_loop(asyncio.new_event_loop())
1313

1414

15+
def exception_handler(loop, context):
16+
# This is a workaround for a difference between CPython and MicroPython: if
17+
# a CPython event loop is closed while there are tasks pending (i.e. not finished)
18+
# on it, then the task will log an error. MicroPython does not log this error.
19+
if context.get("message", "") == "Task was destroyed but it is pending!":
20+
pass
21+
else:
22+
loop.default_exception_handler(context)
23+
24+
1525
async def task():
1626
for i in range(4):
1727
print("task", i)
@@ -22,17 +32,21 @@ async def task():
2232
async def main():
2333
print("start")
2434
loop.create_task(task())
25-
await asyncio.sleep(0)
35+
await asyncio.sleep(0) # yields, meaning new task will run once
2636
print("stop")
2737
loop.stop()
2838

2939

3040
# Use default event loop to run some tasks
3141
loop = asyncio.get_event_loop()
42+
loop.set_exception_handler(exception_handler)
3243
loop.create_task(main())
3344
loop.run_forever()
45+
loop.close()
3446

3547
# Create new event loop, old one should not keep running
3648
loop = asyncio.new_event_loop()
49+
loop.set_exception_handler(exception_handler)
3750
loop.create_task(main())
3851
loop.run_forever()
52+
loop.close()

0 commit comments

Comments
 (0)