This is an automated email from the ASF dual-hosted git repository.

lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git


The following commit(s) were added to refs/heads/main by this push:
     new 99941f1942 fix(python/adbc_driver_manager): don't miscount statements 
closed twice (#5023)
99941f1942 is described below

commit 99941f19426f28ce0c4fddd280bef2897a464c81
Author: Fredrik Fornwall <[email protected]>
AuthorDate: Fri Nov 13 01:36:18 2026 +0100

    fix(python/adbc_driver_manager): don't miscount statements closed twice 
(#5023)
    
    `AdbcStatement.close()` decremented the connection's open-statement
    count before checking whether the statement was already released, so a
    second close() miscounted.
    
    This does not need an explicit double close: if releasing the statement
    fails (e.g. the server is gone) or is interrupted, the dbapi `Cursor`
    stays unclosed, and `Connection.close()` and `Cursor.__del__` close it
    again.
    
    That raised "Underflow in closing this AdbcStatement" or, with another
    cursor open, released the connection while that cursor's statement was
    still open.
    
    ---------
    
    Signed-off-by: Fredrik Fornwall <[email protected]>
---
 python/adbc_driver_manager/adbc_driver_manager/_lib.pyx |  2 +-
 python/adbc_driver_manager/tests/test_lowlevel.py       | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx 
b/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx
index 8927aef761..076def8b5e 100644
--- a/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx
+++ b/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx
@@ -1551,10 +1551,10 @@ cdef class AdbcStatement(_AdbcHandle):
         """Release the handle to the statement."""
         cdef CAdbcError c_error = empty_error()
         cdef CAdbcStatusCode status
-        self.connection._close_child()
         with self._lock:
             if self.statement.private_data == NULL:
                 return
+            self.connection._close_child()
 
             with nogil:
                 status = AdbcStatementRelease(&self.statement, &c_error)
diff --git a/python/adbc_driver_manager/tests/test_lowlevel.py 
b/python/adbc_driver_manager/tests/test_lowlevel.py
index 8b7f220e7e..bc629e1704 100644
--- a/python/adbc_driver_manager/tests/test_lowlevel.py
+++ b/python/adbc_driver_manager/tests/test_lowlevel.py
@@ -579,6 +579,20 @@ def test_child_tracking(sqlite_raw) -> None:
                 db.close()
 
 
[email protected]
+def test_statement_close_twice(sqlite_raw) -> None:
+    _, conn = sqlite_raw
+    with adbc_driver_manager.AdbcStatement(conn):
+        stmt = adbc_driver_manager.AdbcStatement(conn)
+        stmt.close()
+        stmt.close()
+        with pytest.raises(
+            RuntimeError,
+            match="Cannot close AdbcConnection with open AdbcStatement",
+        ):
+            conn.close()
+
+
 @pytest.mark.sqlite
 def test_pycapsule(sqlite_raw) -> None:
     _, conn = sqlite_raw

Reply via email to