Skip to content

Commit 5e38a2e

Browse files
rustyconoverclaude
andcommitted
worker: route serve through run_server so --unix is honoured
vgi-fixture-worker (and the four sibling fixture entry points built on the base Worker class) now participate in the AF_UNIX launcher contract: when invoked as `worker --unix PATH --idle-timeout SEC` the worker binds an AF_UNIX listener at PATH, prints UNIX:<abs_path>\n on stdout, and self-shuts-down after the requested idle period. Without those flags the workers default to the existing serve_stdio path, so subprocess transport is unaffected. Two integration points: - MetaWorker.serve in vgi/meta_worker.py now calls run_server(VgiProtocol, meta) instead of serve_stdio(server) — fixes vgi-fixture-worker, the meta-catalog fixture used by most integration tests - Worker.main in vgi/worker.py adds --unix PATH and --idle-timeout SEC to its typer command, plus a serve_unix branch with on_bound discovery-line emit — fixes vgi-fixture-versioned-worker, vgi-fixture-versioned-tables-worker, vgi-fixture-attach-options-worker, vgi-fixture-simple-writable-worker Both changes are back-compat: the default behaviour (stdio subprocess transport) is unchanged. --http and --unix are mutually exclusive; explicit error on misuse. Together with the C++ AF_UNIX launcher in the vgi extension, this lets ATTACH 'cat' AS x (TYPE vgi, LOCATION 'launch:uv run --project ... vgi-fixture-worker') spawn one warm Python interpreter and share it across every DuckDB process pointed at the same worker tuple. The existing make test_subprocess suite passes through the launcher path as make test_launcher in ~5x less wall-clock time (per-test profiling in vgi/docs/launcher-protocol.md). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent c50e58c commit 5e38a2e

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎uv.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vgi/meta_worker.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,24 @@ def aggregate_streaming_close(self, request: Any, ctx: CallContext) -> Any:
392392

393393
@classmethod
394394
def serve(cls, *worker_classes: type[Worker]) -> None:
395-
"""Instantiate workers and serve via vgi_rpc on stdin/stdout."""
395+
"""Instantiate workers and serve via vgi_rpc.
396+
397+
Defaults to stdin/stdout for the subprocess transport; passes
398+
argv through to ``run_server()`` so the worker also participates
399+
in the AF_UNIX launcher path when launched with
400+
``--unix PATH --idle-timeout SEC`` (the vgi C++ extension uses
401+
this to share warm workers across DuckDB processes).
402+
"""
396403
from vgi.protocol import VgiProtocol
404+
from vgi_rpc.rpc import run_server
397405

398406
# Log startup (some tests check that stderr has output)
399407
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(levelname)s %(message)s")
400408
logger.info("worker_starting")
401409

402410
workers = [wc() for wc in worker_classes]
403411
meta = cls(workers)
404-
server = RpcServer(VgiProtocol, meta)
405-
serve_stdio(server)
412+
run_server(VgiProtocol, meta)
406413

407414

408415
# Register all attach_id-based delegate methods on MetaWorker

‎vgi/worker.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,20 @@ def _run(
880880
"without parsing stdout."
881881
),
882882
),
883+
# AF_UNIX launcher contract — mutually exclusive with --http.
884+
# When --unix PATH is passed, the worker binds an AF_UNIX
885+
# socket, prints UNIX:<abs-path> to stdout, and self-shuts-down
886+
# after --idle-timeout seconds with zero connected clients.
887+
unix: str | None = typer.Option(
888+
None,
889+
"--unix",
890+
help="Bind to this AF_UNIX socket path instead of stdin/stdout (mutex with --http).",
891+
),
892+
idle_timeout: float = typer.Option(
893+
300.0,
894+
"--idle-timeout",
895+
help="Self-shutdown after N seconds idle when serving --unix.",
896+
),
883897
) -> None:
884898
env_debug = os.environ.get("VGI_WORKER_DEBUG", "").lower() in ("1", "true", "yes")
885899
effective_debug = debug or env_debug
@@ -890,6 +904,9 @@ def _run(
890904
log_format=log_format,
891905
)
892906

907+
if http and unix is not None:
908+
raise typer.BadParameter("--http and --unix are mutually exclusive")
909+
893910
if http:
894911
from vgi.serve import (
895912
_maybe_init_sentry,
@@ -914,6 +931,35 @@ def _run(
914931
otel_config=otel_config,
915932
port_file=port_file,
916933
)
934+
elif unix is not None:
935+
# AF_UNIX launcher path. Bind to the requested socket,
936+
# print UNIX:<abs_path> on stdout, idle-shutdown after
937+
# idle_timeout seconds.
938+
from vgi.serve import _maybe_init_sentry, _resolve_otel_config
939+
from vgi_rpc.rpc import serve_unix
940+
941+
_maybe_init_sentry()
942+
otel_config = _resolve_otel_config()
943+
worker = cls(quiet=quiet, log_level=effective_level)
944+
server = RpcServer(VgiProtocol, worker, server_version=_get_vgi_version())
945+
if otel_config is not None:
946+
from vgi_rpc.otel import instrument_server
947+
948+
instrument_server(server, otel_config)
949+
worker._vgi_tracer = VgiTracer.create(otel_config)
950+
abs_path = os.path.abspath(unix)
951+
effective_idle = idle_timeout if idle_timeout > 0 else None
952+
953+
def _emit(bound: str) -> None:
954+
print(f"UNIX:{bound}", flush=True)
955+
956+
serve_unix(
957+
server,
958+
abs_path,
959+
threaded=True,
960+
idle_timeout=effective_idle,
961+
on_bound=_emit,
962+
)
917963
else:
918964
from vgi.serve import _maybe_init_sentry, _resolve_otel_config
919965

0 commit comments

Comments
 (0)