summaryrefslogtreecommitdiff
path: root/Lib/asyncio/base_events.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/base_events.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/base_events.py')
-rw-r--r--Lib/asyncio/base_events.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index de9fa4f4f7..63b072b851 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -186,7 +186,7 @@ def _interleave_addrinfos(addrinfos, first_address_family_count=1):
def _run_until_complete_cb(fut):
if not fut.cancelled():
exc = fut.exception()
- if isinstance(exc, BaseException) and not isinstance(exc, Exception):
+ if isinstance(exc, (SystemExit, KeyboardInterrupt)):
# Issue #22429: run_forever() already finished, no need to
# stop it.
return
@@ -1196,7 +1196,7 @@ class BaseEventLoop(events.AbstractEventLoop):
try:
await waiter
- except Exception:
+ except BaseException:
transport.close()
conmade_cb.cancel()
resume_cb.cancel()
@@ -1710,7 +1710,9 @@ class BaseEventLoop(events.AbstractEventLoop):
if self._exception_handler is None:
try:
self.default_exception_handler(context)
- except Exception:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException:
# Second protection layer for unexpected errors
# in the default implementation, as well as for subclassed
# event loops with overloaded "default_exception_handler".
@@ -1719,7 +1721,9 @@ class BaseEventLoop(events.AbstractEventLoop):
else:
try:
self._exception_handler(self, context)
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
# Exception in the user set custom exception handler.
try:
# Let's try default handler.
@@ -1728,7 +1732,9 @@ class BaseEventLoop(events.AbstractEventLoop):
'exception': exc,
'context': context,
})
- except Exception:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException:
# Guard 'default_exception_handler' in case it is
# overloaded.
logger.error('Exception in default exception handler '