"""Checked group termination for the erlang supervisor. Erlang runs this as its own process, so supervision keeps making progress when the kernel cannot (a native cell holding the GIL, a wedged interpreter), and so the supervisor never parses kill(1) error text or guesses an exit status. One argv carries the request (or `-` and one stdin line, the form run over ssh for a remote kernel's processes); one JSON object on stdout carries the verdict. """ from __future__ import annotations import signal import threading import asyncio import base64 import json import os import sys import albedo_proc import albedo_identity TERM_MS = 250 KILL_MS = 1000 # the supervisor's remaining budget is short: this is the last resort def budget(request: dict, name: str, default: int) -> float: """Seconds for one ladder step, clamped to something a caller can wait for.""" value = request.get(name, default) return ( min(max(value, 10), default) / 1000 if isinstance(value, int) else default / 1000 ) def target(spec: object) -> albedo_proc.Group: """One requested target: a process group, or one process that leads none.""" if not isinstance(spec, dict): raise ValueError("target must be an object") pgid = spec.get("pgid") operand = pgid if isinstance(pgid, int) else spec.get("pid") if not isinstance(operand, int) or operand <= 1: raise ValueError("target needs a pgid or pid above 1") leader = spec.get("leader") whole = isinstance(pgid, int) if not whole: try: whole = os.getpgid(operand) == operand except (ProcessLookupError, PermissionError): pass return albedo_proc.Group( operand, leader if isinstance(leader, str) else None, whole ) def jobs_targets(directory: object) -> list[dict[str, object]]: """The groups a jobs helper's live.json names: its own and each live job's. A directory without the file names none.""" if not isinstance(directory, str): raise ValueError("jobs needs a directory") try: with open(os.path.join(directory, "live.json"), encoding="utf-8") as file: live = json.load(file) except (OSError, ValueError): return [] named = [("jobs helper", live.get("helper"))] + [ (f"job {id}", spec) for id, spec in (live.get("jobs") or {}).items() ] return [ {**spec, "label": label} for label, spec in named if isinstance(spec, dict) and isinstance(spec.get("pgid"), int) ] def read_book(directory: object, job: object) -> dict[str, object]: """A jobs helper's finished-job book, for a daemon that cannot read this host's files itself: every entry without its stored tail (a listing stays small), or, given `job`, that entry whole.""" if not isinstance(directory, str): raise ValueError("book needs a directory") try: with open(os.path.join(directory, "book.json"), encoding="utf-8") as file: jobs = json.load(file)["jobs"] except (OSError, ValueError, KeyError): return {"jobs": []} if isinstance(job, str): return {"jobs": [entry for entry in jobs if entry.get("id") == job]} return {"jobs": [{**entry, "tail": None} for entry in jobs]} def read_range(path: object, offset: object, size: object) -> dict[str, object]: """Up to 64 KiB of a file from `offset`: a job's spilled output.""" if ( not isinstance(path, str) or not isinstance(offset, int) or not isinstance(size, int) ): raise ValueError("read needs a path, an offset and a size") with open(path, "rb") as file: file.seek(offset) data = file.read(min(max(size, 0), 65536)) return {"data": base64.b64encode(data).decode(), "size": len(data)} async def supervise_async(request: object) -> dict[str, object]: if not isinstance(request, dict): raise ValueError("request must be an object") if "discover" in request: home, known = request["discover"], request.get("known", []) if ( not isinstance(home, str) or not isinstance(known, list) or not all(isinstance(path, str) for path in known) ): raise ValueError("discovery needs a home and known run directories") return {"kernels": albedo_identity.discover(home, known)} if "book" in request: return {"file": read_book(request["book"], request.get("job"))} if "read" in request: return { "file": read_range( request["read"], request.get("offset"), request.get("size") ) } if "jobs" in request: request = {**request, "targets": jobs_targets(request["jobs"])} specs = request.get("targets") if not isinstance(specs, list): raise ValueError("request needs a target list") targets = [target(spec) for spec in specs] endings = await albedo_proc.terminate( targets, term=budget(request, "term_ms", TERM_MS), kill=budget(request, "kill_ms", KILL_MS), ) return { "targets": [ {**ending.as_json(), "label": spec.get("label")} for spec, ending in zip(specs, endings) ] } def supervise(request: object) -> dict[str, object]: return asyncio.run(supervise_async(request)) async def serve() -> None: """Answer one JSON request per stdin line until end of input. The daemon keeps one of these running, so a stop does not pay for an interpreter start. Requests run concurrently; each answer is one stdout line carrying the request's id. The first line says the interpreter is ready. """ loop = asyncio.get_running_loop() lines: asyncio.Queue[str | None] = asyncio.Queue() def read() -> None: for line in sys.stdin: loop.call_soon_threadsafe(lines.put_nowait, line) loop.call_soon_threadsafe(lines.put_nowait, None) threading.Thread(target=read, daemon=True).start() async def answer(line: str) -> None: identity = None try: message = json.loads(line) identity = message["id"] reply: dict[str, object] = { "ok": True, **await supervise_async(message["request"]), } except Exception as error: reply = {"ok": False, "error": f"{type(error).__name__}: {error}"} print(json.dumps({"id": identity, "answer": reply}), flush=True) print(json.dumps({"ready": True}), flush=True) running: set[asyncio.Task[None]] = set() while (line := await lines.get()) is not None: task = asyncio.create_task(answer(line)) running.add(task) task.add_done_callback(running.discard) await asyncio.gather(*running) def main(request: str) -> int: try: answer: dict[str, object] = {"ok": True, **supervise(json.loads(request))} status = 0 except Exception as error: answer = {"ok": False, "error": f"{type(error).__name__}: {error}"} status = 2 print(json.dumps(answer)) return status if __name__ == "__main__": if sys.argv[1:] == ["--serve"]: asyncio.run(serve()) sys.exit(0) # Default SIGALRM terminates even a wedged interpreter; no Python handler. # The owning port waits longer than this before reporting a helper timeout. signal.signal(signal.SIGALRM, signal.SIG_DFL) signal.alarm(3) request = sys.argv[1] if len(sys.argv) > 1 else "{}" sys.exit(main(sys.stdin.readline() if request == "-" else request))