From e525704800bbc483b4ca22997157d75bc90b9a41 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 10 Jan 2024 22:31:59 -0500 Subject: [PATCH] catch OSError (base of ConnectionError) and asyncpg errors for terminate Fixed regression in the asyncpg dialect caused by :ticket:`10717` in release 2.0.24 where the change that now attempts to gracefully close the asyncpg connection before terminating would not fall back to ``terminate()`` for other potential connection-related exceptions other than a timeout error, not taking into account cases where the graceful ``.close()`` attempt fails for other reasons such as connection errors. Fixes: #10863 Change-Id: If1791bce26803f92547cdf26fb641996c7f638fa (cherry picked from commit eff3aa8ad6bf74181280a85bf03d401126c65b01) --- doc/build/changelog/unreleased_20/10863.rst | 11 +++++++++++ lib/sqlalchemy/dialects/postgresql/asyncpg.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 doc/build/changelog/unreleased_20/10863.rst diff --git a/doc/build/changelog/unreleased_20/10863.rst b/doc/build/changelog/unreleased_20/10863.rst new file mode 100644 index 0000000000..df722f8fe4 --- /dev/null +++ b/doc/build/changelog/unreleased_20/10863.rst @@ -0,0 +1,11 @@ +.. change:: + :tags: bug, regression, postgresql + :tickets: 10863 + + Fixed regression in the asyncpg dialect caused by :ticket:`10717` in + release 2.0.24 where the change that now attempts to gracefully close the + asyncpg connection before terminating would not fall back to + ``terminate()`` for other potential connection-related exceptions other + than a timeout error, not taking into account cases where the graceful + ``.close()`` attempt fails for other reasons such as connection errors. + diff --git a/lib/sqlalchemy/dialects/postgresql/asyncpg.py b/lib/sqlalchemy/dialects/postgresql/asyncpg.py index 7e93b1232e..85affdca3a 100644 --- a/lib/sqlalchemy/dialects/postgresql/asyncpg.py +++ b/lib/sqlalchemy/dialects/postgresql/asyncpg.py @@ -890,7 +890,7 @@ class AsyncAdapt_asyncpg_connection(AdaptedConnection): # try to gracefully close; see #10717 # timeout added in asyncpg 0.14.0 December 2017 self.await_(self._connection.close(timeout=2)) - except asyncio.TimeoutError: + except (asyncio.TimeoutError, OSError, self.dbapi.PostgresError): # in the case where we are recycling an old connection # that may have already been disconnected, close() will # fail with the above timeout. in this case, terminate -- 2.47.2