From: Daniele Varrazzo Date: Tue, 7 Dec 2021 19:31:00 +0000 (+0100) Subject: Don't raise exceptions on ServerError.close() if the connection is closed X-Git-Tag: pool-3.1~94 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dacd5d7e7d652c87cf4238610fb18a31e447a266;p=thirdparty%2Fpsycopg.git Don't raise exceptions on ServerError.close() if the connection is closed Close #173. --- diff --git a/docs/news.rst b/docs/news.rst index 39afb4858..0748b9fdd 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -16,6 +16,13 @@ Psycopg 3.1 (unreleased) Current release --------------- +Psycopg 3.0.6 +^^^^^^^^^^^^^ + +- `ServerCursor.close()` doesn't raise exceptions if the connection is closed + (:ticket:`#173`). + + Psycopg 3.0.5 ^^^^^^^^^^^^^ diff --git a/psycopg/psycopg/server_cursor.py b/psycopg/psycopg/server_cursor.py index 6e6653df5..28f198bac 100644 --- a/psycopg/psycopg/server_cursor.py +++ b/psycopg/psycopg/server_cursor.py @@ -240,7 +240,8 @@ class ServerCursor(Cursor[Row]): with self._conn.lock: if self.closed: return - self._conn.wait(self._helper._close_gen(self)) + if not self._conn.closed: + self._conn.wait(self._helper._close_gen(self)) super().close() def execute( @@ -363,7 +364,8 @@ class AsyncServerCursor(AsyncCursor[Row]): async with self._conn.lock: if self.closed: return - await self._conn.wait(self._helper._close_gen(self)) + if not self._conn.closed: + await self._conn.wait(self._helper._close_gen(self)) await super().close() async def execute( diff --git a/tests/test_server_cursor.py b/tests/test_server_cursor.py index d4b62ed03..664b0ff59 100644 --- a/tests/test_server_cursor.py +++ b/tests/test_server_cursor.py @@ -130,6 +130,13 @@ def test_close_idempotent(conn): cur.close() +def test_close_broken_conn(conn): + cur = conn.cursor("foo") + conn.close() + cur.close() + assert cur.closed + + def test_cursor_close_fetchone(conn): cur = conn.cursor("foo") assert not cur.closed diff --git a/tests/test_server_cursor_async.py b/tests/test_server_cursor_async.py index 417c4adb6..29ac8a562 100644 --- a/tests/test_server_cursor_async.py +++ b/tests/test_server_cursor_async.py @@ -137,6 +137,13 @@ async def test_close_idempotent(aconn): await cur.close() +async def test_close_broken_conn(aconn): + cur = aconn.cursor("foo") + await aconn.close() + await cur.close() + assert cur.closed + + async def test_cursor_close_fetchone(aconn): cur = aconn.cursor("foo") assert not cur.closed