Skip to content

Commit 6b4fabc

Browse files
committed
fix(client): spawn workers without a shell, so force actually kills them
stop(force=True) killed the wrong process. Direct subprocess workers were spawned with shell=True, which makes proc the *shell*: SIGKILL took out the shell and left the worker holding the pipe, so a forced stop still waited the full length of whatever the worker was doing (CI: 5.18s against a 5s sleep on Linux). On Windows it killed cmd.exe and reported exit code 1 while the worker ran on. Split the command ourselves instead, as the pooled and catalog paths already did -- proc is then the worker on every platform. The two broken-pipe tests that relied on the shell's `exit` builtin now spawn a real process; the two catalog-path ones did too, though those had been passing for the wrong reason (that path never used a shell). Also assert the killed exit code per-platform: Windows has no signals, so TerminateProcess sets an ordinary non-zero code, not a negative one. Fix two docstring/doc cross-references that mkdocs --strict rejected.
1 parent dc8cf39 commit 6b4fabc

6 files changed

Lines changed: 39 additions & 18 deletions

File tree

‎docs/column-statistics.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Cache TTL is carried as IPC batch `custom_metadata` with key `cache_max_age_seco
182182

183183
## Reading Statistics From a Client
184184

185-
DuckDB is not the only caller. A pure-Python [`Client`][] reads the same statistics with
185+
DuckDB is not the only caller. A pure-Python [`Client`][vgi.client.Client] reads the same statistics with
186186
`table_column_statistics`, which issues the RPC and decodes the sparse-union batch for you:
187187

188188
```python test="skip"

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "vgi-python"
3-
version = "0.27.0"
3+
version = "0.28.0"
44
description = "Vector Gateway Interface - Connect DuckDB to external programs via Apache Arrow"
55
readme = "README.md"
66
keywords = [

‎tests/client/test_broken_pipe.py‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
from __future__ import annotations
1111

12+
import shlex
13+
import sys
14+
1215
import pyarrow as pa
1316
import pytest
1417

@@ -17,6 +20,15 @@
1720
from vgi.client.client import Client, ClientError
1821

1922

23+
def _exits_with(code: int) -> str:
24+
"""A worker command that exits immediately with ``code``, reading no input.
25+
26+
Spelled as a real process rather than the shell's ``exit`` builtin: workers are
27+
spawned without a shell, so a builtin would not resolve.
28+
"""
29+
return f"{shlex.quote(sys.executable)} -c {shlex.quote(f'raise SystemExit({code})')}"
30+
31+
2032
def _make_test_batch() -> pa.RecordBatch:
2133
"""Create a simple test batch for invoking functions."""
2234
return pa.RecordBatch.from_pydict({"x": [1, 2, 3]})
@@ -27,9 +39,9 @@ class TestClientWorkerExitHandling:
2739

2840
def test_client_raises_error_when_worker_exits_immediately(self) -> None:
2941
"""Client raises ClientError when worker exits before accepting input."""
30-
# Use 'exit 1' which immediately terminates without reading stdin
31-
# pool=None required because 'exit 1' is a shell command, not a binary
32-
client = Client("exit 1", pool=None)
42+
# A worker that terminates without ever reading stdin.
43+
# pool=None: the pool is for real, reusable workers.
44+
client = Client(_exits_with(1), pool=None)
3345

3446
# Worker exits before sending any output - detected either via BrokenPipeError
3547
# or via early exit check when reading fails
@@ -48,9 +60,8 @@ def test_client_raises_error_when_worker_exits_immediately(self) -> None:
4860

4961
def test_client_raises_error_with_useful_message(self) -> None:
5062
"""Client error message includes useful debugging information."""
51-
# Use a specific exit code
52-
# pool=None required because 'exit 42' is a shell command, not a binary
53-
client = Client("exit 42", pool=None)
63+
# Use a specific exit code so the message can be checked for it.
64+
client = Client(_exits_with(42), pool=None)
5465

5566
try:
5667
client.start()
@@ -84,8 +95,8 @@ class TestCatalogMixinWorkerExitHandling:
8495

8596
def test_catalog_invoke_raises_error_when_worker_exits(self) -> None:
8697
"""Catalog invoke raises CatalogClientError when worker exits."""
87-
# Use 'exit 1' which immediately terminates without reading stdin
88-
client = Client("exit 1")
98+
# A worker that terminates without ever reading stdin.
99+
client = Client(_exits_with(1))
89100

90101
# Worker exits - detected either via BrokenPipeError (shows "terminated
91102
# unexpectedly") or via read failure (shows "Failed to read catalog result")
@@ -94,7 +105,7 @@ def test_catalog_invoke_raises_error_when_worker_exits(self) -> None:
94105

95106
def test_catalog_invoke_error_includes_useful_info(self) -> None:
96107
"""Catalog error message includes useful debugging information."""
97-
client = Client("exit 42")
108+
client = Client(_exits_with(42))
98109

99110
try:
100111
list(client.catalogs())

‎tests/client/test_stop_force.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from __future__ import annotations
1717

18+
import sys
1819
import time
1920
from typing import Any
2021

@@ -44,7 +45,11 @@ def test_force_kills_a_direct_subprocess(tmp_path: Any) -> None:
4445

4546
returncode = client.stop(force=True)
4647
assert returncode is not None
47-
assert returncode < 0, "a killed process reports a negative (signal) exit code"
48+
if sys.platform == "win32":
49+
# Windows has no signals: TerminateProcess sets an ordinary non-zero code.
50+
assert returncode != 0, "a killed process should not report a clean exit"
51+
else:
52+
assert returncode < 0, "a killed process reports a negative (signal) exit code"
4853
assert proc.poll() is not None, "worker process should be reaped"
4954

5055

‎vgi/catalog/catalog_interface.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,8 @@ def deserialize_column_statistics(data: bytes) -> list[ColumnStatistics]:
14391439
14401440
Reads the sparse-union wire batch back into typed [`ColumnStatistics`][], so a client
14411441
can consume the bytes returned by ``catalog_table_column_statistics_get`` without
1442-
re-implementing the union layout. See [`Client.table_column_statistics`][] for the
1442+
re-implementing the union layout. See
1443+
[`Client.table_column_statistics`][vgi.client.CatalogClientMixin.table_column_statistics] for the
14431444
wrapper that fetches and decodes in one call.
14441445
14451446
The optional ``cache_max_age_seconds`` travels in the IPC batch's custom metadata

‎vgi/client/client.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,8 @@ def _spawn_subprocess_connection(self, worker_index: int) -> WorkerConnection:
718718
When a pool is configured, borrows an idle worker (or spawns a new
719719
one) from the pool. Otherwise creates a subprocess directly.
720720
721-
Python-specific: subprocess management relies on ``shell=True``
722-
semantics and the ``WorkerPool`` abstraction that other languages
723-
don't need to mirror.
721+
Python-specific: subprocess management relies on the ``WorkerPool``
722+
abstraction that other languages don't need to mirror.
724723
"""
725724
if self._pool is not None:
726725
_logger.debug("borrowing_worker worker_index=%s", worker_index)
@@ -739,14 +738,19 @@ def _spawn_subprocess_connection(self, worker_index: int) -> WorkerConnection:
739738
)
740739

741740
_logger.debug("spawning_worker worker_index=%s", worker_index)
741+
# Not shell=True: under a shell, proc is the *shell*, so proc.kill()
742+
# (stop(force=True)) kills the shell and leaves the worker holding the
743+
# pipe -- on Windows it kills cmd.exe and reports exit code 1 while the
744+
# worker runs on. Splitting the command ourselves makes proc the worker
745+
# on every platform, and matches how the pooled and catalog paths have
746+
# always spawned it.
742747
proc = subprocess.Popen(
743-
self.server_path,
748+
shlex.split(self.server_path, posix=sys.platform != "win32"),
744749
stdin=subprocess.PIPE,
745750
stdout=subprocess.PIPE,
746751
stderr=None if self.passthrough_stderr else subprocess.PIPE,
747752
text=False,
748753
bufsize=0,
749-
shell=True,
750754
)
751755
_logger.debug("worker_spawned worker_index=%s pid=%s", worker_index, proc.pid)
752756

0 commit comments

Comments
 (0)