Skip to content

Commit 4aec5dc

Browse files
committed
asyncio: Properly cancel the main task on exception
If the main task is interrupted by e.g. a KeyboardInterrupt, then the main task needs to have the exception injected into it so it will run the exception handlers and contextmanager __aexit__ methods.
1 parent b52fe52 commit 4aec5dc

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

extmod/uasyncio/core.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,20 @@ def create_task(coro):
145145
_task_queue.push(t)
146146
return t
147147

148+
def run_until_complete(main_task=None):
149+
try:
150+
_run_until_complete(main_task)
151+
except BaseException as exc:
152+
try:
153+
if main_task:
154+
main_task.coro.throw(exc)
155+
except StopIteration:
156+
# The async task threw the exception
157+
pass
158+
raise
148159

149160
# Keep scheduling tasks until there are none left to schedule
150-
def run_until_complete(main_task=None):
161+
def _run_until_complete(main_task=None):
151162
global cur_task
152163
excs_all = (CancelledError, Exception) # To prevent heap allocation in loop
153164
excs_stop = (CancelledError, StopIteration) # To prevent heap allocation in loop

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy