#!/usr/bin/env python3 """Integration regression: real make/embedded PG/E2E runner, single-PID timeout. Pass --wrapper-dir to use QA's exact node wrapper. Without it, Playwright is blocked before execution, after the real Vite server has started. """ import argparse import importlib.util import json import os from pathlib import Path import shlex import shutil import signal import subprocess import sys import tempfile import time sys.dont_write_bytecode = True ROOT = Path(__file__).resolve().parents[2] spec = importlib.util.spec_from_file_location("supervisor", Path(__file__).with_name("e2e-supervisor.py")) supervision = importlib.util.module_from_spec(spec) spec.loader.exec_module(supervision) def run(wrapper_dir): base = Path(os.environ.get("TMPDIR") or os.environ.get("IDEA_MANAGED_TMP") or "/tmp") with tempfile.TemporaryDirectory(prefix="ticket12-timeout-integration.", dir=base) as temporary: evidence = Path(temporary) if wrapper_dir: wrapper = Path(wrapper_dir).resolve() if not (wrapper / "node").is_file(): raise RuntimeError("QA wrapper node is absent; exact reproduction refused") else: wrapper = evidence / "wrapper" wrapper.mkdir() node = shutil.which("node") (wrapper / "node").write_text( '#!/bin/bash\nif [[ "$*" == *playwright* ]]; then\n' ' exec sleep 60\nelse\n exec ' + shlex.quote(node) + ' "$@"\nfi\n' ) (wrapper / "node").chmod(0o755) command = ["timeout", "--foreground", "--preserve-status", "--signal=INT", "--kill-after=8s", "8s", "make", "test-e2e"] known = {} workspace = None process = None watcher = None names = set() real_ready = False try: with (evidence / "runner.log").open("w") as log: process = subprocess.Popen(command, cwd=ROOT, env=dict(os.environ, PATH=str(wrapper) + ":" + os.environ["PATH"]), stdout=log, stderr=log) deadline = time.monotonic() + 35 while process.poll() is None and time.monotonic() < deadline: if workspace is None: for metadata in base.glob("tradarr-e2e.*/supervisor.json"): try: info = json.loads(metadata.read_text()) owner = supervision.process(info["owner"]) if owner is not None and owner[0] == process.pid: workspace = metadata.parent watcher = info["watcher"] identity = supervision.process(watcher) if identity: known[watcher] = identity break except (OSError, ValueError, KeyError): pass if watcher: known.update(supervision.descendants(watcher)) if workspace is not None and (workspace / "stack.json").exists(): real_ready = True for pid in known: try: names.add(Path(f"/proc/{pid}/comm").read_text().strip()) except OSError: pass time.sleep(.03) code = process.wait(timeout=2) if workspace is None: raise RuntimeError("real runner did not create a supervised workspace") if not real_ready or not {"postgres", "e2e-stack", "sleep"}.issubset(names): raise RuntimeError(f"real stack incomplete: ready={real_ready}, observed process names={sorted(names)}") # This assertion is deliberately immediate. Waiting after timeout # returns keeps the test supervisor alive and masked the QA leak. if watcher: known.update(supervision.descendants(watcher)) survivors = [pid for pid, identity in known.items() if supervision.same_process(pid, identity)] if survivors or workspace.exists(): raise RuntimeError(f"timeout returned before cleanup: {len(survivors)} survivors; workspace remains={workspace.exists()}") if code not in (130, 2): raise RuntimeError(f"runner failed to clean during timeout grace period: exit={code}") print(f"PASS real runner + exact timeout flags: exit={code}, observed={len(known)}, no survivor/workspace when timeout returns") except Exception: print((evidence / "runner.log").read_text()[-5000:] if (evidence / "runner.log").exists() else "", file=sys.stderr) raise finally: # Independent fallback so a red regression does not itself leak. if watcher: known.update(supervision.descendants(watcher)) for pid, identity in known.items(): if supervision.same_process(pid, identity): try: os.kill(pid, signal.SIGKILL) except ProcessLookupError: pass if process is not None and process.poll() is None: process.kill() process.wait() if workspace is not None and workspace.exists(): shutil.rmtree(workspace) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--wrapper-dir") args = parser.parse_args() try: run(args.wrapper_dir) except Exception as error: print(f"FAIL E2E timeout integration: {error}", file=sys.stderr) sys.exit(1)