mirror of
https://github.com/python/cpython.git
synced 2025-12-10 11:00:14 +00:00
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
This commit is contained in:
parent
16cefb0bc7
commit
431b540bf7
16 changed files with 147 additions and 67 deletions
|
|
@ -186,7 +186,7 @@ def _interleave_addrinfos(addrinfos, first_address_family_count=1):
|
||||||
def _run_until_complete_cb(fut):
|
def _run_until_complete_cb(fut):
|
||||||
if not fut.cancelled():
|
if not fut.cancelled():
|
||||||
exc = fut.exception()
|
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
|
# Issue #22429: run_forever() already finished, no need to
|
||||||
# stop it.
|
# stop it.
|
||||||
return
|
return
|
||||||
|
|
@ -1196,7 +1196,7 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await waiter
|
await waiter
|
||||||
except Exception:
|
except BaseException:
|
||||||
transport.close()
|
transport.close()
|
||||||
conmade_cb.cancel()
|
conmade_cb.cancel()
|
||||||
resume_cb.cancel()
|
resume_cb.cancel()
|
||||||
|
|
@ -1710,7 +1710,9 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
if self._exception_handler is None:
|
if self._exception_handler is None:
|
||||||
try:
|
try:
|
||||||
self.default_exception_handler(context)
|
self.default_exception_handler(context)
|
||||||
except Exception:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException:
|
||||||
# Second protection layer for unexpected errors
|
# Second protection layer for unexpected errors
|
||||||
# in the default implementation, as well as for subclassed
|
# in the default implementation, as well as for subclassed
|
||||||
# event loops with overloaded "default_exception_handler".
|
# event loops with overloaded "default_exception_handler".
|
||||||
|
|
@ -1719,7 +1721,9 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
self._exception_handler(self, context)
|
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.
|
# Exception in the user set custom exception handler.
|
||||||
try:
|
try:
|
||||||
# Let's try default handler.
|
# Let's try default handler.
|
||||||
|
|
@ -1728,7 +1732,9 @@ class BaseEventLoop(events.AbstractEventLoop):
|
||||||
'exception': exc,
|
'exception': exc,
|
||||||
'context': context,
|
'context': context,
|
||||||
})
|
})
|
||||||
except Exception:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException:
|
||||||
# Guard 'default_exception_handler' in case it is
|
# Guard 'default_exception_handler' in case it is
|
||||||
# overloaded.
|
# overloaded.
|
||||||
logger.error('Exception in default exception handler '
|
logger.error('Exception in default exception handler '
|
||||||
|
|
|
||||||
|
|
@ -182,7 +182,9 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
|
||||||
for callback, data in self._pending_calls:
|
for callback, data in self._pending_calls:
|
||||||
loop.call_soon(callback, *data)
|
loop.call_soon(callback, *data)
|
||||||
self._pending_calls = None
|
self._pending_calls = None
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
if waiter is not None and not waiter.cancelled():
|
if waiter is not None and not waiter.cancelled():
|
||||||
waiter.set_exception(exc)
|
waiter.set_exception(exc)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,9 @@ class Handle:
|
||||||
def _run(self):
|
def _run(self):
|
||||||
try:
|
try:
|
||||||
self._context.run(self._callback, *self._args)
|
self._context.run(self._callback, *self._args)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
cb = format_helpers._format_callback_source(
|
cb = format_helpers._format_callback_source(
|
||||||
self._callback, self._args)
|
self._callback, self._args)
|
||||||
msg = f'Exception in callback {cb}'
|
msg = f'Exception in callback {cb}'
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,16 @@ __all__ = ('CancelledError', 'InvalidStateError', 'TimeoutError',
|
||||||
'IncompleteReadError', 'LimitOverrunError',
|
'IncompleteReadError', 'LimitOverrunError',
|
||||||
'SendfileNotAvailableError')
|
'SendfileNotAvailableError')
|
||||||
|
|
||||||
import concurrent.futures
|
|
||||||
from . import base_futures
|
|
||||||
|
|
||||||
|
class CancelledError(BaseException):
|
||||||
class CancelledError(concurrent.futures.CancelledError):
|
|
||||||
"""The Future or Task was cancelled."""
|
"""The Future or Task was cancelled."""
|
||||||
|
|
||||||
|
|
||||||
class TimeoutError(concurrent.futures.TimeoutError):
|
class TimeoutError(Exception):
|
||||||
"""The operation exceeded the given deadline."""
|
"""The operation exceeded the given deadline."""
|
||||||
|
|
||||||
|
|
||||||
class InvalidStateError(concurrent.futures.InvalidStateError):
|
class InvalidStateError(Exception):
|
||||||
"""The operation is not allowed in this state."""
|
"""The operation is not allowed in this state."""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -212,7 +212,9 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
|
||||||
|
|
||||||
try:
|
try:
|
||||||
keep_open = self._protocol.eof_received()
|
keep_open = self._protocol.eof_received()
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(
|
self._fatal_error(
|
||||||
exc, 'Fatal error: protocol.eof_received() call failed.')
|
exc, 'Fatal error: protocol.eof_received() call failed.')
|
||||||
return
|
return
|
||||||
|
|
@ -235,7 +237,9 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
|
||||||
if isinstance(self._protocol, protocols.BufferedProtocol):
|
if isinstance(self._protocol, protocols.BufferedProtocol):
|
||||||
try:
|
try:
|
||||||
protocols._feed_data_to_buffered_proto(self._protocol, data)
|
protocols._feed_data_to_buffered_proto(self._protocol, data)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(exc,
|
self._fatal_error(exc,
|
||||||
'Fatal error: protocol.buffer_updated() '
|
'Fatal error: protocol.buffer_updated() '
|
||||||
'call failed.')
|
'call failed.')
|
||||||
|
|
@ -625,7 +629,9 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
|
||||||
except exceptions.CancelledError:
|
except exceptions.CancelledError:
|
||||||
# _close_self_pipe() has been called, stop waiting for data
|
# _close_self_pipe() has been called, stop waiting for data
|
||||||
return
|
return
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self.call_exception_handler({
|
self.call_exception_handler({
|
||||||
'message': 'Error on reading from the event loop self pipe',
|
'message': 'Error on reading from the event loop self pipe',
|
||||||
'exception': exc,
|
'exception': exc,
|
||||||
|
|
|
||||||
|
|
@ -208,12 +208,14 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await waiter
|
await waiter
|
||||||
except:
|
except BaseException:
|
||||||
transport.close()
|
transport.close()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# It's now up to the protocol to handle the connection.
|
# It's now up to the protocol to handle the connection.
|
||||||
except Exception as exc:
|
|
||||||
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
if self._debug:
|
if self._debug:
|
||||||
context = {
|
context = {
|
||||||
'message':
|
'message':
|
||||||
|
|
@ -370,7 +372,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
data = sock.recv(n)
|
data = sock.recv(n)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
return # try again next time
|
return # try again next time
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
fut.set_exception(exc)
|
fut.set_exception(exc)
|
||||||
else:
|
else:
|
||||||
fut.set_result(data)
|
fut.set_result(data)
|
||||||
|
|
@ -404,7 +408,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
nbytes = sock.recv_into(buf)
|
nbytes = sock.recv_into(buf)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
return # try again next time
|
return # try again next time
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
fut.set_exception(exc)
|
fut.set_exception(exc)
|
||||||
else:
|
else:
|
||||||
fut.set_result(nbytes)
|
fut.set_result(nbytes)
|
||||||
|
|
@ -447,7 +453,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
n = sock.send(view[start:])
|
n = sock.send(view[start:])
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
return
|
return
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
fut.set_exception(exc)
|
fut.set_exception(exc)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -487,7 +495,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
fut.add_done_callback(
|
fut.add_done_callback(
|
||||||
functools.partial(self._sock_write_done, fd))
|
functools.partial(self._sock_write_done, fd))
|
||||||
self.add_writer(fd, self._sock_connect_cb, fut, sock, address)
|
self.add_writer(fd, self._sock_connect_cb, fut, sock, address)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
fut.set_exception(exc)
|
fut.set_exception(exc)
|
||||||
else:
|
else:
|
||||||
fut.set_result(None)
|
fut.set_result(None)
|
||||||
|
|
@ -507,7 +517,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
# socket is still registered, the callback will be retried later
|
# socket is still registered, the callback will be retried later
|
||||||
pass
|
pass
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
fut.set_exception(exc)
|
fut.set_exception(exc)
|
||||||
else:
|
else:
|
||||||
fut.set_result(None)
|
fut.set_result(None)
|
||||||
|
|
@ -537,7 +549,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
conn.setblocking(False)
|
conn.setblocking(False)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
self.add_reader(fd, self._sock_accept, fut, True, sock)
|
self.add_reader(fd, self._sock_accept, fut, True, sock)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
fut.set_exception(exc)
|
fut.set_exception(exc)
|
||||||
else:
|
else:
|
||||||
fut.set_result((conn, address))
|
fut.set_result((conn, address))
|
||||||
|
|
@ -785,7 +799,9 @@ class _SelectorSocketTransport(_SelectorTransport):
|
||||||
buf = self._protocol.get_buffer(-1)
|
buf = self._protocol.get_buffer(-1)
|
||||||
if not len(buf):
|
if not len(buf):
|
||||||
raise RuntimeError('get_buffer() returned an empty buffer')
|
raise RuntimeError('get_buffer() returned an empty buffer')
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(
|
self._fatal_error(
|
||||||
exc, 'Fatal error: protocol.get_buffer() call failed.')
|
exc, 'Fatal error: protocol.get_buffer() call failed.')
|
||||||
return
|
return
|
||||||
|
|
@ -794,7 +810,9 @@ class _SelectorSocketTransport(_SelectorTransport):
|
||||||
nbytes = self._sock.recv_into(buf)
|
nbytes = self._sock.recv_into(buf)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
return
|
return
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(exc, 'Fatal read error on socket transport')
|
self._fatal_error(exc, 'Fatal read error on socket transport')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -804,7 +822,9 @@ class _SelectorSocketTransport(_SelectorTransport):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._protocol.buffer_updated(nbytes)
|
self._protocol.buffer_updated(nbytes)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(
|
self._fatal_error(
|
||||||
exc, 'Fatal error: protocol.buffer_updated() call failed.')
|
exc, 'Fatal error: protocol.buffer_updated() call failed.')
|
||||||
|
|
||||||
|
|
@ -815,7 +835,9 @@ class _SelectorSocketTransport(_SelectorTransport):
|
||||||
data = self._sock.recv(self.max_size)
|
data = self._sock.recv(self.max_size)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
return
|
return
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(exc, 'Fatal read error on socket transport')
|
self._fatal_error(exc, 'Fatal read error on socket transport')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -825,7 +847,9 @@ class _SelectorSocketTransport(_SelectorTransport):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._protocol.data_received(data)
|
self._protocol.data_received(data)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(
|
self._fatal_error(
|
||||||
exc, 'Fatal error: protocol.data_received() call failed.')
|
exc, 'Fatal error: protocol.data_received() call failed.')
|
||||||
|
|
||||||
|
|
@ -835,7 +859,9 @@ class _SelectorSocketTransport(_SelectorTransport):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
keep_open = self._protocol.eof_received()
|
keep_open = self._protocol.eof_received()
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(
|
self._fatal_error(
|
||||||
exc, 'Fatal error: protocol.eof_received() call failed.')
|
exc, 'Fatal error: protocol.eof_received() call failed.')
|
||||||
return
|
return
|
||||||
|
|
@ -871,7 +897,9 @@ class _SelectorSocketTransport(_SelectorTransport):
|
||||||
n = self._sock.send(data)
|
n = self._sock.send(data)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
pass
|
pass
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(exc, 'Fatal write error on socket transport')
|
self._fatal_error(exc, 'Fatal write error on socket transport')
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
|
@ -894,7 +922,9 @@ class _SelectorSocketTransport(_SelectorTransport):
|
||||||
n = self._sock.send(self._buffer)
|
n = self._sock.send(self._buffer)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
pass
|
pass
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._loop._remove_writer(self._sock_fd)
|
self._loop._remove_writer(self._sock_fd)
|
||||||
self._buffer.clear()
|
self._buffer.clear()
|
||||||
self._fatal_error(exc, 'Fatal write error on socket transport')
|
self._fatal_error(exc, 'Fatal write error on socket transport')
|
||||||
|
|
@ -970,7 +1000,9 @@ class _SelectorDatagramTransport(_SelectorTransport):
|
||||||
pass
|
pass
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
self._protocol.error_received(exc)
|
self._protocol.error_received(exc)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(exc, 'Fatal read error on datagram transport')
|
self._fatal_error(exc, 'Fatal read error on datagram transport')
|
||||||
else:
|
else:
|
||||||
self._protocol.datagram_received(data, addr)
|
self._protocol.datagram_received(data, addr)
|
||||||
|
|
@ -1007,7 +1039,9 @@ class _SelectorDatagramTransport(_SelectorTransport):
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
self._protocol.error_received(exc)
|
self._protocol.error_received(exc)
|
||||||
return
|
return
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(
|
self._fatal_error(
|
||||||
exc, 'Fatal write error on datagram transport')
|
exc, 'Fatal write error on datagram transport')
|
||||||
return
|
return
|
||||||
|
|
@ -1030,7 +1064,9 @@ class _SelectorDatagramTransport(_SelectorTransport):
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
self._protocol.error_received(exc)
|
self._protocol.error_received(exc)
|
||||||
return
|
return
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._fatal_error(
|
self._fatal_error(
|
||||||
exc, 'Fatal write error on datagram transport')
|
exc, 'Fatal write error on datagram transport')
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -527,7 +527,9 @@ class SSLProtocol(protocols.Protocol):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ssldata, appdata = self._sslpipe.feed_ssldata(data)
|
ssldata, appdata = self._sslpipe.feed_ssldata(data)
|
||||||
except Exception as e:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as e:
|
||||||
self._fatal_error(e, 'SSL error in data received')
|
self._fatal_error(e, 'SSL error in data received')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -542,7 +544,9 @@ class SSLProtocol(protocols.Protocol):
|
||||||
self._app_protocol, chunk)
|
self._app_protocol, chunk)
|
||||||
else:
|
else:
|
||||||
self._app_protocol.data_received(chunk)
|
self._app_protocol.data_received(chunk)
|
||||||
except Exception as ex:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as ex:
|
||||||
self._fatal_error(
|
self._fatal_error(
|
||||||
ex, 'application protocol failed to receive SSL data')
|
ex, 'application protocol failed to receive SSL data')
|
||||||
return
|
return
|
||||||
|
|
@ -628,7 +632,9 @@ class SSLProtocol(protocols.Protocol):
|
||||||
raise handshake_exc
|
raise handshake_exc
|
||||||
|
|
||||||
peercert = sslobj.getpeercert()
|
peercert = sslobj.getpeercert()
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
if isinstance(exc, ssl.CertificateError):
|
if isinstance(exc, ssl.CertificateError):
|
||||||
msg = 'SSL handshake failed on verifying the certificate'
|
msg = 'SSL handshake failed on verifying the certificate'
|
||||||
else:
|
else:
|
||||||
|
|
@ -691,7 +697,9 @@ class SSLProtocol(protocols.Protocol):
|
||||||
# delete it and reduce the outstanding buffer size.
|
# delete it and reduce the outstanding buffer size.
|
||||||
del self._write_backlog[0]
|
del self._write_backlog[0]
|
||||||
self._write_buffer_size -= len(data)
|
self._write_buffer_size -= len(data)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
if self._in_handshake:
|
if self._in_handshake:
|
||||||
# Exceptions will be re-raised in _on_handshake_complete.
|
# Exceptions will be re-raised in _on_handshake_complete.
|
||||||
self._on_handshake_complete(exc)
|
self._on_handshake_complete(exc)
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,9 @@ async def staggered_race(
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = await coro_fn()
|
result = await coro_fn()
|
||||||
except Exception as e:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as e:
|
||||||
exceptions[this_index] = e
|
exceptions[this_index] = e
|
||||||
this_failed.set() # Kickstart the next coroutine
|
this_failed.set() # Kickstart the next coroutine
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -260,11 +260,11 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
|
||||||
super().set_result(exc.value)
|
super().set_result(exc.value)
|
||||||
except exceptions.CancelledError:
|
except exceptions.CancelledError:
|
||||||
super().cancel() # I.e., Future.cancel(self).
|
super().cancel() # I.e., Future.cancel(self).
|
||||||
except Exception as exc:
|
except (KeyboardInterrupt, SystemExit) as exc:
|
||||||
super().set_exception(exc)
|
|
||||||
except BaseException as exc:
|
|
||||||
super().set_exception(exc)
|
super().set_exception(exc)
|
||||||
raise
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
|
super().set_exception(exc)
|
||||||
else:
|
else:
|
||||||
blocking = getattr(result, '_asyncio_future_blocking', None)
|
blocking = getattr(result, '_asyncio_future_blocking', None)
|
||||||
if blocking is not None:
|
if blocking is not None:
|
||||||
|
|
@ -318,7 +318,7 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
|
||||||
def __wakeup(self, future):
|
def __wakeup(self, future):
|
||||||
try:
|
try:
|
||||||
future.result()
|
future.result()
|
||||||
except Exception as exc:
|
except BaseException as exc:
|
||||||
# This may also be a cancellation.
|
# This may also be a cancellation.
|
||||||
self.__step(exc)
|
self.__step(exc)
|
||||||
else:
|
else:
|
||||||
|
|
@ -858,7 +858,9 @@ def run_coroutine_threadsafe(coro, loop):
|
||||||
def callback():
|
def callback():
|
||||||
try:
|
try:
|
||||||
futures._chain_future(ensure_future(coro, loop=loop), future)
|
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():
|
if future.set_running_or_notify_cancel():
|
||||||
future.set_exception(exc)
|
future.set_exception(exc)
|
||||||
raise
|
raise
|
||||||
|
|
|
||||||
|
|
@ -262,7 +262,9 @@ class _FlowControlMixin(Transport):
|
||||||
self._protocol_paused = True
|
self._protocol_paused = True
|
||||||
try:
|
try:
|
||||||
self._protocol.pause_writing()
|
self._protocol.pause_writing()
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._loop.call_exception_handler({
|
self._loop.call_exception_handler({
|
||||||
'message': 'protocol.pause_writing() failed',
|
'message': 'protocol.pause_writing() failed',
|
||||||
'exception': exc,
|
'exception': exc,
|
||||||
|
|
@ -276,7 +278,9 @@ class _FlowControlMixin(Transport):
|
||||||
self._protocol_paused = False
|
self._protocol_paused = False
|
||||||
try:
|
try:
|
||||||
self._protocol.resume_writing()
|
self._protocol.resume_writing()
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._loop.call_exception_handler({
|
self._loop.call_exception_handler({
|
||||||
'message': 'protocol.resume_writing() failed',
|
'message': 'protocol.resume_writing() failed',
|
||||||
'exception': exc,
|
'exception': exc,
|
||||||
|
|
|
||||||
|
|
@ -194,7 +194,9 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
|
||||||
self._child_watcher_callback, transp)
|
self._child_watcher_callback, transp)
|
||||||
try:
|
try:
|
||||||
await waiter
|
await waiter
|
||||||
except Exception:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException:
|
||||||
transp.close()
|
transp.close()
|
||||||
await transp._wait()
|
await transp._wait()
|
||||||
raise
|
raise
|
||||||
|
|
@ -390,7 +392,9 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
|
||||||
else:
|
else:
|
||||||
self._sock_sendfile_update_filepos(fileno, offset, total_sent)
|
self._sock_sendfile_update_filepos(fileno, offset, total_sent)
|
||||||
fut.set_exception(exc)
|
fut.set_exception(exc)
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._sock_sendfile_update_filepos(fileno, offset, total_sent)
|
self._sock_sendfile_update_filepos(fileno, offset, total_sent)
|
||||||
fut.set_exception(exc)
|
fut.set_exception(exc)
|
||||||
else:
|
else:
|
||||||
|
|
@ -641,7 +645,9 @@ class _UnixWritePipeTransport(transports._FlowControlMixin,
|
||||||
n = os.write(self._fileno, data)
|
n = os.write(self._fileno, data)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
n = 0
|
n = 0
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._conn_lost += 1
|
self._conn_lost += 1
|
||||||
self._fatal_error(exc, 'Fatal write error on pipe transport')
|
self._fatal_error(exc, 'Fatal write error on pipe transport')
|
||||||
return
|
return
|
||||||
|
|
@ -661,7 +667,9 @@ class _UnixWritePipeTransport(transports._FlowControlMixin,
|
||||||
n = os.write(self._fileno, self._buffer)
|
n = os.write(self._fileno, self._buffer)
|
||||||
except (BlockingIOError, InterruptedError):
|
except (BlockingIOError, InterruptedError):
|
||||||
pass
|
pass
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
self._buffer.clear()
|
self._buffer.clear()
|
||||||
self._conn_lost += 1
|
self._conn_lost += 1
|
||||||
# Remove writer here, _fatal_error() doesn't it
|
# Remove writer here, _fatal_error() doesn't it
|
||||||
|
|
@ -879,7 +887,9 @@ class BaseChildWatcher(AbstractChildWatcher):
|
||||||
def _sig_chld(self):
|
def _sig_chld(self):
|
||||||
try:
|
try:
|
||||||
self._do_waitpid_all()
|
self._do_waitpid_all()
|
||||||
except Exception as exc:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException as exc:
|
||||||
# self._loop should always be available here
|
# self._loop should always be available here
|
||||||
# as '_sig_chld' is added as a signal handler
|
# as '_sig_chld' is added as a signal handler
|
||||||
# in 'attach_loop'
|
# in 'attach_loop'
|
||||||
|
|
|
||||||
|
|
@ -388,7 +388,9 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
|
||||||
**kwargs)
|
**kwargs)
|
||||||
try:
|
try:
|
||||||
await waiter
|
await waiter
|
||||||
except Exception:
|
except (SystemExit, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException:
|
||||||
transp.close()
|
transp.close()
|
||||||
await transp._wait()
|
await transp._wait()
|
||||||
raise
|
raise
|
||||||
|
|
|
||||||
|
|
@ -476,7 +476,7 @@ class BaseEventLoopTests(test_utils.TestCase):
|
||||||
other_loop.run_until_complete, task)
|
other_loop.run_until_complete, task)
|
||||||
|
|
||||||
def test_run_until_complete_loop_orphan_future_close_loop(self):
|
def test_run_until_complete_loop_orphan_future_close_loop(self):
|
||||||
class ShowStopper(BaseException):
|
class ShowStopper(SystemExit):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def foo(delay):
|
async def foo(delay):
|
||||||
|
|
@ -487,10 +487,8 @@ class BaseEventLoopTests(test_utils.TestCase):
|
||||||
|
|
||||||
self.loop._process_events = mock.Mock()
|
self.loop._process_events = mock.Mock()
|
||||||
self.loop.call_soon(throw)
|
self.loop.call_soon(throw)
|
||||||
try:
|
with self.assertRaises(ShowStopper):
|
||||||
self.loop.run_until_complete(foo(0.1))
|
self.loop.run_until_complete(foo(0.1))
|
||||||
except ShowStopper:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# This call fails if run_until_complete does not clean up
|
# This call fails if run_until_complete does not clean up
|
||||||
# done-callback for the previous future.
|
# done-callback for the previous future.
|
||||||
|
|
|
||||||
|
|
@ -1527,7 +1527,7 @@ class BaseTaskTests:
|
||||||
async def sleeper():
|
async def sleeper():
|
||||||
await asyncio.sleep(10)
|
await asyncio.sleep(10)
|
||||||
|
|
||||||
base_exc = BaseException()
|
base_exc = SystemExit()
|
||||||
|
|
||||||
async def notmutch():
|
async def notmutch():
|
||||||
try:
|
try:
|
||||||
|
|
@ -1541,7 +1541,7 @@ class BaseTaskTests:
|
||||||
task.cancel()
|
task.cancel()
|
||||||
self.assertFalse(task.done())
|
self.assertFalse(task.done())
|
||||||
|
|
||||||
self.assertRaises(BaseException, test_utils.run_briefly, loop)
|
self.assertRaises(SystemExit, test_utils.run_briefly, loop)
|
||||||
|
|
||||||
self.assertTrue(task.done())
|
self.assertTrue(task.done())
|
||||||
self.assertFalse(task.cancelled())
|
self.assertFalse(task.cancelled())
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
Make asyncio.CancelledError a BaseException.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
@ -2672,8 +2672,10 @@ set_exception:
|
||||||
assert(o == Py_None);
|
assert(o == Py_None);
|
||||||
Py_DECREF(o);
|
Py_DECREF(o);
|
||||||
|
|
||||||
if (!PyErr_GivenExceptionMatches(et, PyExc_Exception)) {
|
if (PyErr_GivenExceptionMatches(et, PyExc_KeyboardInterrupt) ||
|
||||||
/* We've got a BaseException; re-raise it */
|
PyErr_GivenExceptionMatches(et, PyExc_SystemExit))
|
||||||
|
{
|
||||||
|
/* We've got a KeyboardInterrupt or a SystemError; re-raise it */
|
||||||
PyErr_Restore(et, ev, tb);
|
PyErr_Restore(et, ev, tb);
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
@ -2950,11 +2952,6 @@ task_wakeup(TaskObj *task, PyObject *o)
|
||||||
}
|
}
|
||||||
|
|
||||||
PyErr_Fetch(&et, &ev, &tb);
|
PyErr_Fetch(&et, &ev, &tb);
|
||||||
if (!PyErr_GivenExceptionMatches(et, PyExc_Exception)) {
|
|
||||||
/* We've got a BaseException; re-raise it */
|
|
||||||
PyErr_Restore(et, ev, tb);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
|
if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
|
||||||
PyErr_NormalizeException(&et, &ev, &tb);
|
PyErr_NormalizeException(&et, &ev, &tb);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue