From 7b9ccbe3f7cb70d0e87430a1a7e0450fcb905d21 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:23:49 +0800 Subject: [PATCH 1/2] Read canonical test worker replies through portable pipes Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../canonical_authority_fixture.py | 97 ++++++++++--------- .../test_canonical_authority_fixture.py | 17 ++++ 2 files changed, 70 insertions(+), 44 deletions(-) diff --git a/tests/control_plane/canonical_authority_fixture.py b/tests/control_plane/canonical_authority_fixture.py index 57a7067c5d..723f88a75c 100644 --- a/tests/control_plane/canonical_authority_fixture.py +++ b/tests/control_plane/canonical_authority_fixture.py @@ -9,15 +9,15 @@ import atexit from collections import deque +from contextlib import suppress import json import os from datetime import datetime from pathlib import Path -import selectors +from queue import Empty, SimpleQueue import subprocess import tempfile import threading -import time import loopx @@ -34,7 +34,6 @@ class _CanonicalAuthorityWorker: def __init__(self, key: tuple[int, str, bool]) -> None: self.key = key self.call_lock = threading.Lock() - self._stderr_tail: deque[str] = deque(maxlen=64) self._process = self._spawn(Path(key[1])) @staticmethod @@ -80,50 +79,55 @@ def _build_script(package: Path) -> str: ) def _spawn(self, package: Path) -> subprocess.Popen[bytes]: - self._stderr_tail.clear() proc = subprocess.Popen( ["node", "--no-warnings", "--experimental-strip-types", "--input-type=module", "-e", self._build_script(package)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - bufsize=0, ) - assert proc.stderr is not None + stdout, stderr = proc.stdout, proc.stderr + assert stdout is not None and stderr is not None + responses: SimpleQueue[bytes | OSError] = SimpleQueue() + stderr_tail: deque[str] = deque(maxlen=64) + self._responses = responses + self._stderr_tail = stderr_tail + + # Windows select() accepts sockets only. Drain the anonymous pipes in + # readers owned by this child, and apply the deadline to the queue. + def _drain_stdout() -> None: + try: + for line in stdout: + if not line.endswith(b"\n"): + break + responses.put(line) + except OSError as exc: + responses.put(exc) + else: + responses.put(b"") def _drain_stderr() -> None: - try: - for raw_line in iter(proc.stderr.readline, b""): - self._stderr_tail.append(raw_line.decode("utf-8", errors="replace").rstrip()) - except OSError: - pass + with suppress(OSError): + for raw_line in stderr: + stderr_tail.append(raw_line.decode("utf-8", errors="replace").rstrip()) - threading.Thread(target=_drain_stderr, daemon=True).start() + self._readers = ( + threading.Thread(target=_drain_stdout, daemon=True), + threading.Thread(target=_drain_stderr, daemon=True), + ) + for reader in self._readers: + reader.start() return proc def _read_line(self, timeout_seconds: float) -> bytes: - stdout = self._process.stdout - assert stdout is not None - deadline = time.monotonic() + timeout_seconds - chunks = bytearray() - selector = selectors.DefaultSelector() - selector.register(stdout, selectors.EVENT_READ) try: - while True: - remaining = deadline - time.monotonic() - if remaining <= 0: - raise TimeoutError("canonical authority worker timed out waiting for response") - events = selector.select(remaining) - if not events: - raise TimeoutError("canonical authority worker timed out waiting for response") - chunk = os.read(stdout.fileno(), 65536) - if not chunk: - raise EOFError("canonical authority worker closed stdout") - chunks.extend(chunk) - if b"\n" in chunk: - line, _rest = bytes(chunks).split(b"\n", 1) - return line - finally: - selector.close() + response = self._responses.get(timeout=timeout_seconds) + except Empty: + raise TimeoutError("canonical authority worker timed out waiting for response") from None + if isinstance(response, OSError): + raise response + if not response: + raise EOFError("canonical authority worker closed stdout") + return response def request(self, payload: dict[str, object]) -> dict: encoded = (json.dumps(payload) + "\n").encode("utf-8") @@ -137,16 +141,16 @@ def request(self, payload: dict[str, object]) -> dict: self._process.stdin.write(encoded) self._process.stdin.flush() raw_line = self._read_line(_WORKER_REQUEST_TIMEOUT_SECONDS) - except (BrokenPipeError, EOFError, OSError): + except TimeoutError: + self.close() + raise + except (EOFError, OSError): self.close() if attempt == 0: self._process = self._spawn(Path(self.key[1])) continue stderr_text = "\n".join(self._stderr_tail) raise AssertionError(f"canonical authority worker failed after respawn: {stderr_text}") - except TimeoutError: - self.close() - raise response = json.loads(raw_line.decode("utf-8")) if not response.get("ok"): raise AssertionError(response.get("error") or "\n".join(self._stderr_tail)) @@ -155,12 +159,9 @@ def request(self, payload: dict[str, object]) -> dict: def close(self) -> None: proc = self._process - for stream in (proc.stdin, proc.stdout, proc.stderr): - if stream is not None: - try: - stream.close() - except OSError: - pass + if proc.stdin is not None: + with suppress(OSError): + proc.stdin.close() if proc.poll() is None: try: proc.wait(timeout=1.5) @@ -171,6 +172,14 @@ def close(self) -> None: except subprocess.TimeoutExpired: proc.kill() proc.wait() + # Exit closes the child's pipe ends so the readers can finish before + # this process closes its buffered streams or starts a new child. + for reader in self._readers: + reader.join() + for stream in (proc.stdout, proc.stderr): + if stream is not None: + with suppress(OSError): + stream.close() def _shutdown_canonical_fixture_workers() -> None: diff --git a/tests/control_plane/test_canonical_authority_fixture.py b/tests/control_plane/test_canonical_authority_fixture.py index 3eddb833f0..38f38cedd7 100644 --- a/tests/control_plane/test_canonical_authority_fixture.py +++ b/tests/control_plane/test_canonical_authority_fixture.py @@ -106,3 +106,20 @@ def test_persistent_canonical_worker_reuses_and_auto_respawns_once(tmp_path, mon assert second["status"] == "applied" assert worker._process.pid != original_pid assert worker._process.poll() is None + + +def test_canonical_worker_reaps_silent_child_without_retry(tmp_path, monkeypatch): + import canonical_authority_fixture as fixture_mod + + worker_type = fixture_mod._CanonicalAuthorityWorker + monkeypatch.setattr(fixture_mod, "_WORKER_REQUEST_TIMEOUT_SECONDS", 0.25) + monkeypatch.setattr(worker_type, "_build_script", staticmethod(lambda _: "process.stdin.resume();")) + worker = worker_type((os.getpid(), str(tmp_path), False)) + original_process = worker._process + try: + with pytest.raises(TimeoutError, match="timed out waiting for response"): + worker.request({"op": "silent"}) + assert worker._process is original_process + assert original_process.poll() is not None + finally: + worker.close() From dd1bd5c76234607f3e996b90f03b46fa20808941 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:05:32 +0800 Subject: [PATCH 2/2] Bound canonical fixture worker shutdown when pipes stay open Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../canonical_authority_fixture.py | 50 ++++++++-------- .../test_canonical_authority_fixture.py | 59 +++++++++++++++++++ 2 files changed, 86 insertions(+), 23 deletions(-) diff --git a/tests/control_plane/canonical_authority_fixture.py b/tests/control_plane/canonical_authority_fixture.py index 723f88a75c..0a053b8b1c 100644 --- a/tests/control_plane/canonical_authority_fixture.py +++ b/tests/control_plane/canonical_authority_fixture.py @@ -18,6 +18,7 @@ import subprocess import tempfile import threading +from time import monotonic import loopx @@ -26,6 +27,7 @@ _WORKERS: dict[tuple[int, str, bool], _CanonicalAuthorityWorker] = {} _SHUTDOWN_REGISTERED = False _WORKER_REQUEST_TIMEOUT_SECONDS = 45.0 +_WORKER_SHUTDOWN_TIMEOUT_SECONDS = 3.0 class _CanonicalAuthorityWorker: @@ -89,6 +91,7 @@ def _spawn(self, package: Path) -> subprocess.Popen[bytes]: assert stdout is not None and stderr is not None responses: SimpleQueue[bytes | OSError] = SimpleQueue() stderr_tail: deque[str] = deque(maxlen=64) + self._closed = False self._responses = responses self._stderr_tail = stderr_tail @@ -96,17 +99,18 @@ def _spawn(self, package: Path) -> subprocess.Popen[bytes]: # readers owned by this child, and apply the deadline to the queue. def _drain_stdout() -> None: try: - for line in stdout: - if not line.endswith(b"\n"): - break - responses.put(line) + with stdout: + for line in stdout: + if not line.endswith(b"\n"): + break + responses.put(line) except OSError as exc: responses.put(exc) else: responses.put(b"") def _drain_stderr() -> None: - with suppress(OSError): + with suppress(OSError), stderr: for raw_line in stderr: stderr_tail.append(raw_line.decode("utf-8", errors="replace").rstrip()) @@ -133,7 +137,7 @@ def request(self, payload: dict[str, object]) -> dict: encoded = (json.dumps(payload) + "\n").encode("utf-8") with self.call_lock: for attempt in range(2): - if self._process.poll() is not None: + if self._closed or self._process.poll() is not None: self.close() self._process = self._spawn(Path(self.key[1])) try: @@ -158,28 +162,28 @@ def request(self, payload: dict[str, object]) -> dict: raise AssertionError("unreachable") def close(self) -> None: + if self._closed: + return + self._closed = True proc = self._process + deadline = monotonic() + _WORKER_SHUTDOWN_TIMEOUT_SECONDS if proc.stdin is not None: with suppress(OSError): proc.stdin.close() - if proc.poll() is None: - try: - proc.wait(timeout=1.5) - except subprocess.TimeoutExpired: - proc.terminate() - try: - proc.wait(timeout=1.5) - except subprocess.TimeoutExpired: - proc.kill() - proc.wait() - # Exit closes the child's pipe ends so the readers can finish before - # this process closes its buffered streams or starts a new child. + grace = _WORKER_SHUTDOWN_TIMEOUT_SECONDS / 3 + for stop, wait_seconds in ((None, grace), (proc.terminate, grace), (proc.kill, _WORKER_SHUTDOWN_TIMEOUT_SECONDS)): + if proc.poll() is not None: + break + if stop is not None: + with suppress(ProcessLookupError): + stop() + with suppress(subprocess.TimeoutExpired): + proc.wait(timeout=min(wait_seconds, max(0.0, deadline - monotonic()))) + break + # A descendant may still hold the output pipes after the worker exits. + # Let each reader close its own stream when its pipe is released. for reader in self._readers: - reader.join() - for stream in (proc.stdout, proc.stderr): - if stream is not None: - with suppress(OSError): - stream.close() + reader.join(timeout=max(0.0, deadline - monotonic())) def _shutdown_canonical_fixture_workers() -> None: diff --git a/tests/control_plane/test_canonical_authority_fixture.py b/tests/control_plane/test_canonical_authority_fixture.py index 38f38cedd7..a794079b76 100644 --- a/tests/control_plane/test_canonical_authority_fixture.py +++ b/tests/control_plane/test_canonical_authority_fixture.py @@ -1,6 +1,8 @@ """Canonical CLI fixtures retain runtime isolation and actual backend identity.""" from __future__ import annotations +from concurrent.futures import ThreadPoolExecutor +import json import os from pathlib import Path import subprocess @@ -123,3 +125,60 @@ def test_canonical_worker_reaps_silent_child_without_retry(tmp_path, monkeypatch assert original_process.poll() is not None finally: worker.close() + + +def test_canonical_worker_timeout_returns_while_descendant_holds_output(tmp_path, monkeypatch): + import canonical_authority_fixture as fixture_mod + + release_file = tmp_path / "release-pipes" + holder_script = """ + const {existsSync} = require("node:fs"); + setInterval(() => { + if (existsSync(process.argv[1])) process.exit(0); + }, 20); + setTimeout(() => process.exit(0), 10000); + """ + worker_script = """ + import {spawn} from "node:child_process"; + import {createInterface} from "node:readline"; + for await (const line of createInterface({input: process.stdin})) { + if (JSON.parse(line).op === "exit-with-held-pipes") { + const holder = spawn(process.execPath, + ["--input-type=commonjs", "-e", HOLDER_SCRIPT, RELEASE_FILE], + {stdio: ["ignore", 1, 2]}); + holder.once("spawn", () => process.exit(0)); + } else { + console.log(JSON.stringify({ok: true, result: {pid: process.pid}})); + } + } + """.replace("HOLDER_SCRIPT", json.dumps(holder_script)).replace("RELEASE_FILE", json.dumps(str(release_file))) + worker_type = fixture_mod._CanonicalAuthorityWorker + monkeypatch.setattr(worker_type, "_build_script", staticmethod(lambda _: worker_script)) + monkeypatch.setattr(fixture_mod, "_WORKER_SHUTDOWN_TIMEOUT_SECONDS", 0.4, raising=False) + monkeypatch.setattr(fixture_mod, "_WORKER_REQUEST_TIMEOUT_SECONDS", 5.0) + worker = worker_type((os.getpid(), str(tmp_path), False)) + original = worker._process + old_readers = worker._readers + try: + assert worker.request({"op": "ready"}) == {"pid": original.pid} + with ThreadPoolExecutor(max_workers=1) as executor: + try: + monkeypatch.setattr(fixture_mod, "_WORKER_REQUEST_TIMEOUT_SECONDS", 0.5) + pending = executor.submit(worker.request, {"op": "exit-with-held-pipes"}) + assert original.wait(timeout=3) == 0 + with pytest.raises(TimeoutError, match="canonical authority worker timed out waiting for response"): + pending.result(timeout=3) + assert all(reader.is_alive() for reader in old_readers) + + monkeypatch.setattr(fixture_mod, "_WORKER_REQUEST_TIMEOUT_SECONDS", 5.0) + replacement = worker.request({"op": "ready"}) + assert replacement["pid"] != original.pid + finally: + release_file.touch() + for reader in old_readers: + reader.join(timeout=3) + assert all(not reader.is_alive() for reader in old_readers) + assert original.stdout is not None and original.stdout.closed + assert original.stderr is not None and original.stderr.closed + finally: + worker.close()