Skip to content

asyncio: Add support to close the running loop and cancel background tasks #15715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

greezybacon
Copy link
Contributor

Summary

CPython will automatically shutdown the event loop and cancel all background async tasks when the main task exits from asyncio.run(...). This is especially important if the background tasks need to perform some sort of cleanup and/or reset.

This adds support for asyncio.all_tasks() which returns a set object similar to the way CPython works and allows inspection of all running tasks. This may be used inside a running event loop to forcibly cancel all running tasks.

Testing

This adds two tests in hopes of measuring compatibility with CPython. The first is asyncio_all_tasks which shows the collection of tasks returned from the asyncio.all_tasks() function. The second is asyncio_shutdown which shows the compatibility of shutting down background tasks as the main task exits for various reasons.

Trade-offs and Alternatives

The spirit of this feature was originally implemented in #9870; however, it needs this concept to fully work correctly.

Micropython still suffers from the issue where a try block in the main task will not have its finally executed if it's interrupted from a KeyboardInterrupted (like eg. from mpremote); however, at least this will trigger the proper cancel and cleanup of background tasks.

As a caveat the Loop in Micropython can be reused after being close()d. In CPython, such is forbidden. CPython uses a Runner to manage the shutdown of the loop and tasks after the main task exits.

Copy link

codecov bot commented Aug 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.44%. Comparing base (6c3dc0c) to head (f9bc1f1).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #15715      +/-   ##
==========================================
+ Coverage   98.43%   98.44%   +0.01%     
==========================================
  Files         163      163              
  Lines       21290    21304      +14     
==========================================
+ Hits        20956    20972      +16     
+ Misses        334      332       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Aug 25, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +576 +0.067% standard[incl +32(data)]
      stm32:  +260 +0.066% PYBV10
     mimxrt:  +272 +0.072% TEENSY40
        rp2:  +272 +0.029% RPI_PICO_W
       samd:  +268 +0.099% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Aug 26, 2024
This allows to collect all running tasks either for inspection and
debugging, or to cancel them all.

Signed-off-by: Jared Hancock <jared.hancock@centeredsolutions.com>
This matches the way CPython works. If the main task finishes or raises
an exception, the background tasks are all considered to be daemon tasks
and are canceled and the loop is shutdown.

Signed-off-by: Jared Hancock <jared.hancock@centeredsolutions.com>
@greezybacon greezybacon force-pushed the feature/asyncio-all-tasks branch from f9bc1f1 to 8b97a76 Compare August 10, 2025 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants