]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Don't raise exceptions on ServerError.close() if the connection is closed
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 7 Dec 2021 19:31:00 +0000 (20:31 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 7 Dec 2021 19:44:04 +0000 (20:44 +0100)
Close #173.

docs/news.rst
psycopg/psycopg/server_cursor.py
tests/test_server_cursor.py
tests/test_server_cursor_async.py

index 39afb48581965e367c3fada3b905824da67bdd9b..0748b9fdd5777cd35d1d9aca0fa92f8a5bdf88bc 100644 (file)
@@ -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
 ^^^^^^^^^^^^^
 
index 6e6653df548f5ae5eecc99d3fe9f0cb8ee5f14cd..28f198bac9ec8aca27a0bc47bef3b8bc416bce7a 100644 (file)
@@ -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(
index d4b62ed0307f086e48e45fb2ec7b8af8b21f1119..664b0ff59e1a263b1d547b34eeaa883870b86127 100644 (file)
@@ -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
index 417c4adb6ae08be543bf1dff3424f8bf2f9c72b7..29ac8a56283f35ee157194bed33bc99eda94ea19 100644 (file)
@@ -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