Skip to content

The agent that grows with you

fix(launchers): a Python repin retires the running Windows launcher

Teknium9e336258165d
diff --git a/hermes_cli/_launchers.py b/hermes_cli/_launchers.py
index 1e6eb47bc27..bfd02ef9882 100644
--- a/hermes_cli/_launchers.py
+++ b/hermes_cli/_launchers.py
@@ -11,6 +11,7 @@ interpreter before it publishes either command.
from __future__ import annotations
+from contextlib import suppress
import json
import os
import shlex
@@ -559,14 +560,39 @@ def stage_launcher(name: str, repo_root: Path, out_dir: Path) -> Path | None:
if path is not None and path.suffix == ".cmd":
# cmd.exe prefers .exe. An older launcher must not shadow the
# newly published command when distlib is unavailable.
- try:
- (Path(out_dir) / f"{name}.exe").unlink(missing_ok=True)
- except OSError:
+ if not _retire_exe(Path(out_dir) / f"{name}.exe"):
return None
return path
return None
+def _retire_exe(exe: Path) -> bool:
+ """Remove ``exe``; a running one is renamed aside instead.
+
+ Windows refuses to delete a mapped image but allows renaming it, and the
+ launcher a Python repin has to retire is usually the one running
+ ``hermes update`` (or the launch retrying its owed launcher step), so a
+ plain unlink left every repinned update owing launchers -- and the install
+ stamp -- forever. The aside copy goes on the next publication once its
+ process has exited."""
+ for leftover in exe.parent.glob(f"{exe.name}.*.deleteme"):
+ with suppress(OSError):
+ leftover.unlink()
+ try:
+ exe.unlink(missing_ok=True)
+ return True
+ except OSError:
+ pass
+ aside = exe.with_name(f"{exe.name}.{os.getpid()}.deleteme")
+ try:
+ os.replace(exe, aside)
+ except OSError:
+ return False
+ with suppress(OSError):
+ aside.unlink()
+ return True
+
+
def _first_token(line: str) -> Path | None:
"""First whitespace-delimited token of *line*, honoring one quote pair.
diff --git a/tests/hermes_cli/test_source_launcher_publication.py b/tests/hermes_cli/test_source_launcher_publication.py
index 1d84a055640..153c0db4def 100644
--- a/tests/hermes_cli/test_source_launcher_publication.py
+++ b/tests/hermes_cli/test_source_launcher_publication.py
@@ -381,21 +381,39 @@ def test_running_source_launcher_can_republish_itself(tmp_path, monkeypatch, lau
@pytest.mark.platforms("windows")
-def test_repin_without_distlib_retires_stale_native_launcher(tmp_path, monkeypatch):
[email protected]("running", [False, True], ids=["idle", "running"])
+def test_repin_without_distlib_retires_stale_native_launcher(tmp_path, monkeypatch, running):
repo, home, _interpreter = fixture_tree(tmp_path, monkeypatch)
+ (repo / "hermes_cli/main.py").write_text(
+ "import sys\ndef main():\n print('ready', flush=True)\n sys.stdin.read()\n return 0\n",
+ encoding="utf-8")
local = repo / ".hermes/bin"
original = Path(_launchers.ensure_install_launchers(repo, local)[0])
assert original.suffix == ".exe"
+ process = None
+ if running:
+ # The launcher a repin retires is the one running `hermes update`.
+ process = subprocess.Popen([str(original)], cwd=tmp_path, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, text=True, encoding="utf-8")
+ assert any(line.strip() == "ready" for line in iter(process.stdout.readline, ""))
new_python = home / "tools" / "repinned" / "python.exe"
new_python.parent.mkdir()
new_python.touch()
(home / "tools/facts.json").write_text(
json.dumps({"packages": {"python": {"entry": "repinned"}}}), encoding="utf-8")
monkeypatch.setattr(_launchers, "_load_script_maker", lambda: None)
- result = _launchers.stage_launcher("hermes", repo, local)
+ try:
+ result = _launchers.stage_launcher("hermes", repo, local)
+ finally:
+ if process is not None:
+ process.communicate("", timeout=30)
assert result == local / "hermes.cmd"
assert not original.exists() # cmd.exe must not run the old exe first
assert result is not None and str(new_python) in result.read_text(encoding="utf-8-sig")
+ if process is not None:
+ assert process.returncode == 0 # the retired launcher finished its own run
+ assert _launchers.stage_launcher("hermes", repo, local) == local / "hermes.cmd"
+ assert not list(local.glob("hermes.exe*")) # the aside copy is collected once it exits
@pytest.mark.platforms("windows")