diff options
author | Angus Gratton <angus@redyak.com.au> | 2024-11-20 16:22:56 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2024-11-28 23:12:34 +1100 |
commit | eb80b04944e01d437efc2fa29189ad1e2bdd997f (patch) | |
tree | f80f1e18d9a73781b5a8ddaa7584e88cf23bdce4 /tests/extmod/asyncio_new_event_loop.py | |
parent | 8ec067272a2b03c182f4723026e1d94dd6a44835 (diff) |
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 <angus@redyak.com.au>
Diffstat (limited to 'tests/extmod/asyncio_new_event_loop.py')
-rw-r--r-- | tests/extmod/asyncio_new_event_loop.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/tests/extmod/asyncio_new_event_loop.py b/tests/extmod/asyncio_new_event_loop.py index bebc3bf70..3f05ffdd5 100644 --- a/tests/extmod/asyncio_new_event_loop.py +++ b/tests/extmod/asyncio_new_event_loop.py @@ -12,6 +12,16 @@ if hasattr(asyncio, "set_event_loop"): asyncio.set_event_loop(asyncio.new_event_loop()) +def exception_handler(loop, context): + # This is a workaround for a difference between CPython and MicroPython: if + # a CPython event loop is closed while there are tasks pending (i.e. not finished) + # on it, then the task will log an error. MicroPython does not log this error. + if context.get("message", "") == "Task was destroyed but it is pending!": + pass + else: + loop.default_exception_handler(context) + + async def task(): for i in range(4): print("task", i) @@ -22,17 +32,21 @@ async def task(): async def main(): print("start") loop.create_task(task()) - await asyncio.sleep(0) + await asyncio.sleep(0) # yields, meaning new task will run once print("stop") loop.stop() # Use default event loop to run some tasks loop = asyncio.get_event_loop() +loop.set_exception_handler(exception_handler) loop.create_task(main()) loop.run_forever() +loop.close() # Create new event loop, old one should not keep running loop = asyncio.new_event_loop() +loop.set_exception_handler(exception_handler) loop.create_task(main()) loop.run_forever() +loop.close() |