fix(desktop-update): clean only the captured shim profile

Track the path actually launched, preserving the no-UI case and unrelated profiles. Adapted the ownership approach from #104362.

Co-authored-by: liuhao1024 <sunsky.lau@gmail.com>
This commit is contained in:
Teknium
2026-09-07 02:14:12 -07:00
parent 97f74b8361
commit 1bd7364d8e
2 changed files with 54 additions and 3 deletions
+7 -3
View File
@@ -72,7 +72,7 @@ RESULT="$HERMES_HOME/.hermes-update-result.json"
STATUS="${TMPDIR:-/tmp}/hermes-update-status.$$"
STARTED_AT="$(date +%s)" # the shim's elapsed clock; see serve-ui.py
UI_SERVER_PID="" UI_BROWSER_PID="" FINAL_CODE=1
UI_SERVER_PID="" UI_BROWSER_PID="" UI_PROFILE_DIR="" FINAL_CODE=1
FINAL_MSG="update did not complete"
DONE_NOTE="" # set when the update succeeded but the app will NOT reopen itself
@@ -267,8 +267,9 @@ start_ui() {
[ -n "$port" ] || { kill -9 "$UI_SERVER_PID" 2>/dev/null; UI_SERVER_PID=""; return; }
# Throwaway profile: new window/process we own; user's browser untouched.
UI_PROFILE_DIR="${TMPDIR:-/tmp}/hermes-update-ui-$$"
"$py" -c 'import os, signal, sys; os.setsid(); signal.signal(signal.SIGTERM, signal.SIG_DFL); os.execv(sys.argv[1], sys.argv[1:])' \
"$browser" --app="http://127.0.0.1:$port/" --user-data-dir="${TMPDIR:-/tmp}/hermes-update-ui-$$" \
"$browser" --app="http://127.0.0.1:$port/" --user-data-dir="$UI_PROFILE_DIR" \
--no-first-run --no-default-browser-check --window-size=280,320 >/dev/null 2>&1 &
UI_BROWSER_PID=$!
log "shim: app window on 127.0.0.1:$port"
@@ -290,7 +291,10 @@ stop_ui() { # error/manual outcomes keep the window up briefly so a watching
if [ -n "$UI_BROWSER_PID" ]; then
{ kill "$UI_BROWSER_PID" && wait "$UI_BROWSER_PID"; } 2>/dev/null
fi
rm -rf "${TMPDIR:-/tmp}/hermes-update-ui-$$" 2>/dev/null || true
if [ -n "$UI_PROFILE_DIR" ]; then
rm -rf "$UI_PROFILE_DIR" 2>/dev/null || true
UI_PROFILE_DIR=""
fi
UI_SERVER_PID="" UI_BROWSER_PID=""
}
@@ -0,0 +1,47 @@
"""The update hand-off cleans only the browser profile it launched."""
import os
from pathlib import Path
import subprocess
import pytest
@pytest.mark.linux_only
@pytest.mark.parametrize("failed", [False, True])
def test_shim_removes_owned_profile_on_exit(tmp_path, failed):
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
browser = bin_dir / "google-chrome"
browser.write_text(
'#!/bin/bash\ntrap "exit 0" TERM\n'
'for arg in "$@"; do\n'
'case "$arg" in --user-data-dir=*) dir="${arg#--user-data-dir=}" ;; esac\n'
'done\nmkdir -p "$dir"\nprintf "%s" "$dir" > "$HOME/launched-profile"\n'
'while :; do sleep 0.1; done\n', encoding="utf-8",
)
browser.chmod(0o755)
config = tmp_path / ".config"
config.mkdir()
(config / "mimeapps.list").write_text(
'[Default Applications]\nx-scheme-handler/http=google-chrome.desktop\n'
'x-scheme-handler/https=google-chrome.desktop\ntext/html=google-chrome.desktop\n',
encoding="utf-8",
)
sibling = tmp_path / "hermes-update-ui-unrelated"
sibling.mkdir()
(sibling / "keep").write_text("keep", encoding="utf-8")
install = tmp_path / "hermes-agent"
install.mkdir()
env = {**os.environ, "HOME": str(tmp_path), "HERMES_HOME": str(tmp_path),
"XDG_CONFIG_HOME": str(config), "TMPDIR": str(tmp_path),
"PATH": f"{bin_dir}:/usr/bin:/bin", "HERMES_SELFTEST_HOLD_SECONDS": "1",
"HERMES_UPDATE_SHIM_GRACE_SECONDS": "1", "HERMES_SELFTEST_FAIL": "1" if failed else "0"}
result = subprocess.run(
["bash", str(Path(__file__).resolve().parents[1] / "scripts/desktop-update/posix.sh"),
"--install-root", str(install), "--self-test-ui"],
env=env, capture_output=True, text=True, timeout=30,
)
assert result.returncode == (1 if failed else 0), result.stdout + result.stderr
owned = Path((tmp_path / "launched-profile").read_text(encoding="utf-8"))
assert not owned.exists()
assert (sibling / "keep").read_text(encoding="utf-8") == "keep"