From: Mike Bayer Date: Tue, 13 Feb 2024 13:45:53 +0000 (-0500) Subject: use correct exception for terminate catch + test X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80b52dc522f9f03a86ca6c3a5766cd9c594804ec;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git use correct exception for terminate catch + test Fixed regression caused by just-released fix for :ticket:`10863` where an invalid exception class were added to the "except" block, which does not get exercised unless such a catch actually happens. A mock-style test has been added to ensure this catch is exercised in unit tests. Fixes: #11005 Change-Id: I5a65403fb7bb35296ff44ae3cf6a336f8e0bda97 --- diff --git a/doc/build/changelog/unreleased_20/11005.rst b/doc/build/changelog/unreleased_20/11005.rst new file mode 100644 index 0000000000..7c9292e5c1 --- /dev/null +++ b/doc/build/changelog/unreleased_20/11005.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, postgresql, regression + :tickets: 11005 + + Fixed regression caused by just-released fix for :ticket:`10863` where an + invalid exception class were added to the "except" block, which does not + get exercised unless such a catch actually happens. A mock-style test has + been added to ensure this catch is exercised in unit tests. + diff --git a/lib/sqlalchemy/dialects/postgresql/asyncpg.py b/lib/sqlalchemy/dialects/postgresql/asyncpg.py index 590823ad1c..b8e815168b 100644 --- a/lib/sqlalchemy/dialects/postgresql/asyncpg.py +++ b/lib/sqlalchemy/dialects/postgresql/asyncpg.py @@ -913,7 +913,11 @@ class AsyncAdapt_asyncpg_connection(AsyncAdapt_dbapi_connection): # try to gracefully close; see #10717 # timeout added in asyncpg 0.14.0 December 2017 await_(self._connection.close(timeout=2)) - except (asyncio.TimeoutError, OSError, self.dbapi.PostgresError): + except ( + asyncio.TimeoutError, + OSError, + self.dbapi.asyncpg.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 diff --git a/test/dialect/postgresql/test_dialect.py b/test/dialect/postgresql/test_dialect.py index 32a5a84ac8..40718ee2df 100644 --- a/test/dialect/postgresql/test_dialect.py +++ b/test/dialect/postgresql/test_dialect.py @@ -178,6 +178,28 @@ class DialectTest(fixtures.TestBase): with expect_raises(dataclasses.FrozenInstanceError): r1.lower = 8 # type: ignore + @testing.only_on("postgresql+asyncpg") + def test_asyncpg_terminate_catch(self): + """test for #11005""" + + with testing.db.connect() as connection: + emulated_dbapi_connection = connection.connection.dbapi_connection + + async def boom(): + raise OSError("boom") + + with mock.patch.object( + emulated_dbapi_connection, + "_connection", + mock.Mock(close=mock.Mock(return_value=boom())), + ) as mock_asyncpg_connection: + emulated_dbapi_connection.terminate() + + eq_( + mock_asyncpg_connection.mock_calls, + [mock.call.close(timeout=2), mock.call.terminate()], + ) + def test_version_parsing(self): def mock_conn(res): return mock.Mock(