122 lines
5.7 KiB
Python
122 lines
5.7 KiB
Python
"""Bounded child-process supervision. No runner command comes from a policy file."""
|
|
from __future__ import annotations
|
|
|
|
import ctypes
|
|
from ctypes import wintypes
|
|
from dataclasses import dataclass
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import signal
|
|
import subprocess
|
|
import time
|
|
from typing import Sequence
|
|
|
|
|
|
class _WindowsJob:
|
|
"""Close a private job to kill the worker and any ADL/compiler descendants."""
|
|
def __init__(self, process):
|
|
class Basic(ctypes.Structure):
|
|
_fields_ = [("process_time", ctypes.c_longlong), ("job_time", ctypes.c_longlong),
|
|
("flags", wintypes.DWORD), ("minimum_working_set", ctypes.c_size_t),
|
|
("maximum_working_set", ctypes.c_size_t), ("active_processes", wintypes.DWORD),
|
|
("affinity", ctypes.c_size_t), ("priority", wintypes.DWORD),
|
|
("scheduling", wintypes.DWORD)]
|
|
|
|
class IO(ctypes.Structure):
|
|
_fields_ = [(name, ctypes.c_ulonglong) for name in (
|
|
"read_ops", "write_ops", "other_ops", "read_bytes", "write_bytes", "other_bytes")]
|
|
|
|
class Extended(ctypes.Structure):
|
|
_fields_ = [("basic", Basic), ("io", IO), ("process_memory", ctypes.c_size_t),
|
|
("job_memory", ctypes.c_size_t), ("peak_process_memory", ctypes.c_size_t),
|
|
("peak_job_memory", ctypes.c_size_t)]
|
|
|
|
self.kernel = ctypes.WinDLL("kernel32", use_last_error=True)
|
|
self.kernel.CreateJobObjectW.argtypes = [ctypes.c_void_p, wintypes.LPCWSTR]
|
|
self.kernel.CreateJobObjectW.restype = wintypes.HANDLE
|
|
self.kernel.SetInformationJobObject.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD]
|
|
self.kernel.SetInformationJobObject.restype = wintypes.BOOL
|
|
self.kernel.AssignProcessToJobObject.argtypes = [wintypes.HANDLE, wintypes.HANDLE]
|
|
self.kernel.AssignProcessToJobObject.restype = wintypes.BOOL
|
|
self.kernel.CloseHandle.argtypes = [wintypes.HANDLE]
|
|
self.kernel.CloseHandle.restype = wintypes.BOOL
|
|
self.handle = self.kernel.CreateJobObjectW(None, None)
|
|
if not self.handle:
|
|
raise ctypes.WinError(ctypes.get_last_error())
|
|
limits = Extended()
|
|
limits.basic.flags = 0x2000 # JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
|
|
if not self.kernel.SetInformationJobObject(self.handle, 9, ctypes.byref(limits), ctypes.sizeof(limits)):
|
|
code = ctypes.get_last_error(); self.close(); raise ctypes.WinError(code)
|
|
if not self.kernel.AssignProcessToJobObject(self.handle, wintypes.HANDLE(int(process._handle))):
|
|
code = ctypes.get_last_error(); self.close(); raise ctypes.WinError(code)
|
|
|
|
def close(self):
|
|
if self.handle:
|
|
self.kernel.CloseHandle(self.handle)
|
|
self.handle = None
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ProcessResult:
|
|
status: str
|
|
returncode: int | None
|
|
duration_seconds: float
|
|
stdout: str
|
|
stderr: str
|
|
|
|
|
|
def run_isolated(argv: Sequence[str], *, cwd: Path, timeout: float, cancel_event=None,
|
|
output_limit: int = 2 * 1024 * 1024) -> ProcessResult:
|
|
"""Use only with bundled fixed commands; never forward imported argv/config.
|
|
|
|
Separate cwd/logs, shell=False, no inherited stdin, no visible helper window,
|
|
a wall-clock deadline, and a kill-on-close process tree on Windows. This is
|
|
process/lifetime isolation, not an OS network sandbox.
|
|
"""
|
|
if isinstance(timeout, bool) or not isinstance(timeout, (float, int)) or not math.isfinite(timeout) or timeout <= 0:
|
|
raise ValueError("timeout must be a finite positive number")
|
|
if cancel_event is not None and cancel_event.is_set():
|
|
return ProcessResult("cancelled", None, 0.0, "", "")
|
|
cwd.mkdir(parents=True, exist_ok=True)
|
|
stdout_path, stderr_path = cwd / "worker.stdout.log", cwd / "worker.stderr.log"
|
|
started = time.monotonic()
|
|
process, job, status = None, None, "error"
|
|
with stdout_path.open("wb") as stdout, stderr_path.open("wb") as stderr:
|
|
try:
|
|
process = subprocess.Popen(list(argv), cwd=cwd, stdin=subprocess.DEVNULL, stdout=stdout,
|
|
stderr=stderr, shell=False, close_fds=True,
|
|
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
|
start_new_session=os.name != "nt")
|
|
if os.name == "nt":
|
|
job = _WindowsJob(process)
|
|
while process.poll() is None:
|
|
if cancel_event is not None and cancel_event.is_set():
|
|
status = "cancelled"; break
|
|
if time.monotonic() - started >= timeout:
|
|
status = "timeout"; break
|
|
if stdout_path.stat().st_size + stderr_path.stat().st_size > output_limit:
|
|
status = "output_limit"; break
|
|
time.sleep(.025)
|
|
else:
|
|
status = "completed" if process.returncode == 0 else "error"
|
|
except OSError as error:
|
|
stderr.write(str(error).encode("utf-8", errors="replace"))
|
|
finally:
|
|
if job is not None:
|
|
job.close()
|
|
elif process is not None and os.name != "nt":
|
|
try:
|
|
os.killpg(process.pid, signal.SIGKILL)
|
|
except ProcessLookupError:
|
|
pass
|
|
if process is not None:
|
|
if process.poll() is None:
|
|
process.kill()
|
|
process.wait(timeout=5)
|
|
def bounded(path):
|
|
with path.open("rb") as stream:
|
|
return stream.read(output_limit).decode("utf-8", errors="replace")
|
|
return ProcessResult(status, process.returncode if process is not None else None,
|
|
time.monotonic() - started, bounded(stdout_path), bounded(stderr_path))
|