From 18236909a4ee75107696147766e45686c71ec8b8 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Sat, 26 Sep 2026 21:51:38 +0200 Subject: [PATCH 1/3] fix(python/adbc_driver_manager): cancel the connection when reading a partition Cursor.adbc_read_partition() reads through AdbcConnectionReadPartition and the returned stream belongs to the connection, but the cursor cancelled its statement instead: both from the SIGINT handler around the blocking calls and from Cursor.adbc_cancel(). A driver with no active query on the statement treats that as a no-op, leaving the partition read running. Track the current cancel target on the cursor: the statement after _clear()/execute(), the connection after adbc_read_partition(). Use it for adbc_cancel(), the blocking partition read, and the row iterator that consumes the result stream. Fix the AdbcStatement.cancel docstring, which claimed to cancel the connection. Test against the Flight SQL test server, whose "forever" query blocks ReadPartition until the client cancels the connection. Signed-off-by: Fredrik Fornwall Assisted-by: OpenAI Codex --- .../tests/test_errors.py | 28 +++++++++++++++++++ .../adbc_driver_manager/_lib.pyx | 2 +- .../adbc_driver_manager/dbapi.py | 27 ++++++++++-------- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/python/adbc_driver_flightsql/tests/test_errors.py b/python/adbc_driver_flightsql/tests/test_errors.py index 00110c472d2..fd47c9a721b 100644 --- a/python/adbc_driver_flightsql/tests/test_errors.py +++ b/python/adbc_driver_flightsql/tests/test_errors.py @@ -73,6 +73,34 @@ def _cancel(): cur.fetchone() +def test_read_partition_cancel(test_dbapi): + with test_dbapi.cursor() as cur: + partitions, _ = cur.adbc_execute_partitions("forever") + assert len(partitions) == 1 + + errors = [] + + def _read(): + try: + cur.adbc_read_partition(partitions[0]) + except Exception as e: + errors.append(e) + + t = threading.Thread(target=_read, daemon=True) + t.start() + time.sleep(2) + cur.adbc_cancel() + t.join(timeout=30) + assert not t.is_alive(), "cancel did not interrupt ReadPartition" + + assert len(errors) == 1 + assert isinstance(errors[0], test_dbapi.OperationalError) + assert str(errors[0]) == ( + "CANCELLED: [FlightSQL] context canceled" + " (Canceled; ReadPartition(DoGet)). Vendor code: 1" + ) + + def test_query_error_fetch(test_dbapi): with test_dbapi.cursor() as cur: cur.execute("error_do_get") diff --git a/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx b/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx index 076def8b5e5..2f645b7c14a 100644 --- a/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx +++ b/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx @@ -1540,7 +1540,7 @@ cdef class AdbcStatement(_AdbcHandle): check_error(status, &c_error) def cancel(self) -> None: - """Attempt to cancel any ongoing operations on the connection.""" + """Attempt to cancel any ongoing operations on the statement.""" cdef CAdbcError c_error = empty_error() cdef CAdbcStatusCode status with nogil: diff --git a/python/adbc_driver_manager/adbc_driver_manager/dbapi.py b/python/adbc_driver_manager/adbc_driver_manager/dbapi.py index e7c51c0c7fb..ff5e585adb9 100644 --- a/python/adbc_driver_manager/adbc_driver_manager/dbapi.py +++ b/python/adbc_driver_manager/adbc_driver_manager/dbapi.py @@ -49,6 +49,7 @@ import weakref from typing import ( Any, + Callable, Dict, List, Literal, @@ -804,6 +805,7 @@ def __init__( self._last_query: Optional[Union[str, bytes]] = None self._results: Optional["_RowIterator"] = None + self._cancel: Callable[[], None] = self._stmt.cancel self._arraysize = 1 self._rowcount = -1 self._bind_by_name = False @@ -815,6 +817,7 @@ def _clear(self) -> None: if self._results is not None: self._results.close() self._results = None + self._cancel = self._stmt.cancel @property def arraysize(self) -> int: @@ -943,7 +946,7 @@ def execute(self, operation: Union[bytes, str], parameters=None) -> "Self": handle, self._rowcount = _blocking_call( self._stmt.execute_query, (), {}, self._stmt.cancel ) - self._results = _RowIterator(self._stmt, handle, self._backend) + self._results = _RowIterator(self._cancel, handle, self._backend) return self def executemany(self, operation: Union[bytes, str], seq_of_parameters) -> None: @@ -1082,13 +1085,13 @@ def __next__(self) -> tuple: def adbc_cancel(self) -> None: """ - Cancel any ongoing operations on this statement. + Cancel any ongoing operations on this cursor. Notes ----- This is an extension and not part of the DBAPI standard. """ - self._stmt.cancel() + self._cancel() def adbc_ingest( self, @@ -1307,12 +1310,12 @@ def adbc_read_partition(self, partition: bytes) -> None: """ _requires_pyarrow() self._clear() - self._results = None + self._cancel = self._conn._conn.cancel handle = _blocking_call( - self._conn._conn.read_partition, (partition,), {}, self._stmt.cancel + self._conn._conn.read_partition, (partition,), {}, self._cancel ) self._rowcount = -1 - self._results = _RowIterator(self._stmt, handle, self._backend) + self._results = _RowIterator(self._cancel, handle, self._backend) @property def adbc_statement(self) -> _lib.AdbcStatement: @@ -1457,11 +1460,11 @@ class _RowIterator(_Closeable): def __init__( self, - stmt: _lib.AdbcStatement, + cancel: Callable[[], None], handle: _lib.ArrowArrayStreamHandle, dbapi_backend: _dbapi_backend.DbapiBackend, ) -> None: - self._stmt = stmt + self._cancel = cancel self._handle: Optional[_lib.ArrowArrayStreamHandle] = handle self._backend = dbapi_backend self._reader: Optional["AdbcRecordBatchReader"] = None @@ -1512,7 +1515,7 @@ def fetchone(self) -> Optional[tuple]: try: while True: self._current_batch = _blocking_call( - self.reader.read_next_batch, (), {}, self._stmt.cancel + self.reader.read_next_batch, (), {}, self._cancel ) if self._current_batch.num_rows > 0: break @@ -1548,10 +1551,10 @@ def fetchall(self) -> List[tuple]: return rows def fetch_arrow_table(self) -> "pyarrow.Table": - return _blocking_call(self.reader.read_all, (), {}, self._stmt.cancel) + return _blocking_call(self.reader.read_all, (), {}, self._cancel) def fetch_df(self) -> "pandas.DataFrame": - return _blocking_call(self.reader.read_pandas, (), {}, self._stmt.cancel) + return _blocking_call(self.reader.read_pandas, (), {}, self._cancel) def fetch_polars(self) -> "polars.DataFrame": import polars @@ -1562,7 +1565,7 @@ def fetch_polars(self) -> "polars.DataFrame": ), (), {}, - self._stmt.cancel, + self._cancel, ) def fetch_arrow(self) -> _lib.ArrowArrayStreamHandle: From b5001bee97543dfff770849e8d405f17d454f65d Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Thu, 12 Nov 2026 08:44:51 +0100 Subject: [PATCH 2/3] test(python/adbc_driver_flightsql): check cancel target of partition results Assert that the row iterator created by adbc_read_partition() cancels the connection, which the SIGINT handler uses during fetches. Signed-off-by: Fredrik Fornwall Assisted-by: OpenAI Codex --- python/adbc_driver_flightsql/tests/test_errors.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/python/adbc_driver_flightsql/tests/test_errors.py b/python/adbc_driver_flightsql/tests/test_errors.py index fd47c9a721b..31fe4c09de6 100644 --- a/python/adbc_driver_flightsql/tests/test_errors.py +++ b/python/adbc_driver_flightsql/tests/test_errors.py @@ -101,6 +101,16 @@ def _read(): ) +def test_read_partition_fetch_cancels_connection(test_dbapi): + # The stream from a partition belongs to the connection, so a SIGINT + # during a fetch must cancel the connection, not the statement. + with test_dbapi.cursor() as cur: + partitions, _ = cur.adbc_execute_partitions("SELECT 1") + cur.adbc_read_partition(partitions[0]) + assert cur._results._cancel == test_dbapi.adbc_connection.cancel + assert cur.fetchone() == (5,) + + def test_query_error_fetch(test_dbapi): with test_dbapi.cursor() as cur: cur.execute("error_do_get") From 5d0585a946759ed2eac4ca4c9de40db45b9be9c3 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Fri, 13 Nov 2026 04:04:52 +0100 Subject: [PATCH 3/3] test(python/adbc_driver_flightsql): consolidate cancellation coverage Assisted-by: OpenAI Codex Signed-off-by: Fredrik Fornwall --- python/adbc_driver_flightsql/tests/test_errors.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/python/adbc_driver_flightsql/tests/test_errors.py b/python/adbc_driver_flightsql/tests/test_errors.py index 31fe4c09de6..7ab9699cdaa 100644 --- a/python/adbc_driver_flightsql/tests/test_errors.py +++ b/python/adbc_driver_flightsql/tests/test_errors.py @@ -40,6 +40,11 @@ def assert_detail(e): def test_query_cancel(test_dbapi): with test_dbapi.cursor() as cur: + partitions, _ = cur.adbc_execute_partitions("SELECT 1") + cur.adbc_read_partition(partitions[0]) + assert cur._results._cancel == test_dbapi.adbc_connection.cancel + assert cur.fetchone() == (5,) + cur.execute("forever") cur.adbc_cancel() with pytest.raises( @@ -101,16 +106,6 @@ def _read(): ) -def test_read_partition_fetch_cancels_connection(test_dbapi): - # The stream from a partition belongs to the connection, so a SIGINT - # during a fetch must cancel the connection, not the statement. - with test_dbapi.cursor() as cur: - partitions, _ = cur.adbc_execute_partitions("SELECT 1") - cur.adbc_read_partition(partitions[0]) - assert cur._results._cancel == test_dbapi.adbc_connection.cancel - assert cur.fetchone() == (5,) - - def test_query_error_fetch(test_dbapi): with test_dbapi.cursor() as cur: cur.execute("error_do_get")