Skip to content

Commit f1d4061

Browse files
vuanhphungIsaac
andcommitted
fix: clear the GC pause flag inside the lock; document the override
__exit__ cleared _joined before taking the lock, so an interrupt at the lock acquisition could skip the decrement. Clear it inside the lock, with no call between it and the decrement (as already in __enter__). The CHANGELOG now says that a gc.disable() made while a batch is being built is undone when the pause ends, while a GC disabled before fetchall() is left alone. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent ef58abe commit f1d4061

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- Fix inline parameters (`use_inline_params=True`) so a sequence directly after `IN` renders as a list again: `x IN %(ids)s` renders as `x IN (1,2,3)` instead of `x IN ARRAY(1,2,3)`, which Spark rejects. Inside the list, tuples render as rows for a multi-column `IN`, and a `set` or `frozenset` is accepted again. Sequences anywhere else still render as `ARRAY(...)` ([#629](https://github.com/databricks/databricks-sql-python/issues/629))
55
- Speed up Thrift response handling: read replies in 1 MiB chunks and decode them with Thrift's C-accelerated protocol, and stop stringifying every request/response for the default no-op debug logger. Wide result schemas, metadata calls with many rows, and inline Arrow results benefit most.
66
- Lower the peak memory of `fetchall()` and large `fetchmany()` calls by converting Arrow results to rows 100,000 rows at a time instead of building one DataFrame for the whole result, and build each row with one Python call instead of three, which makes these calls about 10% faster.
7-
- Speed up `fetchall()` and large `fetchmany()` calls on the default (pandas) conversion path by about another 30%: Python's cyclic garbage collector is paused while each batch of up to 100,000 rows is built (only if it is enabled; it is re-enabled as soon as no batch is being built, so between batches unless conversions overlap).
7+
- Speed up `fetchall()` and large `fetchmany()` calls on the default (pandas) conversion path by about another 30%: Python's cyclic garbage collector is paused while each batch of up to 100,000 rows is built (only if it is enabled; it is re-enabled as soon as no batch is being built, so between batches unless conversions overlap). If the application calls `gc.disable()` while a batch is being built, the GC is re-enabled when that pause ends; a GC that is already disabled when `fetchall()` runs is left alone.
88

99
# 4.9.0 (2026-11-04)
1010
- Upgrade Databricks SQL Kernel to 1.4.0, which keeps Lakehouse Real-Time session affinity and flushes shared telemetry only after the final session closes.

‎src/databricks/sql/result_set.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def __enter__(self) -> None:
9999
_GCPause.started = now
100100
elif now - _GCPause.started > _GC_PAUSE_MAX_SECONDS:
101101
return
102+
# No call between these two lines, so no async exception can
103+
# split them.
102104
_GCPause.active += 1
103105
self._joined = True
104106
gc.disable()
@@ -110,8 +112,9 @@ def __enter__(self) -> None:
110112
def __exit__(self, *exc_info) -> None:
111113
if not self._joined:
112114
return
113-
self._joined = False
114115
with _GCPause.lock:
116+
# No call between these two lines, so no async exception can split them.
117+
self._joined = False
115118
_GCPause.active -= 1
116119
if _GCPause.active:
117120
return

‎tests/unit/test_pandas_compatibility.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,21 @@ def disable_then_interrupt():
704704
self.assertFalse(gc.isenabled())
705705
self.assertTrue(gc.isenabled())
706706

707+
def test_interrupt_while_ending_a_pause_keeps_state_consistent(self):
708+
real_enable = gc.enable
709+
710+
def enable_then_interrupt():
711+
real_enable()
712+
raise KeyboardInterrupt
713+
714+
pause = result_set._GCPause(10**6)
715+
pause.__enter__()
716+
with patch.object(result_set.gc, "enable", enable_then_interrupt):
717+
with self.assertRaises(KeyboardInterrupt):
718+
pause.__exit__(None, None, None)
719+
self.assertTrue(gc.isenabled())
720+
self.assertEqual(result_set._GCPause.active, 0)
721+
707722
def test_gc_is_reenabled_when_building_fails(self):
708723
with self.assertRaises(ZeroDivisionError):
709724
with result_set._GCPause(10**6):

0 commit comments

Comments
 (0)