Skip to content

Commit 5b97619

Browse files
rustyconoverclaude
andcommitted
table_function: enforce TState serializability unconditionally
The TState-must-extend-ArrowSerializableDataclass check in __init_subclass__ was nested inside the FunctionArguments auto-extraction branch, so any class that set FunctionArguments explicitly in its body skipped TState validation entirely. That let non-serializable state slip through. It appears to work on subprocess transport (the worker process is long-lived, so the live state object survives between process() ticks) but breaks on HTTP, where each tick is an independent request and state must round-trip through the stream-state token. A stateless worker that never advances turns into an unbounded producer-exchange loop. The projection_repro fixture's _ChunkedState was exactly this case: a plain @DataClass(slots=True) used as TState, never validated because the fixture sets FunctionArguments explicitly. Make it extend ArrowSerializableDataclass so the framework threads it through the stream-state token, and hoist the validation out of the hasattr guard so it runs for every TableFunctionGenerator subclass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6a8d97c commit 5b97619

2 files changed

Lines changed: 44 additions & 18 deletions

File tree

‎vgi/_test_fixtures/projection_repro/worker.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from typing import Annotated, Any, ClassVar
3737

3838
import pyarrow as pa
39+
from vgi_rpc import ArrowSerializableDataclass
3940
from vgi_rpc.rpc import OutputCollector
4041

4142
from vgi import Worker
@@ -202,8 +203,18 @@ def process(
202203
# ---------------------------------------------------------------------------
203204

204205

205-
@dataclass(slots=True)
206-
class _ChunkedState:
206+
@dataclass(kw_only=True)
207+
class _ChunkedState(ArrowSerializableDataclass):
208+
"""Cross-tick progress for the multi-tick reproducer functions.
209+
210+
Must extend ``ArrowSerializableDataclass`` so the framework can
211+
serialize it into the stream-state token — without that, HTTP
212+
transport (where each ``process()`` tick is an independent request)
213+
restarts from ``initial_state()`` every exchange and the producer
214+
loop never terminates. Subprocess transport happens to keep the live
215+
object around between ticks, which masked the missing contract.
216+
"""
217+
207218
emitted: int = 0
208219

209220

‎vgi/table_function.py‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,37 @@ def __init_subclass__(cls) -> None:
381381
"""Validate FunctionArguments, auto-extracting from generic parameter if needed."""
382382
super().__init_subclass__()
383383

384+
# Validate TState (second generic type parameter) is serializable.
385+
#
386+
# This runs unconditionally — independently of the FunctionArguments
387+
# auto-extraction below. The check used to be nested inside the
388+
# ``not hasattr(cls, "FunctionArguments")`` branch, so any class that
389+
# set ``FunctionArguments`` explicitly in its body silently skipped
390+
# TState validation. That let non-serializable state slip through: it
391+
# appears to work on subprocess transport (the worker process is
392+
# long-lived, so the live state object survives between ``process()``
393+
# ticks) but breaks on HTTP, where each tick is an independent request
394+
# and state must round-trip through the stream-state token.
395+
for base in cls.__dict__.get("__orig_bases__", ()):
396+
origin = get_origin(base)
397+
if origin is not None and issubclass(origin, TableFunctionBase):
398+
type_args = get_args(base)
399+
if len(type_args) >= 2:
400+
state_type = type_args[1]
401+
if (
402+
state_type is not None
403+
and state_type is not type(None)
404+
and not isinstance(state_type, TypeVar)
405+
and isinstance(state_type, type)
406+
and not issubclass(state_type, ArrowSerializableDataclass)
407+
):
408+
raise TypeError(
409+
f"{cls.__name__}: TState type {state_type.__name__} must extend "
410+
f"ArrowSerializableDataclass for HTTP state serialization. "
411+
f"Use @dataclass(kw_only=True) and inherit from ArrowSerializableDataclass."
412+
)
413+
break
414+
384415
# Auto-extract FunctionArguments from generic type parameter if not explicitly set.
385416
# e.g., class MyFunc(TableFunctionGenerator[MyArgs]) -> cls.FunctionArguments = MyArgs
386417
if not hasattr(cls, "FunctionArguments"):
@@ -396,22 +427,6 @@ def __init_subclass__(cls) -> None:
396427
cls.FunctionArguments = make_dataclass(f"_{cls.__name__}Args", [])
397428
else:
398429
cls.FunctionArguments = type_args[0]
399-
400-
# Validate TState (second type parameter) is serializable
401-
if len(type_args) >= 2:
402-
state_type = type_args[1]
403-
if (
404-
state_type is not None
405-
and state_type is not type(None)
406-
and not isinstance(state_type, TypeVar)
407-
and isinstance(state_type, type)
408-
and not issubclass(state_type, ArrowSerializableDataclass)
409-
):
410-
raise TypeError(
411-
f"{cls.__name__}: TState type {state_type.__name__} must extend "
412-
f"ArrowSerializableDataclass for HTTP state serialization. "
413-
f"Use @dataclass(kw_only=True) and inherit from ArrowSerializableDataclass."
414-
)
415430
break
416431

417432
# Skip validation for abstract base classes

0 commit comments

Comments
 (0)