Skip to content

Commit

Permalink
asyncio.CancelledError is BaseException now, so fix except Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Oct 24, 2019
1 parent 0bb811d commit 51636f7
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 39 deletions.
8 changes: 6 additions & 2 deletions uvloop/_testbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ def __init__(self, test, sock, prog, timeout):
def run(self):
try:
self._prog(TestSocketWrapper(self._sock))
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
self._test._abort_socket_test(ex)


Expand Down Expand Up @@ -485,7 +487,9 @@ def _run(self):
try:
with conn:
self._handle_client(conn)
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
self._active = False
try:
raise
Expand Down
8 changes: 6 additions & 2 deletions uvloop/cbhandles.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ cdef class Handle:
raise RuntimeError('invalid Handle.cb_type: {}'.format(
cb_type))

except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
if cb_type == 1:
msg = 'Exception in callback {}'.format(callback)
else:
Expand Down Expand Up @@ -263,7 +265,9 @@ cdef class TimerHandle:
callback(*args)
else:
callback()
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
context = {
'message': 'Exception in callback {}'.format(callback),
'exception': ex,
Expand Down
8 changes: 6 additions & 2 deletions uvloop/dns.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ cdef void __on_addrinfo_resolved(uv.uv_getaddrinfo_t *resolver,
ai = AddrInfo()
ai.set_data(res)
callback(ai)
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
loop._handle_exception(ex)
finally:
request.on_done()
Expand All @@ -407,7 +409,9 @@ cdef void __on_nameinfo_resolved(uv.uv_getnameinfo_t* req,
else:
callback(((<bytes>hostname).decode(),
(<bytes>service).decode()))
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
loop._handle_exception(ex)
finally:
request.on_done()
8 changes: 6 additions & 2 deletions uvloop/handles/basetransport.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ cdef class UVBaseTransport(UVSocketHandle):
self._protocol_paused = 1
try:
self._protocol.pause_writing()
except Exception as exc:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as exc:
self._loop.call_exception_handler({
'message': 'protocol.pause_writing() failed',
'exception': exc,
Expand All @@ -83,7 +85,9 @@ cdef class UVBaseTransport(UVSocketHandle):
self._protocol_paused = 0
try:
self._protocol.resume_writing()
except Exception as exc:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as exc:
self._loop.call_exception_handler({
'message': 'protocol.resume_writing() failed',
'exception': exc,
Expand Down
4 changes: 3 additions & 1 deletion uvloop/handles/process.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,9 @@ cdef class UVProcessTransport(UVProcess):
cdef _call_connection_made(self, waiter):
try:
self._protocol.connection_made(self)
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
if waiter is not None and not waiter.cancelled():
waiter.set_exception(ex)
else:
Expand Down
108 changes: 81 additions & 27 deletions uvloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,9 @@ cdef class Loop:
data = result
else:
data = (<AddrInfo>result).unpack()
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
if not fut.cancelled():
fut.set_exception(ex)
else:
Expand Down Expand Up @@ -899,7 +901,9 @@ cdef class Loop:
# No need to re-add the reader, let's just wait until
# the poll handler calls this callback again.
pass
except Exception as exc:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as exc:
fut.set_exception(exc)
self._remove_reader(sock)
else:
Expand All @@ -924,7 +928,9 @@ cdef class Loop:
# No need to re-add the reader, let's just wait until
# the poll handler calls this callback again.
pass
except Exception as exc:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as exc:
fut.set_exception(exc)
self._remove_reader(sock)
else:
Expand Down Expand Up @@ -952,7 +958,9 @@ cdef class Loop:
except (BlockingIOError, InterruptedError):
# Try next time.
return
except Exception as exc:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as exc:
fut.set_exception(exc)
self._remove_writer(sock)
return
Expand Down Expand Up @@ -984,7 +992,9 @@ cdef class Loop:
# There is an active reader for _sock_accept, so
# do nothing, it will be called again.
pass
except Exception as exc:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as exc:
fut.set_exception(exc)
self._remove_reader(sock)
else:
Expand Down Expand Up @@ -1027,7 +1037,9 @@ cdef class Loop:
except (BlockingIOError, InterruptedError):
# socket is still registered, the callback will be retried later
pass
except Exception as exc:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as exc:
fut.set_exception(exc)
else:
fut.set_result(None)
Expand Down Expand Up @@ -1528,7 +1540,9 @@ cdef class Loop:

try:
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
app_transport.close()
conmade_cb.cancel()
resume_cb.cancel()
Expand Down Expand Up @@ -1695,7 +1709,9 @@ cdef class Loop:

try:
tcp._open(sock.fileno())
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
tcp._close()
raise

Expand Down Expand Up @@ -1729,7 +1745,9 @@ cdef class Loop:

try:
tcp._open(sock.fileno())
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
tcp._close()
raise

Expand Down Expand Up @@ -1910,7 +1928,9 @@ cdef class Loop:
tr._close()
tr = None
exceptions.append(exc)
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
if tr is not None:
tr._close()
tr = None
Expand Down Expand Up @@ -1948,7 +1968,9 @@ cdef class Loop:
tr._open(sock.fileno())
tr._init_protocol()
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
# It's OK to call `_close()` here, as opposed to
# `_force_close()` or `close()` as we want to terminate the
# transport immediately. The `waiter` can only be waken
Expand All @@ -1963,7 +1985,9 @@ cdef class Loop:
app_transport = protocol._get_app_transport()
try:
await ssl_waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
app_transport.close()
raise
return app_transport, app_protocol
Expand Down Expand Up @@ -2064,7 +2088,9 @@ cdef class Loop:
raise OSError(errno.EADDRINUSE, msg) from None
else:
raise
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
sock.close()
raise

Expand All @@ -2088,7 +2114,9 @@ cdef class Loop:

try:
pipe._open(sock.fileno())
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
pipe._close()
sock.close()
raise
Expand Down Expand Up @@ -2161,7 +2189,9 @@ cdef class Loop:
tr.connect(path)
try:
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
tr._close()
raise

Expand All @@ -2184,7 +2214,9 @@ cdef class Loop:
tr._open(sock.fileno())
tr._init_protocol()
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
tr._close()
raise

Expand All @@ -2194,7 +2226,9 @@ cdef class Loop:
app_transport = protocol._get_app_transport()
try:
await ssl_waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
app_transport.close()
raise
return app_transport, app_protocol
Expand Down Expand Up @@ -2233,7 +2267,9 @@ cdef class Loop:
else:
try:
value = repr(value)
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
value = ('Exception in __repr__ {!r}; '
'value type: {!r}'.format(ex, type(value)))
log_lines.append('{}: {}'.format(key, value))
Expand Down Expand Up @@ -2287,7 +2323,9 @@ cdef class Loop:
if self._exception_handler is None:
try:
self.default_exception_handler(context)
except Exception:
except (KeyboardInterrupt, SystemExit):
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".
Expand All @@ -2296,7 +2334,9 @@ cdef class Loop:
else:
try:
self._exception_handler(self, context)
except Exception as exc:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as exc:
# Exception in the user set custom exception handler.
try:
# Let's try default handler.
Expand All @@ -2305,7 +2345,9 @@ cdef class Loop:
'exception': exc,
'context': context,
})
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
# Guard 'default_exception_handler' in case it is
# overloaded.
aio_logger.error('Exception in default exception handler '
Expand Down Expand Up @@ -2558,14 +2600,18 @@ cdef class Loop:
app_transport = protocol._get_app_transport()
try:
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
app_transport.close()
raise
return app_transport, protocol
else:
try:
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
transport._close()
raise
return transport, protocol
Expand Down Expand Up @@ -2641,7 +2687,9 @@ cdef class Loop:

try:
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
proc.close()
raise

Expand Down Expand Up @@ -2693,7 +2741,9 @@ cdef class Loop:
transp._open(pipe.fileno())
transp._init_protocol()
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
transp._close()
raise
transp._attach_fileobj(pipe)
Expand All @@ -2718,7 +2768,9 @@ cdef class Loop:
transp._open(pipe.fileno())
transp._init_protocol()
await waiter
except Exception:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException:
transp._close()
raise
transp._attach_fileobj(pipe)
Expand Down Expand Up @@ -2948,7 +3000,9 @@ cdef class Loop:
if reuse_port:
self._sock_set_reuseport(udp._fileno())
udp._bind(lai.ai_addr, reuse_address)
except Exception as ex:
except (KeyboardInterrupt, SystemExit):
raise
except BaseException as ex:
lai = lai.ai_next
excs.append(ex)
continue
Expand Down
Loading

0 comments on commit 51636f7

Please sign in to comment.