Agent Zero AI framework
Serialize runtime package preparation
Alessandroef011600eff8
diff --git a/helpers/system_packages.py b/helpers/system_packages.pynew file mode 100644index 000000000..7b4b979a0--- /dev/null+++ b/helpers/system_packages.py@@ -0,0 +1,42 @@+from __future__ import annotations++import subprocess+import threading+import time+from typing import Callable+++APT_LOCK_TIMEOUT_SECONDS = 240+APT_LOCK_RETRY_SECONDS = 5++_apt_lock = threading.RLock()+++def run_apt_with_retries(+ runner: Callable[[], subprocess.CompletedProcess[str]],+ *,+ lock_timeout_seconds: int = APT_LOCK_TIMEOUT_SECONDS,+ retry_seconds: int = APT_LOCK_RETRY_SECONDS,+) -> subprocess.CompletedProcess[str]:+ """Run an apt/dpkg command, serializing in-process callers and waiting out apt locks."""++ with _apt_lock:+ deadline = time.monotonic() + max(0, lock_timeout_seconds)+ while True:+ result = runner()+ if result.returncode == 0 or not is_apt_lock_error(result):+ return result+ remaining = deadline - time.monotonic()+ if remaining <= 0:+ return result+ time.sleep(min(max(1, retry_seconds), remaining))+++def is_apt_lock_error(result: subprocess.CompletedProcess[str]) -> bool:+ output = f"{result.stderr or ''}\n{result.stdout or ''}".lower()+ return (+ "could not get lock" in output+ or "unable to lock directory" in output+ or "unable to acquire the dpkg frontend lock" in output+ or "is another process using it" in output+ )diff --git a/plugins/_desktop/hooks.py b/plugins/_desktop/hooks.pyindex 8c5068b1b..2b369ef9c 100644--- a/plugins/_desktop/hooks.py+++ b/plugins/_desktop/hooks.py@@ -7,11 +7,21 @@ import urllib.requestfrom pathlib import Pathfrom typing import Any-+from helpers import system_packages++LIBREOFFICE_RUNTIME_PACKAGES = (+ "libreoffice-core",+ "libreoffice-writer",+ "libreoffice-calc",+ "libreoffice-impress",+ "libreoffice-gtk3",+ "python3-uno",+)XPRA_SOURCE_FILE = Path("/etc/apt/sources.list.d/xpra.sources")XPRA_KEYRING_FILE = Path("/usr/share/keyrings/xpra.asc")XPRA_KEY_URL = "https://xpra.org/xpra.asc"RUNTIME_PACKAGES = (+ *LIBREOFFICE_RUNTIME_PACKAGES,"xpra-server","xpra-client","xpra-client-gtk3",@@ -102,14 +112,7 @@ def _purge_packages(installed = installed_packages if installed_packages is not None else []if not installed:return- result = subprocess.run(- ["apt-get", "purge", "-y", *installed],- check=False,- text=True,- capture_output=True,- timeout=180,- env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},- )+ result = _run_apt_command(["apt-get", "purge", "-y", *installed], timeout=180)if result.returncode == 0:removed.extend(installed)return@@ -161,14 +164,7 @@ def _install_runtime_packages(*,optional: bool = False,) -> bool:- result = subprocess.run(- ["apt-get", "install", "-y", "--no-install-recommends", *packages],- check=False,- text=True,- capture_output=True,- timeout=900,- env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},- )+ result = _run_apt_command(["apt-get", "install", "-y", "--no-install-recommends", *packages], timeout=900)if result.returncode == 0:installed.extend(packages)return True@@ -185,14 +181,7 @@ def _is_xpra_codec_dependency_gap(output: str) -> bool:def _apt_update(errors: list[str]) -> bool:- result = subprocess.run(- ["apt-get", "update"],- check=False,- text=True,- capture_output=True,- timeout=300,- env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},- )+ result = _run_apt_command(["apt-get", "update"], timeout=300)if result.returncode == 0:return Trueerrors.append((result.stderr or result.stdout or "apt-get update failed").strip())@@ -222,13 +211,9 @@ def _package_candidates_available(packages: list[str]) -> bool:def _ensure_xpra_repository(installed: list[str], errors: list[str]) -> None:if not _package_installed("ca-certificates"):- result = subprocess.run(+ result = _run_apt_command(["apt-get", "install", "-y", "--no-install-recommends", "ca-certificates"],- check=False,- text=True,- capture_output=True,timeout=180,- env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},)if result.returncode != 0:errors.append((result.stderr or result.stdout or "apt-get install ca-certificates failed").strip())@@ -254,6 +239,19 @@ def _download(url: str) -> bytes:return response.read()+def _run_apt_command(command: list[str], *, timeout: int) -> subprocess.CompletedProcess[str]:+ return system_packages.run_apt_with_retries(+ lambda: subprocess.run(+ command,+ check=False,+ text=True,+ capture_output=True,+ timeout=timeout,+ env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},+ )+ )++def _xpra_repository_source() -> str:os_release = _read_os_release()os_id = os_release.get("ID", "")diff --git a/plugins/_office/hooks.py b/plugins/_office/hooks.pyindex 95a002f5b..07bc8fa5c 100644--- a/plugins/_office/hooks.py+++ b/plugins/_office/hooks.py@@ -6,7 +6,7 @@ import subprocessfrom pathlib import Pathfrom typing import Any-from helpers import files+from helpers import files, system_packagesPROJECT_ROOT = Path(__file__).resolve().parents[2]@@ -305,14 +305,7 @@ def _purge_packages(installed = installed_packages if installed_packages is not None else _installed_packages(RETIRED_WEB_PACKAGES)if not installed:return- result = subprocess.run(- ["apt-get", "purge", "-y", *installed],- check=False,- text=True,- capture_output=True,- timeout=180,- env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},- )+ result = _run_apt_command(["apt-get", "purge", "-y", *installed], timeout=180)if result.returncode == 0:removed.extend(installed)return@@ -348,14 +341,7 @@ def _install_runtime_packages(installed: list[str],errors: list[str],) -> bool:- result = subprocess.run(- ["apt-get", "install", "-y", "--no-install-recommends", *packages],- check=False,- text=True,- capture_output=True,- timeout=900,- env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},- )+ result = _run_apt_command(["apt-get", "install", "-y", "--no-install-recommends", *packages], timeout=900)if result.returncode == 0:installed.extend(packages)return True@@ -365,15 +351,21 @@ def _install_runtime_packages(def _apt_update(errors: list[str]) -> bool:- result = subprocess.run(- ["apt-get", "update"],- check=False,- text=True,- capture_output=True,- timeout=300,- env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},- )+ result = _run_apt_command(["apt-get", "update"], timeout=300)if result.returncode == 0:return Trueerrors.append((result.stderr or result.stdout or "apt-get update failed").strip())return False+++def _run_apt_command(command: list[str], *, timeout: int) -> subprocess.CompletedProcess[str]:+ return system_packages.run_apt_with_retries(+ lambda: subprocess.run(+ command,+ check=False,+ text=True,+ capture_output=True,+ timeout=timeout,+ env={**os.environ, "DEBIAN_FRONTEND": "noninteractive"},+ )+ )diff --git a/tests/test_office_document_store.py b/tests/test_office_document_store.pyindex 5f85d2681..a6a662233 100644--- a/tests/test_office_document_store.py+++ b/tests/test_office_document_store.py@@ -13,11 +13,11 @@ from pathlib import Pathimport pytest-PROJECT_ROOT = Path(__file__).resolve().parents[1]if str(PROJECT_ROOT) not in sys.path:sys.path.insert(0, str(PROJECT_ROOT))+from helpers import system_packagesfrom plugins._office import hooksfrom plugins._desktop import hooks as desktop_hooksfrom plugins._desktop.helpers import desktop_session@@ -1334,6 +1334,54 @@ def test_cleanup_hook_removes_retired_supervisor_program_after_marker(tmp_path,]+def test_office_runtime_dependency_install_waits_out_apt_locks(monkeypatch):+ calls = []+ installed_state = {"libreoffice-core": False}+ update_attempts = {"count": 0}++ monkeypatch.setattr(hooks.os, "geteuid", lambda: 0)+ monkeypatch.setattr(+ hooks.shutil,+ "which",+ lambda name: f"/usr/bin/{name}" if name in {"apt-get", "dpkg-query"} else "",+ )+ monkeypatch.setattr(hooks, "RUNTIME_PACKAGES", ("libreoffice-core",))+ monkeypatch.setattr(hooks, "_package_installed", lambda package: installed_state.get(package, False))+ monkeypatch.setattr(system_packages.time, "sleep", lambda _seconds: None)++ def fake_run(command, **kwargs):+ calls.append(command)+ if command == ["apt-get", "update"]:+ update_attempts["count"] += 1+ if update_attempts["count"] == 1:+ return types.SimpleNamespace(+ returncode=100,+ stdout="",+ stderr="E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1829 (apt-get)",+ )+ if command[:2] == ["apt-get", "install"]:+ installed_state["libreoffice-core"] = True+ return types.SimpleNamespace(returncode=0, stdout="", stderr="")++ monkeypatch.setattr(hooks.subprocess, "run", fake_run)+ installed = []+ errors = []++ hooks._ensure_runtime_dependencies(installed, errors)++ assert errors == []+ assert installed == ["libreoffice-core"]+ assert calls[:2] == [["apt-get", "update"], ["apt-get", "update"]]+ assert calls[2][:4] == ["apt-get", "install", "-y", "--no-install-recommends"]+++def test_desktop_runtime_packages_include_libreoffice_for_desktop_status():+ assert set(desktop_hooks.LIBREOFFICE_RUNTIME_PACKAGES).issubset(desktop_hooks.RUNTIME_PACKAGES)+ assert "libreoffice-writer" in desktop_hooks.RUNTIME_PACKAGES+ assert "libreoffice-calc" in desktop_hooks.RUNTIME_PACKAGES+ assert "libreoffice-impress" in desktop_hooks.RUNTIME_PACKAGES++def test_cleanup_hook_installs_missing_desktop_session_dependencies(monkeypatch):calls = []installed_state = {"xpra": False}