You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by li...@apache.org on 2023/03/13 13:35:29 UTC

[arrow-adbc] branch main updated: feat(python/adbc_driver_manager): add __del__ for resources (#498)

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 4de4fde  feat(python/adbc_driver_manager): add __del__ for resources (#498)
4de4fde is described below

commit 4de4fdeb6e7f661a404da37889f5204552046b8c
Author: David Li <li...@gmail.com>
AuthorDate: Mon Mar 13 09:35:23 2023 -0400

    feat(python/adbc_driver_manager): add __del__ for resources (#498)
    
    This helps ensure we don't leak resources. A warning will be raised when
    running under a 'test' environment to help developers catch issues.
    
    Fixes #454.
---
 .../adbc_driver_manager/dbapi.py                   | 55 +++++++++++++++++++++-
 python/adbc_driver_manager/tests/test_dbapi.py     | 17 +++++++
 2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/python/adbc_driver_manager/adbc_driver_manager/dbapi.py b/python/adbc_driver_manager/adbc_driver_manager/dbapi.py
index 66500be..eddb32b 100644
--- a/python/adbc_driver_manager/adbc_driver_manager/dbapi.py
+++ b/python/adbc_driver_manager/adbc_driver_manager/dbapi.py
@@ -15,11 +15,22 @@
 # specific language governing permissions and limitations
 # under the License.
 
-"""
-PEP 249 (DB-API 2.0) API wrapper for the ADBC Driver Manager.
+"""PEP 249 (DB-API 2.0) API wrapper for the ADBC Driver Manager.
+
+Resource Management
+===================
+
+You must ``close()`` Connection and Cursor objects, or else driver
+resources may be leaked.  A ``__del__`` is implemented as a fallback,
+but Python does not guarantee the timing of when this is called.  For
+development, ``__del__`` will raise a ResourceWarning when running
+under pytest, or when the environment variable
+``_ADBC_DRIVER_MANAGER_WARN_UNCLOSED_RESOURCE`` is set to ``1``.
+
 """
 
 import datetime
+import os
 import threading
 import time
 import typing
@@ -257,6 +268,7 @@ class Connection(_Closeable):
         conn: _lib.AdbcConnection,
         conn_kwargs: Optional[Dict[str, str]] = None,
     ) -> None:
+        self._closed = False
         if isinstance(db, _SharedDatabase):
             self._db = db.clone()
         else:
@@ -284,8 +296,12 @@ class Connection(_Closeable):
         Failure to close a connection may leak memory or database
         connections.
         """
+        if self._closed:
+            return
+
         self._conn.close()
         self._db.close()
+        self._closed = True
 
     def commit(self) -> None:
         """Explicitly commit."""
@@ -301,6 +317,11 @@ class Connection(_Closeable):
         if self._commit_supported:
             self._conn.rollback()
 
+    def __del__(self) -> None:
+        if not self._closed:
+            self.close()
+            _warn_unclosed("adbc_driver_manager.dbapi.Connection")
+
     # ------------------------------------------------------------
     # API Extensions
     # ------------------------------------------------------------
@@ -466,6 +487,7 @@ class Cursor(_Closeable):
         self._results: Optional["_RowIterator"] = None
         self._arraysize = 1
         self._rowcount = -1
+        self._closed = False
 
     @property
     def arraysize(self) -> int:
@@ -512,9 +534,14 @@ class Cursor(_Closeable):
 
     def close(self):
         """Close the cursor and free resources."""
+        if self._closed:
+            return
+
         if self._results is not None:
             self._results.close()
+            self._results = None
         self._stmt.close()
+        self._closed = True
 
     def _bind(self, parameters) -> None:
         if isinstance(parameters, pyarrow.RecordBatch):
@@ -666,6 +693,11 @@ class Cursor(_Closeable):
         """Preallocate memory for the result set (no-op)."""
         pass
 
+    def __del__(self) -> None:
+        if not self._closed:
+            self.close()
+            _warn_unclosed("adbc_driver_manager.dbapi.Cursor")
+
     def __iter__(self):
         return self
 
@@ -830,6 +862,10 @@ class Cursor(_Closeable):
         return self._results.fetch_df()
 
 
+# ----------------------------------------------------------
+# Utilities
+
+
 class _RowIterator(_Closeable):
     """Track state needed to iterate over the result set."""
 
@@ -892,3 +928,18 @@ class _RowIterator(_Closeable):
 
     def fetch_df(self):
         return self._reader.read_pandas()
+
+
+_PYTEST_ENV_VAR = "PYTEST_CURRENT_TEST"
+_ADBC_ENV_VAR = "_ADBC_DRIVER_MANAGER_WARN_UNCLOSED_RESOURCE"
+
+
+def _warn_unclosed(name):
+    if _PYTEST_ENV_VAR in os.environ or os.environ.get(_ADBC_ENV_VAR) == "1":
+        warnings.warn(
+            f"A {name} was not explicitly close()d, which may leak "
+            f"driver resources. (This warning is only emitted if "
+            f"{_PYTEST_ENV_VAR} or {_ADBC_ENV_VAR} are set.)",
+            category=ResourceWarning,
+            stacklevel=2,
+        )
diff --git a/python/adbc_driver_manager/tests/test_dbapi.py b/python/adbc_driver_manager/tests/test_dbapi.py
index f40ea81..a75bfed 100644
--- a/python/adbc_driver_manager/tests/test_dbapi.py
+++ b/python/adbc_driver_manager/tests/test_dbapi.py
@@ -292,3 +292,20 @@ def test_executemany(sqlite):
         assert next(cur) == (3, 4)
         assert cur.rownumber == 2
         assert next(cur) == (5, 6)
+
+
+@pytest.mark.sqlite
+def test_close_warning(sqlite):
+    with pytest.warns(
+        ResourceWarning,
+        match=r"A adbc_driver_manager.dbapi.Cursor was not explicitly close\(\)d",
+    ):
+        cur = sqlite.cursor()
+        del cur
+
+    with pytest.warns(
+        ResourceWarning,
+        match=r"A adbc_driver_manager.dbapi.Connection was not explicitly close\(\)d",
+    ):
+        conn = dbapi.connect(driver="adbc_driver_sqlite")
+        del conn