summaryrefslogtreecommitdiff
path: root/Lib/asyncio/tasks.py
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2019-05-27 14:45:12 +0200
committerGitHub <noreply@github.com>2019-05-27 14:45:12 +0200
commit431b540bf79f0982559b1b0e420b1b085f667bb7 (patch)
tree2e7027339ce786cc90e04cba1b03c71ecf38dfda /Lib/asyncio/tasks.py
parent16cefb0bc7b05c08caf08525398ff178c35dece4 (diff)
downloadcpython-git-431b540bf79f0982559b1b0e420b1b085f667bb7.tar.gz
bpo-32528: Make asyncio.CancelledError a BaseException. (GH-13528)
This will address the common mistake many asyncio users make: an "except Exception" clause breaking Tasks cancellation. In addition to this change, we stop inheriting asyncio.TimeoutError and asyncio.InvalidStateError from their concurrent.futures.* counterparts. There's no point for these exceptions to share the inheritance chain. In 3.9 we'll focus on implementing supervisors and cancel scopes, which should allow better handling of all exceptions, including SystemExit and KeyboardInterrupt
Diffstat (limited to 'Lib/asyncio/tasks.py')
-rw-r--r--Lib/asyncio/tasks.py10
1 files changed, 6 insertions, 4 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 1dc595298c..78e76003b3 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -260,11 +260,11 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
super().set_result(exc.value)
except exceptions.CancelledError:
super().cancel() # I.e., Future.cancel(self).
- except Exception as exc:
+ except (KeyboardInterrupt, SystemExit) as exc:
super().set_exception(exc)
+ raise
except BaseException as exc:
super().set_exception(exc)
- raise
else:
blocking = getattr(result, '_asyncio_future_blocking', None)
if blocking is not None:
@@ -318,7 +318,7 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
def __wakeup(self, future):
try:
future.result()
- except Exception as exc:
+ except BaseException as exc:
# This may also be a cancellation.
self.__step(exc)
else:
@@ -858,7 +858,9 @@ def run_coroutine_threadsafe(coro, loop):
def callback():
try:
futures._chain_future(ensure_future(coro, loop=loop), future)
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
if future.set_running_or_notify_cancel():
future.set_exception(exc)
raise