Skip to content

The agent that grows with you

fix(update): keep the completion parent stdlib-only around the libatomic pre-install

kshitijk4poor47e6c2ebb565
diff --git a/hermes_cli/update_completion.py b/hermes_cli/update_completion.py
index 091f7e0473c..b1d4c58a141 100644
--- a/hermes_cli/update_completion.py
+++ b/hermes_cli/update_completion.py
@@ -37,12 +37,15 @@ def _failed_result(request: dict, result_path: Path, code: int) -> int:
def run_completion(request: dict) -> dict:
"""Wait for new code; zero exit without a correlated terminal result fails closed."""
- from pm.libatomic import install_before_lock
-
- # The child below runs in a new session without a controlling terminal,
- # so this is the last point where sudo can ask for a password.
- install_before_lock()
root = Path(request["source"])
+ # The child below runs in a new session without a controlling terminal, so
+ # this is the last point where sudo can ask for a password. Run the new
+ # tree's pre-install as its own process (this parent stays stdlib-only); a
+ # tree without it, or any failure, just leaves the in-lock repair to report.
+ subprocess.run([sys.executable, "-I", "-S", "-c",
+ "import sys; sys.path.insert(0, sys.argv[1]); "
+ "from pm.libatomic import install_before_lock; install_before_lock()", str(root)],
+ cwd=root, stdout=None, stderr=subprocess.DEVNULL, check=False)
env = dict(os.environ, HERMES_HOME=request["home"], PYTHONUNBUFFERED="1")
for key in ("PYTHONPATH", "PYTHONHOME", "VIRTUAL_ENV"):
env.pop(key, None)
diff --git a/pm/libatomic.py b/pm/libatomic.py
index e826c89d93f..1664f5eb144 100644
--- a/pm/libatomic.py
+++ b/pm/libatomic.py
@@ -101,9 +101,12 @@ def install_before_lock() -> None:
from pm.store import MUSL_TARGETS, current_target
- if not sys.platform.startswith("linux") or _is_root():
+ if not sys.platform.startswith("linux") or _is_root() or not _has_terminal():
+ return
+ try:
+ target = current_target()
+ except RuntimeError: # unsupported architecture: PM reports it elsewhere
return
- target = current_target()
if target in MUSL_TARGETS or target.endswith("-bionic"):
return
try:
@@ -112,15 +115,17 @@ def install_before_lock() -> None:
except OSError:
pass
command = _host_install_command()
- argv = _command_plan(command)[0] if command else None
- if not argv or not _has_terminal():
+ if command is None:
+ return
+ argv = _command_plan(command)[0]
+ if not argv:
return
sudo = argv[0]
- if subprocess.run([sudo, "-n", "true"], stdin=subprocess.DEVNULL,
- capture_output=True, check=False).returncode == 0:
+ if _run([sudo, "-n", "true"], timeout=10):
return # the non-interactive repair will succeed on its own
print("→ Node.js needs libatomic.so.1; sudo may ask for your password to install it", flush=True)
- _install([sudo], command, stdin=None)
+ # Outside PM's lock: no timeout, so a slow prompt or mirror is never killed mid-dpkg.
+ _install([sudo], command, stdin=None, timeout=None)
def _has_terminal() -> bool:
@@ -131,20 +136,22 @@ def _has_terminal() -> bool:
return True
-def _run(argv: list[str], stdin=subprocess.DEVNULL) -> bool:
+def _run(argv: list[str], stdin: int | None = subprocess.DEVNULL, timeout: float | None = 300) -> bool:
try:
- return subprocess.run(argv, stdin=stdin, timeout=300, check=False).returncode == 0
+ return subprocess.run(argv, stdin=stdin, timeout=timeout, check=False).returncode == 0
except (OSError, subprocess.TimeoutExpired):
return False
-def _install(prefix: list[str], command: tuple[str, ...], stdin=subprocess.DEVNULL) -> bool:
- if _run([*prefix, *command], stdin):
+def _install(prefix: list[str], command: tuple[str, ...], stdin: int | None = subprocess.DEVNULL,
+ timeout: float | None = 300) -> bool:
+ if _run([*prefix, *command], stdin, timeout):
return True
if command[0] != "apt-get":
return False
# Minimal Debian/Ubuntu images ship empty package lists.
- return _run([*prefix, "apt-get", "update", "-qq"], stdin) and _run([*prefix, *command], stdin)
+ return (_run([*prefix, "apt-get", "update", "-qq"], stdin, timeout)
+ and _run([*prefix, *command], stdin, timeout))
def try_install_libatomic() -> tuple[bool, str]:
diff --git a/tests/pm/test_node_libatomic_install_boundary.py b/tests/pm/test_node_libatomic_install_boundary.py
index 6646357c074..52526a088b6 100644
--- a/tests/pm/test_node_libatomic_install_boundary.py
+++ b/tests/pm/test_node_libatomic_install_boundary.py
@@ -97,19 +97,22 @@ def test_installer_sudo_warmup_honours_non_interactive(tmp_path, flags):
def test_update_completion_installs_before_detaching(monkeypatch):
- """run_completion's child has no controlling terminal; the interactive install runs first."""
+ """run_completion's child has no controlling terminal; the pre-install runs first, in-session."""
from hermes_cli import update_completion
- from pm import libatomic
- order = []
- monkeypatch.setattr(libatomic, "install_before_lock", lambda: order.append("install"))
+ calls = []
+ monkeypatch.setattr(update_completion.subprocess, "run",
+ lambda argv, **kw: calls.append(("run", argv, kw)))
- def popen(*_args, **_kwargs):
- order.append("detach")
+ def popen(argv, **kw):
+ calls.append(("popen", argv, kw))
raise RuntimeError("stop after spawn")
monkeypatch.setattr(update_completion.subprocess, "Popen", popen)
with pytest.raises(RuntimeError, match="stop after spawn"):
update_completion.run_completion({"source": "/nonexistent", "home": "/nonexistent",
"receipt": {"update_id": "u"}})
- assert order == ["install", "detach"]
+ assert [kind for kind, _argv, _kw in calls] == ["run", "popen"]
+ _kind, argv, kw = calls[0]
+ assert "install_before_lock" in " ".join(argv)
+ assert "start_new_session" not in kw and kw.get("check") is False