Skip to content

Commit 33e6de3

Browse files
committed
Handle asyncio.CancelledError (now a BaseException)
1 parent 7cb31bc commit 33e6de3

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

asyncpg/connect_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ async def _connect_addr(*, addr, loop, timeout, params, config,
541541
if timeout <= 0:
542542
raise asyncio.TimeoutError
543543
await asyncio.wait_for(connected, loop=loop, timeout=timeout)
544-
except Exception:
544+
except (Exception, asyncio.CancelledError):
545545
tr.close()
546546
raise
547547

@@ -614,7 +614,7 @@ async def _negotiate_ssl_connection(host, port, conn_factory, *, loop, ssl,
614614

615615
try:
616616
return await conn_factory(sock=sock) # Must come after tr.close()
617-
except Exception:
617+
except (Exception, asyncio.CancelledError):
618618
sock.close()
619619
raise
620620

asyncpg/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ async def close(self, *, timeout=None):
10801080
try:
10811081
if not self.is_closed():
10821082
await self._protocol.close(timeout)
1083-
except Exception:
1083+
except (Exception, asyncio.CancelledError):
10841084
# If we fail to close gracefully, abort the connection.
10851085
self._abort()
10861086
raise
@@ -1213,7 +1213,7 @@ async def _cancel(self, waiter):
12131213
# the CancelledError, and don't want the loop to warn about
12141214
# an unretrieved exception.
12151215
pass
1216-
except Exception as ex:
1216+
except (Exception, asyncio.CancelledError) as ex:
12171217
if not waiter.done():
12181218
waiter.set_exception(ex)
12191219
finally:

asyncpg/pool.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ async def acquire(self) -> PoolConnectionProxy:
146146
if self._setup is not None:
147147
try:
148148
await self._setup(proxy)
149-
except Exception as ex:
149+
except (Exception, asyncio.CancelledError) as ex:
150150
# If a user-defined `setup` function fails, we don't
151151
# know if the connection is safe for re-use, hence
152152
# we close it. A new connection will be created
@@ -204,7 +204,7 @@ async def release(self, timeout):
204204
budget -= time.monotonic() - started
205205

206206
await self._con.reset(timeout=budget)
207-
except Exception as ex:
207+
except (Exception, asyncio.CancelledError) as ex:
208208
# If the `reset` call failed, terminate the connection.
209209
# A new one will be created when `acquire` is called
210210
# again.
@@ -480,7 +480,7 @@ async def _get_new_connection(self):
480480
if self._init is not None:
481481
try:
482482
await self._init(con)
483-
except Exception as ex:
483+
except (Exception, asyncio.CancelledError) as ex:
484484
# If a user-defined `init` function fails, we don't
485485
# know if the connection is safe for re-use, hence
486486
# we close it. A new connection will be created
@@ -587,7 +587,7 @@ async def _acquire_impl():
587587
ch = await self._queue.get() # type: PoolConnectionHolder
588588
try:
589589
proxy = await ch.acquire() # type: PoolConnectionProxy
590-
except Exception:
590+
except (Exception, asyncio.CancelledError):
591591
self._queue.put_nowait(ch)
592592
raise
593593
else:
@@ -679,7 +679,7 @@ async def close(self):
679679
ch.close() for ch in self._holders]
680680
await asyncio.gather(*close_coros, loop=self._loop)
681681

682-
except Exception:
682+
except (Exception, asyncio.CancelledError):
683683
self.terminate()
684684
raise
685685

asyncpg/protocol/protocol.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ cdef class BaseProtocol(CoreProtocol):
350350
sink(buffer),
351351
timeout=timer.get_remaining_budget(),
352352
loop=self.loop)
353-
except Exception as ex:
353+
except (Exception, asyncio.CancelledError) as ex:
354354
# Abort the COPY operation on any error in
355355
# output sink.
356356
self._request_cancel()
@@ -476,7 +476,7 @@ cdef class BaseProtocol(CoreProtocol):
476476
else:
477477
raise apg_exc.InternalClientError('TimoutError was not raised')
478478

479-
except Exception as e:
479+
except (Exception, asyncio.CancelledError) as e:
480480
self._write_copy_fail_msg(str(e))
481481
self._request_cancel()
482482
# Make asyncio shut up about unretrieved QueryCanceledError

0 commit comments

Comments
 (0)