processes.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. """Contain each managed router tree without adopting the owner process."""
  2. from __future__ import annotations
  3. from collections.abc import Mapping
  4. import ctypes
  5. from ctypes import wintypes
  6. import subprocess
  7. import sys
  8. import threading
  9. import psutil
  10. class _BasicLimits(ctypes.Structure):
  11. _fields_ = [
  12. ("PerProcessUserTimeLimit", ctypes.c_longlong),
  13. ("PerJobUserTimeLimit", ctypes.c_longlong),
  14. ("LimitFlags", wintypes.DWORD),
  15. ("MinimumWorkingSetSize", ctypes.c_size_t),
  16. ("MaximumWorkingSetSize", ctypes.c_size_t),
  17. ("ActiveProcessLimit", wintypes.DWORD),
  18. ("Affinity", ctypes.c_size_t),
  19. ("PriorityClass", wintypes.DWORD),
  20. ("SchedulingClass", wintypes.DWORD),
  21. ]
  22. class _IoCounters(ctypes.Structure):
  23. _fields_ = [(name, ctypes.c_ulonglong) for name in (
  24. "ReadOperationCount", "WriteOperationCount", "OtherOperationCount",
  25. "ReadTransferCount", "WriteTransferCount", "OtherTransferCount",
  26. )]
  27. class _ExtendedLimits(ctypes.Structure):
  28. _fields_ = [
  29. ("BasicLimitInformation", _BasicLimits),
  30. ("IoInfo", _IoCounters),
  31. ("ProcessMemoryLimit", ctypes.c_size_t),
  32. ("JobMemoryLimit", ctypes.c_size_t),
  33. ("PeakProcessMemoryUsed", ctypes.c_size_t),
  34. ("PeakJobMemoryUsed", ctypes.c_size_t),
  35. ]
  36. class _WindowsJob:
  37. def __init__(self):
  38. self._lock = threading.Lock()
  39. self._api = ctypes.WinDLL("kernel32", use_last_error=True)
  40. for name, args, result in (
  41. ("CreateJobObjectW", [ctypes.c_void_p, wintypes.LPCWSTR], wintypes.HANDLE),
  42. ("SetInformationJobObject", [wintypes.HANDLE, ctypes.c_int,
  43. ctypes.c_void_p, wintypes.DWORD], wintypes.BOOL),
  44. ("AssignProcessToJobObject", [wintypes.HANDLE, wintypes.HANDLE], wintypes.BOOL),
  45. ("CloseHandle", [wintypes.HANDLE], wintypes.BOOL),
  46. ):
  47. fn = getattr(self._api, name)
  48. fn.argtypes = args
  49. fn.restype = result
  50. # NULL security attributes create a non-inheritable, unnamed owner handle.
  51. self._handle = self._api.CreateJobObjectW(None, None)
  52. if not self._handle:
  53. raise ctypes.WinError(ctypes.get_last_error())
  54. try:
  55. limits = _ExtendedLimits()
  56. # Neither BREAKAWAY_OK nor SILENT_BREAKAWAY_OK: descendants stay contained.
  57. limits.BasicLimitInformation.LimitFlags = 0x2000 # KILL_ON_JOB_CLOSE
  58. if not self._api.SetInformationJobObject(
  59. self._handle, 9, ctypes.byref(limits), ctypes.sizeof(limits)):
  60. raise ctypes.WinError(ctypes.get_last_error())
  61. except BaseException:
  62. self.close()
  63. raise
  64. def assign(self, proc: subprocess.Popen) -> None:
  65. # Popen retains the original process handle, avoiding a PID-reuse race.
  66. if not self._api.AssignProcessToJobObject(self._handle, int(proc._handle)):
  67. raise ctypes.WinError(ctypes.get_last_error())
  68. def close(self) -> None:
  69. """Terminate the contained tree; repeated closes are harmless."""
  70. with self._lock:
  71. if self._handle is not None:
  72. if not self._api.CloseHandle(self._handle):
  73. raise ctypes.WinError(ctypes.get_last_error())
  74. self._handle = None
  75. _CREDENTIAL_ENV_MARKERS = ("_API_KEY", "_TOKEN", "_SECRET", "PASSWORD", "_CREDENTIALS")
  76. def server_child_env(base_env: Mapping[str, str]) -> dict[str, str]:
  77. """Return the environment a native inference child (llama-server) gets.
  78. Provider and tool credentials never belong in a native child that talks to nobody but
  79. us — and on Windows they are not merely leaked: the bundled OpenMP runtime died with
  80. STATUS_HEAP_CORRUPTION during initialisation with one `*_API_KEY` present and loaded fine
  81. with only that variable removed (#116109, confirmed on a model-free libomp.dll probe).
  82. Everything else (PATH, CUDA_*, HSA_*, OMP_*, TEMP, …) passes through untouched. Applied by
  83. the llama-server supervisor only: spawn_server is also the generic bounded-probe spawner
  84. (git / PowerShell / update probes), whose children legitimately need GH_TOKEN, HF_TOKEN, …
  85. """
  86. return {
  87. key: value for key, value in base_env.items()
  88. if not any(marker in key.upper() for marker in _CREDENTIAL_ENV_MARKERS)
  89. }
  90. def spawn_server(cmd, *, owned: bool = False, **kwargs) -> tuple[subprocess.Popen, _WindowsJob | None]:
  91. """Start a router, returning its process and an owner-held containment handle.
  92. Keep the job until shutdown and call close() to terminate the entire tree.
  93. Windows closes it automatically if the owner dies. On POSIX ``owned=True`` gives
  94. the same guarantee: the router leads its own process group (its model children
  95. stay in it) and that group goes to this process's owner-death supervisor, which
  96. kills it the moment the owner dies by any cause (#126631). Without it Popen's
  97. ordinary behavior applies (this is also the generic bounded-probe spawner, whose
  98. short-lived children need no supervision). Assignment happens before the
  99. child's first instruction.
  100. """
  101. if sys.platform != "win32":
  102. if not owned:
  103. return subprocess.Popen(cmd, **kwargs), None
  104. from hermes_cli.process_identity import spawn_owned
  105. return spawn_owned(cmd, purpose="llama-server", **kwargs), None
  106. job = _WindowsJob()
  107. proc = None
  108. try:
  109. kwargs["creationflags"] = kwargs.get("creationflags", 0) | 0x00000004 # CREATE_SUSPENDED
  110. proc = subprocess.Popen(cmd, **kwargs)
  111. job.assign(proc)
  112. psutil.Process(proc.pid).resume()
  113. return proc, job
  114. except BaseException:
  115. try:
  116. if proc is not None:
  117. # Assignment may have failed: closing an empty job is not enough.
  118. proc.kill()
  119. proc.wait(timeout=10)
  120. finally:
  121. try:
  122. job.close()
  123. finally:
  124. if proc is not None:
  125. for stream in (proc.stdin, proc.stdout, proc.stderr):
  126. if stream is not None:
  127. stream.close()
  128. proc._handle.Close()
  129. raise