desktop_release_holders.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. """Name the programs holding ``apps/desktop/release`` open by working directory (#101789).
  2. Windows refuses to rename a directory that a live process uses as its current working
  3. directory. A resident helper that some Hermes window spawned (an IME cloud service such as
  4. Sogou's ``SogouCloud.exe``, an AV or assistant component) inherits that window's cwd inside
  5. ``release/win-unpacked`` and keeps it after the app exits, so the stage-and-swap promotion
  6. fails on every retry although the build succeeded. Its exe lives elsewhere, so the
  7. exe-under-release stop never sees it.
  8. These holders belong to the user, not to Hermes: they are NAMED in the failure and closed
  9. only with the user's consent, given either at an interactive terminal prompt or through the
  10. Desktop's "close it and retry" choice. That choice leaves a consent file in ``release/`` bound
  11. to the hand-off run it starts and to the exact processes (pid + start time) the user saw; the
  12. next swap attempt consumes it whatever its source, and only that run's swap may act on it.
  13. A non-interactive run with no matching consent never closes anything.
  14. """
  15. from __future__ import annotations
  16. import contextlib
  17. import json
  18. import os
  19. import sys
  20. import time
  21. from dataclasses import dataclass
  22. from pathlib import Path
  23. from typing import Callable, Optional
  24. # windows.ps1 parses this exact prefix out of the update output (one line per holder).
  25. HOLDER_LINE_PREFIX = "Desktop app folder held by: "
  26. # Written by the Desktop when the user chooses "Close <program> and retry"; removed at the start
  27. # of the next promotion swap attempt, so a consent covers exactly one retry.
  28. CONSENT_FILE_NAME = ".close-holders-consent.json"
  29. # psutil create_time is a float rebuilt from the OS on each read: equal within this tolerance.
  30. _START_TIME_TOLERANCE_S = 0.5
  31. _CLOSE_WAIT_S = 5
  32. @dataclass(frozen=True)
  33. class ReleaseHolder:
  34. pid: int
  35. name: str
  36. exe: str
  37. create_time: float
  38. def describe(self) -> str:
  39. return f"{self.name} (pid {self.pid}, {self.exe or 'path unreadable'})"
  40. def holder_line(self) -> str:
  41. """The machine-read line windows.ps1 hands to the Desktop (pid + start time identify it)."""
  42. return (f"{HOLDER_LINE_PREFIX}{self.name} (pid {self.pid}, started {self.create_time:.3f}, "
  43. f"{self.exe or 'path unreadable'})")
  44. @dataclass(frozen=True)
  45. class HolderConsent:
  46. """The processes the user agreed to close, as (pid, start time, normalized exe)."""
  47. holders: frozenset[tuple[int, float, str]] = frozenset()
  48. def covers(self, holder: ReleaseHolder) -> bool:
  49. return bool(holder.exe) and any(
  50. pid == holder.pid and abs(start - holder.create_time) <= _START_TIME_TOLERANCE_S
  51. and exe == _norm(holder.exe)
  52. for pid, start, exe in self.holders
  53. )
  54. NO_CONSENT = HolderConsent()
  55. def _norm(path: str) -> str:
  56. return os.path.normcase(os.path.abspath(path))
  57. def _inside(path: str, root: str) -> bool:
  58. return path == root or path.startswith(root.rstrip("\\/") + os.sep)
  59. def _release_root(release_dir: Path) -> Optional[str]:
  60. try:
  61. return _norm(str(release_dir.resolve()))
  62. except OSError:
  63. return None
  64. def _exe_or_empty(proc) -> str:
  65. """Read only for a matched holder: another process's exe is none of our business."""
  66. try:
  67. return proc.exe() or ""
  68. except Exception: # health: allow BLE001 -- access denied for a protected process: named without a path
  69. return ""
  70. def find_release_cwd_holders(folder: Path) -> list[ReleaseHolder]:
  71. """Every other process whose working directory is ``folder`` (the app folder being renamed,
  72. e.g. ``release/win-unpacked``) or below it.
  73. A process whose cwd cannot be read (another user's, access denied) is skipped: it cannot
  74. be shown to hold the tree. Never raises.
  75. """
  76. root = _release_root(folder)
  77. try:
  78. import psutil
  79. procs = psutil.process_iter(["pid", "name"])
  80. except Exception: # health: allow BLE001 -- diagnostics on a failure path; no psutil = no names
  81. return []
  82. if root is None:
  83. return []
  84. me = os.getpid()
  85. holders: list[ReleaseHolder] = []
  86. for proc in procs:
  87. try:
  88. if proc.pid == me:
  89. continue
  90. cwd = proc.cwd()
  91. if not cwd or not _inside(_norm(cwd), root):
  92. continue
  93. holders.append(ReleaseHolder(
  94. pid=int(proc.pid), name=proc.info.get("name") or "unknown program",
  95. exe=_exe_or_empty(proc), create_time=float(proc.create_time()),
  96. ))
  97. except Exception: # health: allow BLE001 -- psutil raises per-process (gone, denied, zombie)
  98. continue
  99. return holders
  100. def take_consent(release_dir: Path, run_id: Optional[str]) -> HolderConsent:
  101. """Consume the Desktop's consent at the start of a swap attempt: the file is removed whether
  102. or not it applies, and it applies only to the hand-off run that the Desktop started for it."""
  103. path = release_dir / CONSENT_FILE_NAME
  104. try:
  105. raw = path.read_bytes()
  106. except OSError:
  107. return NO_CONSENT
  108. with contextlib.suppress(OSError):
  109. path.unlink()
  110. try:
  111. data = json.loads(raw.decode("utf-8-sig"))
  112. if not run_id or data.get("run_id") != run_id:
  113. return NO_CONSENT
  114. holders = frozenset(
  115. (int(h["pid"]), float(h["start_time"]), _norm(h["exe"]))
  116. for h in data["holders"] if isinstance(h.get("exe"), str) and h["exe"]
  117. )
  118. except (ValueError, TypeError, KeyError, AttributeError):
  119. return NO_CONSENT
  120. return HolderConsent(holders)
  121. def _ask_at_terminal(holders: list[ReleaseHolder], input_fn: Callable[[str], str]) -> bool:
  122. """Only a person at a terminal can answer; a hidden hand-off or a pipe never consents."""
  123. if not holders or not (sys.stdin and sys.stdin.isatty() and sys.stdout and sys.stdout.isatty()):
  124. return False
  125. names = ", ".join(h.describe() for h in holders)
  126. try:
  127. answer = input_fn(f" Close {names} and retry? [y/N] ")
  128. except (EOFError, KeyboardInterrupt):
  129. return False
  130. return answer.strip().lower() in ("y", "yes")
  131. def close_holders(holders: list[ReleaseHolder], folder: Path) -> list[int]:
  132. """Terminate exactly these holders, each re-checked right before: same pid AND creation
  133. time (no recycled pid), cwd still inside ``folder``, and not one of our ancestors."""
  134. root = _release_root(folder)
  135. try:
  136. import psutil
  137. ancestors = {p.pid for p in psutil.Process(os.getpid()).parents()}
  138. except Exception: # health: allow BLE001 -- no process table means nothing can be verified
  139. return []
  140. targets = []
  141. for holder in holders:
  142. try:
  143. proc = psutil.Process(holder.pid)
  144. if proc.pid in ancestors or abs(proc.create_time() - holder.create_time) > _START_TIME_TOLERANCE_S:
  145. continue
  146. if root is None or not _inside(_norm(proc.cwd()), root):
  147. continue
  148. proc.terminate()
  149. targets.append(proc)
  150. except Exception: # health: allow BLE001 -- gone or denied: it is not closed by us
  151. continue
  152. if targets:
  153. with contextlib.suppress(Exception):
  154. _, alive = psutil.wait_procs(targets, timeout=_CLOSE_WAIT_S)
  155. for proc in alive:
  156. with contextlib.suppress(Exception):
  157. proc.kill()
  158. if alive:
  159. psutil.wait_procs(alive, timeout=_CLOSE_WAIT_S)
  160. return [int(p.pid) for p in targets if not p.is_running()]
  161. def release_after_refused_rename(
  162. folder: Path, *, consent: HolderConsent = NO_CONSENT, input_fn: Callable[[str], str] = input,
  163. ) -> tuple[list[ReleaseHolder], list[int]]:
  164. """After Windows refused to rename ``folder``: name every cwd holder inside it, then close
  165. the ones covered by ``consent`` (taken at the start of this swap attempt) or agreed to at the
  166. terminal. Returns (holders found, pids closed)."""
  167. holders = find_release_cwd_holders(folder)
  168. if not holders:
  169. return [], []
  170. # Human lines only: the machine lines go out once, for the holders still in the way when the
  171. # swap is given up (describe_block), so a holder closed here is not offered to the Desktop again.
  172. for holder in holders:
  173. print(f" {holder.describe()}")
  174. chosen = [h for h in holders if consent.covers(h)]
  175. # A holder whose program path cannot be read is usually protected (another user, elevated):
  176. # it cannot be closed from here either, so the user closes it themselves.
  177. closable = [h for h in holders if h.exe]
  178. for holder in holders:
  179. if not holder.exe:
  180. print(f" Close {holder.name} (pid {holder.pid}) yourself: Hermes cannot close it.")
  181. if not chosen and _ask_at_terminal(closable, input_fn):
  182. chosen = closable
  183. if not chosen:
  184. return holders, []
  185. closed = close_holders(chosen, folder)
  186. if closed:
  187. print(f" ✓ Closed with your consent: {', '.join(h.describe() for h in chosen if h.pid in closed)}")
  188. return holders, closed
  189. def describe_block(holders: list[ReleaseHolder], release_dir: Path) -> str:
  190. """The final report of a refused swap; ``holders`` are the ones still holding the folder."""
  191. names = ", ".join(h.describe() for h in holders)
  192. pids = " ".join(f"/PID {h.pid}" for h in holders)
  193. return (
  194. "".join(f" {h.holder_line()}\n" for h in holders)
  195. + f"✗ Windows would not replace the desktop app: {names} is using a folder inside {release_dir}.\n"
  196. f" It is not part of Hermes, so it was left running. Close it (Task Manager, or `taskkill {pids}`)\n"
  197. " and run `hermes desktop --force-build`, or run Update now in Hermes Desktop and choose to close it."
  198. )
  199. def holder_names(holders: list[ReleaseHolder]) -> str:
  200. return ", ".join(f"{h.name} (pid {h.pid})" for h in holders)