Skip to content

Commit 19b3b5c

Browse files
rustyconoverclaude
andcommitted
buffered_table_destructor RPC: best-effort end-of-query cleanup (review §2)
Adds a fourth buffered_table_* RPC. The C++ Sink+Source operator fires it from its GlobalSinkState destructor regardless of success / cancel / throw. The worker handler: * Pops any in-flight finalize iterators for execution_id (process-local generator stack frames; no FunctionStorage equivalent). * Calls storage.execution_clear() to wipe function_state and function_state_log rows for the scope. Idempotent: missing entries are not errors. Storage cleanup wrapped in try/except: failures are logged but don't propagate (we're in a destructor on the worker side and called from a destructor on the C++ side; throwing here would mask the original error). Backstop for missed deliveries is FunctionStorage's cleanup_old_entries(max_age_days=1). Protocol additions: * BufferedTableDestructorRequest{function_name, execution_id, attach_opaque_data} * BufferedTableDestructorResponse (empty ack) Also: register the new method in MetaWorker (otherwise the meta dispatch fails the protocol-implementation check at startup) and in the conformance allowlist as NotExposed (DuckDB-only, mirrors the aggregate_destructor entry). C++ codegen + wiring lands in the vgi repo in the next commit. Tests: all 27 table_in_out integration tests pass. vgi-python suite: 31 failed / 1651 passed — baseline failure count unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent de3cb06 commit 19b3b5c

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎tests/conformance/test_protocol_inventory.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ class NotExposed:
134134
"integration/aggregate/* and tests/test_aggregate_function.py."
135135
),
136136
),
137+
"buffered_table_destructor": NotExposed(
138+
reason=(
139+
"DuckDB-only. The C++ Sink+Source operator fires this from its "
140+
"destructor; non-DuckDB clients have no equivalent. Covered by C++ "
141+
"integration/table_in_out/buffered_*.test."
142+
),
143+
),
137144
# ---------- Catalog lifecycle / transactions ----------
138145
"catalog_catalogs": ("catalogs",),
139146
"catalog_attach": ("catalog_attach",),

‎vgi/meta_worker.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,10 @@ def buffered_table_finalize(self, request: Any, ctx: CallContext) -> Any:
589589
"""Dispatch buffered_table_finalize to the right worker."""
590590
return self._dispatch_aggregate(request, "buffered_table_finalize", ctx)
591591

592+
def buffered_table_destructor(self, request: Any, ctx: CallContext) -> Any:
593+
"""Dispatch buffered_table_destructor to the right worker."""
594+
return self._dispatch_aggregate(request, "buffered_table_destructor", ctx)
595+
592596
# ========== Serve entry point ==========
593597

594598
@classmethod

‎vgi/protocol.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,30 @@ class BufferedTableFinalizeResponse(ArrowSerializableDataclass):
14041404
has_more: bool
14051405

14061406

1407+
@dataclass(frozen=True, slots=True, kw_only=True)
1408+
class BufferedTableDestructorRequest(ArrowSerializableDataclass):
1409+
"""Request for buffered_table_destructor — best-effort end-of-query cleanup.
1410+
1411+
Fired by the C++ Sink+Source operator from its destructor regardless
1412+
of success / cancel / throw. Idempotent on the worker side: missing
1413+
entries are not errors. Delivery is best-effort — worker death or
1414+
network drop can lose the RPC; FunctionStorage's
1415+
``cleanup_old_entries(max_age_days=1)`` is the backstop for missed
1416+
deliveries.
1417+
"""
1418+
1419+
function_name: str
1420+
execution_id: bytes
1421+
attach_opaque_data: bytes | None = None
1422+
1423+
1424+
@dataclass(frozen=True, slots=True, kw_only=True)
1425+
class BufferedTableDestructorResponse(ArrowSerializableDataclass):
1426+
"""Response from buffered_table_destructor — empty ack."""
1427+
1428+
pass
1429+
1430+
14071431
# ---------------------------------------------------------------------------
14081432
# Aggregate Window Function RPC Types
14091433
# ---------------------------------------------------------------------------
@@ -1680,6 +1704,10 @@ def buffered_table_finalize(self, request: BufferedTableFinalizeRequest) -> Buff
16801704
"""Pull one batch from a finalize_state_id's generator."""
16811705
...
16821706

1707+
def buffered_table_destructor(self, request: BufferedTableDestructorRequest) -> BufferedTableDestructorResponse:
1708+
"""Best-effort end-of-query cleanup of buffered_table state. Must not raise."""
1709+
...
1710+
16831711
# ========== Aggregate Window Function Methods (optional, all unary) ==========
16841712

16851713
def aggregate_window_init(self, request: AggregateWindowInitRequest) -> AggregateWindowInitResponse:

‎vgi/worker.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,55 @@ def buffered_table_finalize(
26172617
writer.write_batch(batch)
26182618
return BufferedTableFinalizeResponse(output_batch=sink.getvalue().to_pybytes(), has_more=has_more)
26192619

2620+
def buffered_table_destructor(
2621+
self,
2622+
request: Any, # BufferedTableDestructorRequest — lazy import below
2623+
ctx: CallContext,
2624+
) -> Any: # BufferedTableDestructorResponse
2625+
"""Best-effort end-of-query cleanup.
2626+
2627+
The C++ Sink+Source operator fires this from its destructor
2628+
regardless of success / cancel / throw. Idempotent: missing
2629+
entries are not errors. Delivery is best-effort — worker death
2630+
or network drop can lose the RPC; FunctionStorage's
2631+
``cleanup_old_entries(max_age_days=1)`` is the backstop for
2632+
missed deliveries.
2633+
"""
2634+
from vgi.protocol import BufferedTableDestructorResponse
2635+
2636+
# Drop any leftover finalize iterators for this execution_id.
2637+
# These hold generator stack frames (process-local by nature) so
2638+
# they have no FunctionStorage equivalent — popping the dict is
2639+
# the only cleanup possible. In steady state finalize iterators
2640+
# self-clean on StopIteration; this branch only matches on
2641+
# mid-drain cancellation paths.
2642+
leftover = [k for k in _buffered_table_finalize_iters
2643+
if k[0] == request.execution_id]
2644+
for k in leftover:
2645+
_buffered_table_finalize_iters.pop(k, None)
2646+
2647+
# Wipe FunctionStorage rows: the b"buf_init" metadata, the
2648+
# b"buf" per-state-id RMW slots, and the b"buf" append log.
2649+
# execution_clear() sweeps every namespace for the scope in one
2650+
# call. Best-effort: failures here don't propagate (the operator
2651+
# is already torn down); cleanup_old_entries is the backstop.
2652+
try:
2653+
func_cls = self._resolve_function_by_name(
2654+
request.function_name,
2655+
self._unwrap_attach(request.attach_opaque_data),
2656+
function_type=TableInOutGenerator,
2657+
)
2658+
storage = BoundStorage(
2659+
func_cls.storage, request.execution_id, request=request, auth=ctx.auth,
2660+
)
2661+
storage.execution_clear()
2662+
except Exception:
2663+
_logger.exception(
2664+
"buffered_table_destructor: storage cleanup failed "
2665+
"(execution_id=%s)", request.execution_id.hex(),
2666+
)
2667+
return BufferedTableDestructorResponse()
2668+
26202669
# ========== Windowed Aggregate Methods ==========
26212670

26222671
def aggregate_window_init(

0 commit comments

Comments
 (0)