mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Every wait for the recipe cleanup lock is capped at 60 s with stderr progress naming the lock and, where available, the holder PID; a launch check that times out is refused with exit 4 instead of hanging. cleanup_failure_first() closes resources through a yielded ExitStack and attaches any close error, including KeyboardInterrupt and SystemExit, as a note on a propagating cleanup failure. An end-to-end test pins that a raw OSError from the evaluator's normal-exit stop exits 4 with a marker. Fixes #652 Fixes #654
545 lines
22 KiB
Python
545 lines
22 KiB
Python
"""Own POSIX subprocess groups and defer cancellation during spawn and cleanup."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextlib import ExitStack, contextmanager, nullcontext
|
|
import logging
|
|
import math
|
|
import os
|
|
import selectors
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
_GRACE_ENV = "OBLITERATUS_PROCESS_GRACE_SECONDS"
|
|
_DRAIN_ENV = "OBLITERATUS_PROCESS_DRAIN_SECONDS"
|
|
_POLL_INTERVAL = 0.05
|
|
# Bound on waiting for the leader to exit after the final group signal. A
|
|
# leader in uninterruptible sleep cannot act on SIGKILL until it wakes.
|
|
_REAP_TIMEOUT = 10.0
|
|
# verify-edit owns at most two groups at once (the evaluator and a locally
|
|
# launched vLLM server) and stops them one after the other.
|
|
_NESTED_GROUPS = 2
|
|
# Margin for polling overshoot and interpreter work around the nested cleanups.
|
|
_NESTED_SLACK = 1.0
|
|
|
|
# Timed-out leaders, kept referenced so that Popen.__del__ never hands them to
|
|
# subprocess._active, where the next Popen() would reap them and release the
|
|
# PGID. Guarded by _UNREAPED_LOCK.
|
|
_UNREAPED: list[subprocess.Popen] = []
|
|
_UNREAPED_LOCK = threading.Lock()
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def process_grace() -> float:
|
|
"""Read the POSIX TERM-to-KILL timeout in seconds."""
|
|
return _process_seconds(_GRACE_ENV, "10")
|
|
|
|
|
|
def process_drain() -> float:
|
|
"""Read the POSIX post-cleanup pipe drain timeout in seconds (default 0.25)."""
|
|
return _process_seconds(_DRAIN_ENV, "0.25")
|
|
|
|
|
|
def _process_seconds(variable: str, default: str) -> float:
|
|
try:
|
|
seconds = float(os.environ.get(variable, default))
|
|
if math.isfinite(seconds) and seconds >= 0:
|
|
return seconds
|
|
except ValueError:
|
|
pass
|
|
raise ValueError(f"{variable} must be a finite, nonnegative number of seconds")
|
|
|
|
|
|
class ProcessInterrupted(SystemExit):
|
|
"""Exit with the conventional status for the signal (SIGTERM) that interrupted the run."""
|
|
|
|
def __init__(self, signum: int):
|
|
super().__init__(128 + signum)
|
|
self.signum = signum
|
|
|
|
def __str__(self) -> str:
|
|
return f"interrupted by {signal.Signals(self.signum).name}"
|
|
|
|
|
|
def _interruption(signum: int) -> BaseException:
|
|
if signum == signal.SIGINT:
|
|
# Exactly KeyboardInterrupt: CPython 3.12 exits an uncaught subclass
|
|
# with status 1 rather than by SIGINT.
|
|
interruption = KeyboardInterrupt(f"interrupted by {signal.Signals(signum).name}")
|
|
interruption.signum = signum
|
|
return interruption
|
|
return ProcessInterrupted(signum)
|
|
|
|
|
|
class ProcessSignals:
|
|
"""Defer POSIX cancellation outside interruptible waits.
|
|
|
|
Nested scopes share the first signal and deliver it once. During exception
|
|
unwinding, leave a pending signal for the enclosing owner to deliver after
|
|
cleanup. At the outermost exit, the interruption retains the original
|
|
error as exception context. SIGINT is delivered as a plain KeyboardInterrupt
|
|
(with a ``signum`` attribute) and SIGTERM as SystemExit (ProcessInterrupted).
|
|
"""
|
|
|
|
def __init__(self):
|
|
self._previous = {}
|
|
self._parent = None
|
|
self._owner = self
|
|
self._signum = None
|
|
self._delivered = False
|
|
self._armed = False
|
|
|
|
def __enter__(self):
|
|
self._parent = None
|
|
self._owner = self
|
|
self._signum = None
|
|
self._delivered = False
|
|
if os.name == "posix" and threading.current_thread() is threading.main_thread():
|
|
self._previous = {
|
|
signum: signal.getsignal(signum) for signum in (signal.SIGTERM, signal.SIGINT)
|
|
}
|
|
for handler in self._previous.values():
|
|
parent = getattr(handler, "__self__", None)
|
|
if isinstance(parent, ProcessSignals):
|
|
self._parent = parent
|
|
self._owner = parent._owner
|
|
break
|
|
for signum in self._previous:
|
|
signal.signal(signum, self._handle)
|
|
return self
|
|
|
|
def __exit__(self, _exc_type, error, _traceback):
|
|
self.disarm()
|
|
owner = self._owner
|
|
if error is not None and self._parent is not None and owner._signum is not None and not owner._delivered:
|
|
self._parent.disarm()
|
|
previous, self._previous = self._previous, {}
|
|
for signum, handler in previous.items():
|
|
if handler is None:
|
|
# getsignal() returns None for a handler installed outside
|
|
# Python, which signal.signal() cannot reinstall.
|
|
logger.warning(
|
|
"cannot restore the non-Python %s handler; restoring SIG_DFL",
|
|
signal.Signals(signum).name)
|
|
handler = signal.SIG_DFL
|
|
signal.signal(signum, handler)
|
|
if error is None or self._parent is None:
|
|
if isinstance(error, ProcessGroupCleanupError) and self._keep_cleanup_failure(error):
|
|
return
|
|
self.check()
|
|
|
|
def _keep_cleanup_failure(self, error: BaseException) -> bool:
|
|
# A group that may still be running outranks a deferred signal: deliver
|
|
# the signal as a note on the propagating cleanup failure instead.
|
|
owner = self._owner
|
|
if owner._signum is None or owner._delivered:
|
|
return False
|
|
owner._delivered = True
|
|
error.add_note(f"{_interruption(owner._signum)} during process group cleanup;"
|
|
" the cleanup failure takes priority")
|
|
return True
|
|
|
|
def _record(self, signum):
|
|
owner = self._owner
|
|
if owner._signum is None:
|
|
owner._signum = signum
|
|
|
|
def _handle(self, signum, _frame):
|
|
self._record(signum)
|
|
if self._armed:
|
|
self.check()
|
|
|
|
def check(self):
|
|
owner = self._owner
|
|
if owner._signum is not None and not owner._delivered:
|
|
self.disarm()
|
|
owner._delivered = True
|
|
raise _interruption(owner._signum)
|
|
|
|
def arm(self):
|
|
self._armed = True
|
|
self.check()
|
|
|
|
def disarm(self):
|
|
self._armed = False
|
|
|
|
@contextmanager
|
|
def interruptible(self):
|
|
try:
|
|
self.arm()
|
|
yield
|
|
finally:
|
|
self.disarm()
|
|
|
|
|
|
def _signal_group(pgid: int, signum: int) -> bool:
|
|
try:
|
|
os.killpg(pgid, signum)
|
|
except ProcessLookupError:
|
|
return False
|
|
return True
|
|
|
|
|
|
class ProcessGroupOwnershipError(RuntimeError):
|
|
"""The POSIX leader's PID cannot be reserved for group cleanup."""
|
|
|
|
|
|
class ProcessGroupCleanupError(RuntimeError):
|
|
"""The POSIX leader did not exit after the final group signal; it remains unreaped.
|
|
|
|
The module keeps the leader's Popen (see ``unreaped_leaders``) so its PGID
|
|
stays reserved until ``cleanup_unreaped_leaders`` reaps it.
|
|
"""
|
|
|
|
|
|
def _forget(process: subprocess.Popen) -> None:
|
|
with _UNREAPED_LOCK:
|
|
_UNREAPED[:] = [owned for owned in _UNREAPED if owned is not process]
|
|
|
|
|
|
def unreaped_leaders() -> list[subprocess.Popen]:
|
|
"""Return the leaders whose cleanup timed out and that are still held unreaped."""
|
|
with _UNREAPED_LOCK:
|
|
return list(_UNREAPED)
|
|
|
|
|
|
def cleanup_unreaped_leaders(grace: float) -> None:
|
|
"""Retry cleanup of every held leader through ``terminate_group``.
|
|
|
|
Each retry re-checks wait ownership first, so a leader reaped elsewhere is
|
|
dropped without a signal. A leader that still does not exit stays held.
|
|
Raise the first failure after trying every leader.
|
|
"""
|
|
errors = []
|
|
for process in unreaped_leaders():
|
|
try:
|
|
terminate_group(process, grace)
|
|
except (ProcessGroupCleanupError, ProcessGroupOwnershipError) as error:
|
|
errors.append(error)
|
|
except OSError as error:
|
|
# Signalling or reaping failed: the group may still be running.
|
|
failure = _stop_failure(process, error)
|
|
failure.__cause__ = error
|
|
errors.append(failure)
|
|
if errors:
|
|
for other in errors[1:]:
|
|
errors[0].add_note(f"Also failed: {other}")
|
|
raise errors[0]
|
|
|
|
|
|
def start_owned_group(argv, **kwargs) -> subprocess.Popen:
|
|
"""Launch with exclusive reaping responsibility for the group leader.
|
|
|
|
Reject an inherited SIGCHLD=SIG_IGN or custom SIGCHLD handler before
|
|
spawning. The host must leave SIGCHLD at SIG_DFL and exclude independent
|
|
reapers (including waitpid(-1)) for the lifetime of the group. An unreaped
|
|
leader reserves its PGID until the final group signal. Without
|
|
waitid(WNOWAIT) there is no portable way to observe that exit while
|
|
keeping the reservation.
|
|
"""
|
|
if os.name == "posix":
|
|
if signal.getsignal(signal.SIGCHLD) != signal.SIG_DFL:
|
|
raise ProcessGroupOwnershipError("owned process groups require SIGCHLD at SIG_DFL")
|
|
if not all(hasattr(os, attr) for attr in (
|
|
"waitid", "P_PID", "WEXITED", "WNOHANG", "WNOWAIT", "CLD_EXITED",
|
|
)):
|
|
raise ProcessGroupOwnershipError("owned process groups require os.waitid with WNOWAIT")
|
|
return subprocess.Popen(argv, start_new_session=True, **kwargs)
|
|
|
|
|
|
def _lost_leader(process: subprocess.Popen) -> ProcessGroupOwnershipError:
|
|
return ProcessGroupOwnershipError(
|
|
f"lost wait ownership of group leader {process.pid}; check SIGCHLD and external reapers")
|
|
|
|
|
|
def leader_returncode(process: subprocess.Popen, *, block: bool = False) -> int | None:
|
|
"""Observe a POSIX leader's exit without releasing its PID."""
|
|
if process.returncode is not None:
|
|
return process.returncode
|
|
flags = os.WEXITED | os.WNOWAIT
|
|
if not block:
|
|
flags |= os.WNOHANG
|
|
try:
|
|
status = os.waitid(os.P_PID, process.pid, flags)
|
|
except ChildProcessError as error:
|
|
raise _lost_leader(process) from error
|
|
if status is None:
|
|
return None
|
|
return status.si_status if status.si_code == os.CLD_EXITED else -status.si_status
|
|
|
|
|
|
def capture_group_output(
|
|
process: subprocess.Popen, timeout: float | None, *,
|
|
signals: ProcessSignals, grace: float, drain: float,
|
|
) -> tuple[str, str]:
|
|
"""Read through leader exit and allow up to `drain` seconds after group cleanup."""
|
|
deadline = time.monotonic() + timeout if timeout is not None else None
|
|
output = {process.stdout: bytearray(), process.stderr: bytearray()}
|
|
|
|
def remaining_time():
|
|
if deadline is None:
|
|
return None
|
|
remaining = deadline - time.monotonic()
|
|
if remaining <= 0:
|
|
raise subprocess.TimeoutExpired(
|
|
process.args, timeout, output=bytes(output[process.stdout]) or None,
|
|
stderr=bytes(output[process.stderr]) or None)
|
|
return remaining
|
|
|
|
def read_ready(selector, wait):
|
|
for key, _ in selector.select(wait):
|
|
data = os.read(key.fd, 32768)
|
|
if data:
|
|
output[key.fileobj].extend(data)
|
|
else:
|
|
selector.unregister(key.fileobj)
|
|
|
|
with cleanup_failure_first() as resources:
|
|
try:
|
|
selector = resources.enter_context(selectors.DefaultSelector())
|
|
for stream in output:
|
|
selector.register(stream, selectors.EVENT_READ)
|
|
with signals.interruptible():
|
|
while leader_returncode(process) is None:
|
|
remaining = remaining_time()
|
|
wait = _POLL_INTERVAL if remaining is None else min(_POLL_INTERVAL, remaining)
|
|
read_ready(selector, wait)
|
|
except BaseException as error:
|
|
_cleanup_after_error(process, grace, error)
|
|
raise
|
|
|
|
stop_group(process, grace)
|
|
drain_deadline = time.monotonic() + drain
|
|
with signals.interruptible():
|
|
while selector.get_map():
|
|
remaining = drain_deadline - time.monotonic()
|
|
if remaining <= 0:
|
|
break
|
|
# Bound each wait: selectors overflow on very large timeouts.
|
|
read_ready(selector, min(_POLL_INTERVAL, remaining))
|
|
|
|
stdout, stderr = (
|
|
data.decode(stream.encoding, stream.errors).replace("\r\n", "\n").replace("\r", "\n")
|
|
for stream, data in output.items()
|
|
)
|
|
return stdout, stderr
|
|
|
|
|
|
def _group_may_be_running(pgid: int) -> bool:
|
|
# Linux includes zombies in killpg(..., 0), including our unreaped leader.
|
|
# A process-table snapshot can shorten the grace period. Always follow it
|
|
# with a group signal in case children forked during the scan.
|
|
if sys.platform != "linux":
|
|
return True
|
|
saw_leader = False
|
|
try:
|
|
with os.scandir("/proc") as entries:
|
|
for entry in entries:
|
|
if not entry.name.isdecimal():
|
|
continue
|
|
try:
|
|
with open(f"{entry.path}/stat", "rb") as stat:
|
|
state, _, group = stat.read().rsplit(b")", 1)[1].split(maxsplit=3)[:3]
|
|
except (FileNotFoundError, ProcessLookupError):
|
|
continue
|
|
if int(group) == pgid:
|
|
if state not in (b"X", b"Z"):
|
|
return True
|
|
saw_leader |= int(entry.name) == pgid
|
|
except OSError:
|
|
# Without a readable process table, allow the full grace period.
|
|
return True
|
|
# A procfs mount from another PID namespace may omit the leader.
|
|
return not saw_leader
|
|
|
|
|
|
def terminate_group(process: subprocess.Popen, grace: float) -> None:
|
|
"""Stop the entire session leader's group, including after the leader exits."""
|
|
# Serialize with Popen's POSIX wait/poll and concurrent cleanup. The PID
|
|
# reserves the PGID until the final group signal, even for a zombie leader.
|
|
with process._waitpid_lock:
|
|
if process.returncode is not None:
|
|
_forget(process)
|
|
return
|
|
# Surface ownership loss. After an external reap the numeric PGID
|
|
# is no longer reserved, so signalling it could hit an unrelated group.
|
|
try:
|
|
leader_returncode(process)
|
|
except ProcessGroupOwnershipError:
|
|
_forget(process)
|
|
raise
|
|
pgid = process.pid
|
|
if _signal_group(pgid, signal.SIGTERM):
|
|
deadline = time.monotonic() + grace
|
|
while _signal_group(pgid, 0):
|
|
remaining = deadline - time.monotonic()
|
|
if remaining <= 0 or not _group_may_be_running(pgid):
|
|
_signal_group(pgid, signal.SIGKILL)
|
|
break
|
|
time.sleep(min(_POLL_INTERVAL, remaining))
|
|
# Observe the exit without reaping, so the PGID stays reserved if the
|
|
# leader never exits; a later call can then still signal the group.
|
|
reap_deadline = time.monotonic() + _REAP_TIMEOUT
|
|
while leader_returncode(process) is None:
|
|
remaining = reap_deadline - time.monotonic()
|
|
if remaining <= 0:
|
|
# Hold the Popen so the leader stays unreaped after the caller unwinds.
|
|
with _UNREAPED_LOCK:
|
|
if not any(owned is process for owned in _UNREAPED):
|
|
_UNREAPED.append(process)
|
|
raise ProcessGroupCleanupError(
|
|
f"group leader {pgid} did not exit within {_REAP_TIMEOUT:g}s after"
|
|
" the final group signal; left unreaped to keep its group ID reserved")
|
|
time.sleep(min(_POLL_INTERVAL, remaining))
|
|
try:
|
|
_, status = os.waitpid(pgid, 0)
|
|
except ChildProcessError as error:
|
|
# Another reaper took the leader after it exited. No signal follows.
|
|
_forget(process)
|
|
raise _lost_leader(process) from error
|
|
process.returncode = os.waitstatus_to_exitcode(status)
|
|
_forget(process)
|
|
|
|
|
|
def _stop_failure(process: subprocess.Popen, error: BaseException) -> ProcessGroupCleanupError:
|
|
return ProcessGroupCleanupError(
|
|
f"could not stop the process group of leader {process.pid} ({error!r});"
|
|
" it may still be running")
|
|
|
|
|
|
def stop_group(process: subprocess.Popen, grace: float) -> None:
|
|
"""``terminate_group`` for owners: every stop failure is a ProcessGroupCleanupError.
|
|
|
|
An OSError from killpg/waitpid or a lost wait ownership means the group may
|
|
still be running, so it is reported as a cleanup failure (exit 4), chained
|
|
to the underlying error.
|
|
"""
|
|
try:
|
|
terminate_group(process, grace)
|
|
except ProcessGroupCleanupError:
|
|
raise
|
|
except Exception as error:
|
|
raise _stop_failure(process, error) from error
|
|
|
|
|
|
def _cleanup_after_error(process: subprocess.Popen, grace: float, error: BaseException) -> None:
|
|
"""Stop the group after `error`; a cleanup failure replaces it, chained as its cause.
|
|
|
|
A group that may still be running outranks the original error, which must
|
|
not hide it (for example as an "untested" capability result). Every stop
|
|
failure counts; the stop error itself is kept as the context.
|
|
"""
|
|
try:
|
|
terminate_group(process, grace)
|
|
except ProcessGroupCleanupError as cleanup_error:
|
|
raise cleanup_error from error
|
|
except Exception as stop_error:
|
|
raise _stop_failure(process, stop_error) from error
|
|
except BaseException as cleanup_error:
|
|
raise cleanup_error from error
|
|
|
|
|
|
@contextmanager
|
|
def cleanup_failure_first():
|
|
"""Keep a propagating ProcessGroupCleanupError primary over errors closing resources.
|
|
|
|
Yield an ExitStack: enter the resources to close (pipes, a selector) on it.
|
|
When the body raises a cleanup failure, the stack is closed while it
|
|
propagates, and whatever closing raises is attached to it as a note
|
|
instead of replacing it; the failure keeps its own cause and context.
|
|
Because only exceptions raised by the stack's own close are attached, an
|
|
unrelated, older cleanup failure found in some other exception's context
|
|
is never promoted.
|
|
|
|
That includes an interrupt (KeyboardInterrupt or SystemExit) raised while
|
|
closing: a group may still be running, and only a propagating cleanup
|
|
failure makes the caller record it and exit 4. Re-raising the interrupt
|
|
would lose that, so it becomes a note, as ProcessSignals does for a signal
|
|
deferred during a failed cleanup. Without a cleanup failure, close errors
|
|
propagate as usual.
|
|
"""
|
|
with ExitStack() as resources:
|
|
try:
|
|
yield resources
|
|
except ProcessGroupCleanupError as failure:
|
|
closing = resources.pop_all()
|
|
try:
|
|
closing.__exit__(type(failure), failure, failure.__traceback__)
|
|
except BaseException as error:
|
|
close_errors, current = [], error
|
|
# Follow the chain of close errors back to the failure; a
|
|
# context cycle (or a chain not reaching it) ends the walk.
|
|
while (current is not None and current is not failure
|
|
and not any(current is seen for seen in close_errors)):
|
|
close_errors.append(current)
|
|
current = current.__context__
|
|
for close_error in reversed(close_errors):
|
|
failure.add_note(
|
|
f"closing resources during cleanup also failed: {close_error!r}")
|
|
raise # the cleanup failure, with its own cause and context
|
|
|
|
|
|
def nested_cleanup_grace(grace: float) -> float:
|
|
"""TERM-to-KILL wait for a step that stops its own groups (verify-edit).
|
|
|
|
The step stops up to ``_NESTED_GROUPS`` groups in turn, each taking up to
|
|
`grace` before SIGKILL and ``_REAP_TIMEOUT`` for its leader to exit.
|
|
"""
|
|
return _NESTED_GROUPS * (grace + _REAP_TIMEOUT) + _NESTED_SLACK
|
|
|
|
|
|
def wait_for_step(argv, *, stdout, signals: ProcessSignals, grace: float, server_cleanup: bool,
|
|
cleanup_exit: int | None = None, launch_guard=None) -> int:
|
|
"""Run one step in its own POSIX session and clean up before returning.
|
|
|
|
`cleanup_exit` is the status by which the step reports that it could not
|
|
stop its own groups. It is never lost to a signal: on an interrupted wait
|
|
it becomes a ProcessGroupCleanupError chained to the interruption, and on
|
|
a normal exit it is returned while any deferred signal stays pending.
|
|
|
|
`launch_guard`, when given, is a context-manager factory entered around the
|
|
launch only (not the wait); it may raise to refuse the launch.
|
|
"""
|
|
signals.check()
|
|
process = None
|
|
try:
|
|
with launch_guard() if launch_guard is not None else nullcontext():
|
|
process = start_owned_group(argv, stdout=stdout, stderr=subprocess.STDOUT)
|
|
except BaseException as error:
|
|
if process is None:
|
|
raise
|
|
# Launched, but the guard failed on release: stop the step, then report.
|
|
_cleanup_after_error(process, nested_cleanup_grace(grace) if server_cleanup else grace, error)
|
|
raise
|
|
try:
|
|
with signals.interruptible():
|
|
observed = leader_returncode(process, block=True)
|
|
except BaseException as error:
|
|
# verify-edit can own an evaluator group and a separate vLLM group.
|
|
# Allow it to stop both before escalating against verify-edit itself.
|
|
if server_cleanup:
|
|
grace = nested_cleanup_grace(grace)
|
|
_cleanup_after_error(process, grace, error)
|
|
if cleanup_exit is not None and process.returncode == cleanup_exit:
|
|
raise ProcessGroupCleanupError(
|
|
f"step {process.pid} exited {cleanup_exit} while it was being stopped: it could"
|
|
" not stop a process group it started") from error
|
|
raise
|
|
try:
|
|
stop_group(process, grace)
|
|
except BaseException as error:
|
|
if cleanup_exit is None or observed != cleanup_exit:
|
|
raise
|
|
# The step already reported a cleanup failure; stopping its own group
|
|
# failing too must not hide it.
|
|
raise ProcessGroupCleanupError(
|
|
f"step {process.pid} exited {cleanup_exit}: it could not stop a process group it"
|
|
f" started; stopping the step's own group then failed: {error!r}") from error
|
|
if cleanup_exit is None or process.returncode != cleanup_exit:
|
|
signals.check()
|
|
return process.returncode
|