]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Dispose more carefully of connections and threads in concurrency tests
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 14 Nov 2021 19:34:22 +0000 (20:34 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 14 Nov 2021 19:46:24 +0000 (20:46 +0100)
I have seen some segfault on Windows sometimes, probably with the
test_concurrency thread stomping over the memory of the process forked
in test_multiprocess_close.

Async tests refactored the same way, although they don't exhibit the
same problem.

tests/test_concurrency.py
tests/test_concurrency_async.py

index 0fd628ab8f879102ab60e5b37ac7fb617ffbdcf4..981d8e2627d5298808a3223dbb6e96b4b56147c8 100644 (file)
@@ -149,6 +149,8 @@ def test_notifies(conn, dsn):
     assert n.payload == "2"
     assert t1 - t0 == pytest.approx(0.5, abs=0.05)
 
+    t.join()
+
 
 @pytest.mark.slow
 def test_cancel(conn, retries):
@@ -179,6 +181,8 @@ def test_cancel(conn, retries):
             conn.rollback()
             assert cur.execute("select 1").fetchone()[0] == 1
 
+            t.join()
+
 
 @pytest.mark.slow
 def test_identify_closure(dsn, retries):
@@ -192,18 +196,20 @@ def test_identify_closure(dsn, retries):
         with retry:
             conn = psycopg.connect(dsn)
             conn2 = psycopg.connect(dsn)
-
-            t0 = time.time()
-            sel = selectors.DefaultSelector()
-            sel.register(conn, selectors.EVENT_READ)
-            t = threading.Thread(target=closer)
-            t.start()
-
-            assert sel.select(timeout=1.0)
-            with pytest.raises(psycopg.OperationalError):
-                conn.execute("select 1")
-            t1 = time.time()
-            assert 0.3 < t1 - t0 < 0.6
-
-            conn.close()
-            conn2.close()
+            try:
+                t0 = time.time()
+                sel = selectors.DefaultSelector()
+                sel.register(conn, selectors.EVENT_READ)
+                t = threading.Thread(target=closer)
+                t.start()
+                try:
+                    assert sel.select(timeout=1.0)
+                    with pytest.raises(psycopg.OperationalError):
+                        conn.execute("select 1")
+                    t1 = time.time()
+                    assert 0.3 < t1 - t0 < 0.6
+                finally:
+                    t.join()
+            finally:
+                conn.close()
+                conn2.close()
index 3192a7abe731ae17b52078f637e047f59760ba08..f1aa8498b523835a51dcf6e827f518e0a4c1005e 100644 (file)
@@ -146,18 +146,21 @@ async def test_identify_closure(dsn, retries):
         with retry:
             aconn = await psycopg.AsyncConnection.connect(dsn)
             conn2 = await psycopg.AsyncConnection.connect(dsn)
-
-            t0 = time.time()
-            ev = asyncio.Event()
-            loop = asyncio.get_event_loop()
-            loop.add_reader(aconn.fileno(), ev.set)
-            create_task(closer())
-
-            await asyncio.wait_for(ev.wait(), 1.0)
-            with pytest.raises(psycopg.OperationalError):
-                await aconn.execute("select 1")
-            t1 = time.time()
-            assert 0.3 < t1 - t0 < 0.6
-
-            await aconn.close()
-            await conn2.close()
+            try:
+                t0 = time.time()
+                ev = asyncio.Event()
+                loop = asyncio.get_event_loop()
+                loop.add_reader(aconn.fileno(), ev.set)
+                t = create_task(closer())
+                try:
+
+                    await asyncio.wait_for(ev.wait(), 1.0)
+                    with pytest.raises(psycopg.OperationalError):
+                        await aconn.execute("select 1")
+                    t1 = time.time()
+                    assert 0.3 < t1 - t0 < 0.6
+                finally:
+                    await asyncio.gather(*t)
+            finally:
+                await aconn.close()
+                await conn2.close()