Skip to content

Commit ef58abe

Browse files
vuanhphungIsaac
andcommitted
fix: undo a half-joined GC pause when __enter__ is interrupted
An async exception (e.g. KeyboardInterrupt) arriving after a batch joined the pause but before __enter__ returned left the pause count at 1, and the GC disabled, for the rest of the process, since __exit__ never runs for a failed __enter__. Roll the join back in that case. The CHANGELOG now says the GC is re-enabled as soon as no batch is being built. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 056f35a commit ef58abe

3 files changed

Lines changed: 35 additions & 11 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 between batches).
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).
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: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,27 @@ def __init__(self, num_rows: int):
9090
def __enter__(self) -> None:
9191
if self._num_rows < _GC_PAUSE_MIN_ROWS:
9292
return
93-
with _GCPause.lock:
94-
now = time.monotonic()
95-
if _GCPause.active == 0:
96-
if not gc.isenabled():
93+
try:
94+
with _GCPause.lock:
95+
now = time.monotonic()
96+
if _GCPause.active == 0:
97+
if not gc.isenabled():
98+
return
99+
_GCPause.started = now
100+
elif now - _GCPause.started > _GC_PAUSE_MAX_SECONDS:
97101
return
98-
_GCPause.started = now
99-
elif now - _GCPause.started > _GC_PAUSE_MAX_SECONDS:
100-
return
101-
_GCPause.active += 1
102-
self._joined = True
103-
gc.disable()
102+
_GCPause.active += 1
103+
self._joined = True
104+
gc.disable()
105+
except BaseException:
106+
# E.g. a KeyboardInterrupt after joining: __exit__ won't be called.
107+
self.__exit__(None, None, None)
108+
raise
104109

105110
def __exit__(self, *exc_info) -> None:
106111
if not self._joined:
107112
return
113+
self._joined = False
108114
with _GCPause.lock:
109115
_GCPause.active -= 1
110116
if _GCPause.active:

‎tests/unit/test_pandas_compatibility.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,24 @@ def test_gc_disabled_by_the_caller_stays_disabled(self):
686686
self.assertEqual(self._convert(), (0, 0))
687687
self.assertFalse(gc.isenabled())
688688

689+
def test_interrupt_while_joining_a_pause_is_undone(self):
690+
real_disable = gc.disable
691+
692+
def disable_then_interrupt():
693+
real_disable()
694+
raise KeyboardInterrupt
695+
696+
with patch.object(result_set.gc, "disable", disable_then_interrupt):
697+
with self.assertRaises(KeyboardInterrupt):
698+
with result_set._GCPause(10**6):
699+
self.fail("the batch should not start")
700+
self.assertTrue(gc.isenabled())
701+
self.assertEqual(result_set._GCPause.active, 0)
702+
# Later pauses still work.
703+
with result_set._GCPause(10**6):
704+
self.assertFalse(gc.isenabled())
705+
self.assertTrue(gc.isenabled())
706+
689707
def test_gc_is_reenabled_when_building_fails(self):
690708
with self.assertRaises(ZeroDivisionError):
691709
with result_set._GCPause(10**6):

0 commit comments

Comments
 (0)