summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/selector_events.py
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2019-05-27 12:45:12 (GMT)
committerGitHub <noreply@github.com>2019-05-27 12:45:12 (GMT)
commit431b540bf79f0982559b1b0e420b1b085f667bb7 (patch)
tree2e7027339ce786cc90e04cba1b03c71ecf38dfda /Lib/asyncio/selector_events.py
parent16cefb0bc7b05c08caf08525398ff178c35dece4 (diff)
downloadcpython-431b540bf79f0982559b1b0e420b1b085f667bb7.zip
cpython-431b540bf79f0982559b1b0e420b1b085f667bb7.tar.gz
cpython-431b540bf79f0982559b1b0e420b1b085f667bb7.tar.bz2
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/selector_events.py')
-rw-r--r--Lib/asyncio/selector_events.py76
1 files changed, 56 insertions, 20 deletions
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index 6461d30..f5f43a9 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -208,12 +208,14 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
try:
await waiter
- except:
+ except BaseException:
transport.close()
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:
context = {
'message':
@@ -370,7 +372,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
data = sock.recv(n)
except (BlockingIOError, InterruptedError):
return # try again next time
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
fut.set_exception(exc)
else:
fut.set_result(data)
@@ -404,7 +408,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
nbytes = sock.recv_into(buf)
except (BlockingIOError, InterruptedError):
return # try again next time
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
fut.set_exception(exc)
else:
fut.set_result(nbytes)
@@ -447,7 +453,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
n = sock.send(view[start:])
except (BlockingIOError, InterruptedError):
return
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
fut.set_exception(exc)
return
@@ -487,7 +495,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
fut.add_done_callback(
functools.partial(self._sock_write_done, fd))
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)
else:
fut.set_result(None)
@@ -507,7 +517,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
except (BlockingIOError, InterruptedError):
# socket is still registered, the callback will be retried later
pass
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
fut.set_exception(exc)
else:
fut.set_result(None)
@@ -537,7 +549,9 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
conn.setblocking(False)
except (BlockingIOError, InterruptedError):
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)
else:
fut.set_result((conn, address))
@@ -785,7 +799,9 @@ class _SelectorSocketTransport(_SelectorTransport):
buf = self._protocol.get_buffer(-1)
if not len(buf):
raise RuntimeError('get_buffer() returned an empty buffer')
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(
exc, 'Fatal error: protocol.get_buffer() call failed.')
return
@@ -794,7 +810,9 @@ class _SelectorSocketTransport(_SelectorTransport):
nbytes = self._sock.recv_into(buf)
except (BlockingIOError, InterruptedError):
return
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(exc, 'Fatal read error on socket transport')
return
@@ -804,7 +822,9 @@ class _SelectorSocketTransport(_SelectorTransport):
try:
self._protocol.buffer_updated(nbytes)
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(
exc, 'Fatal error: protocol.buffer_updated() call failed.')
@@ -815,7 +835,9 @@ class _SelectorSocketTransport(_SelectorTransport):
data = self._sock.recv(self.max_size)
except (BlockingIOError, InterruptedError):
return
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(exc, 'Fatal read error on socket transport')
return
@@ -825,7 +847,9 @@ class _SelectorSocketTransport(_SelectorTransport):
try:
self._protocol.data_received(data)
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(
exc, 'Fatal error: protocol.data_received() call failed.')
@@ -835,7 +859,9 @@ class _SelectorSocketTransport(_SelectorTransport):
try:
keep_open = self._protocol.eof_received()
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(
exc, 'Fatal error: protocol.eof_received() call failed.')
return
@@ -871,7 +897,9 @@ class _SelectorSocketTransport(_SelectorTransport):
n = self._sock.send(data)
except (BlockingIOError, InterruptedError):
pass
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(exc, 'Fatal write error on socket transport')
return
else:
@@ -894,7 +922,9 @@ class _SelectorSocketTransport(_SelectorTransport):
n = self._sock.send(self._buffer)
except (BlockingIOError, InterruptedError):
pass
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._loop._remove_writer(self._sock_fd)
self._buffer.clear()
self._fatal_error(exc, 'Fatal write error on socket transport')
@@ -970,7 +1000,9 @@ class _SelectorDatagramTransport(_SelectorTransport):
pass
except OSError as 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')
else:
self._protocol.datagram_received(data, addr)
@@ -1007,7 +1039,9 @@ class _SelectorDatagramTransport(_SelectorTransport):
except OSError as exc:
self._protocol.error_received(exc)
return
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(
exc, 'Fatal write error on datagram transport')
return
@@ -1030,7 +1064,9 @@ class _SelectorDatagramTransport(_SelectorTransport):
except OSError as exc:
self._protocol.error_received(exc)
return
- except Exception as exc:
+ except (SystemExit, KeyboardInterrupt):
+ raise
+ except BaseException as exc:
self._fatal_error(
exc, 'Fatal write error on datagram transport')
return