fix(replace,wm,autostart): preserve workspaces across slow/3rd-party replace and guard polkit agent startup

This commit is contained in:
Luxferre committed 2026-11-27 09:06:02 +02:00
1 parent 4ec3f59e02
commit c5ce065c33
6 files changed
+759 -53

No files matched your search

+120
View File
@@ -512,6 +512,125 @@ def test_dwm_sighup_reload_xvfb():
terminate(xvfb)
def test_xdg_desktop_dedup_and_polkit_guard():
with tempfile.TemporaryDirectory() as tmp:
env, _, xdg_home, xdg_sys, _, bin_dir = make_base_env(tmp)
conf_dir = os.path.join(xdg_home, "anode")
markers = os.path.join(tmp, "markers")
os.makedirs(markers, exist_ok=True)
app_bin = os.path.join(bin_dir, "anode_xdg_app")
compile_sleep_dummy(app_bin)
lxpolkit_bin = os.path.join(bin_dir, "lxpolkit")
shutil.copy2(app_bin, lxpolkit_bin)
m_app = os.path.join(markers, "app.pid")
m_pre_app = os.path.join(markers, "pre_app.pid")
m_xdg_dup = os.path.join(markers, "xdg_dup.pid")
m_polkit_pre = os.path.join(markers, "polkit_pre.pid")
m_polkit_new = os.path.join(markers, "polkit_new.pid")
m_polkit_bad_sess = os.path.join(markers, "polkit_bad_sess.pid")
# 1) exec_once + XDG .desktop of the same app in a single start must only launch once
write_text(os.path.join(conf_dir, "autostart"), f"exec_once anode_xdg_app '{m_app}'\n")
write_text(
os.path.join(xdg_sys, "autostart", "app.desktop"),
f"[Desktop Entry]\nType=Application\nTryExec=anode_xdg_app\nExec=anode_xdg_app '{m_app}'\n",
)
res = run_autostart(["start"], env)
assert res.returncode == 0, f"start failed: {res.stderr}"
assert wait_until(lambda: os.path.exists(m_app), timeout=4.0), "anode_xdg_app did not launch"
time.sleep(0.35)
try:
with open(m_app, "r", encoding="utf-8") as f:
pids = [line.strip() for line in f if line.strip()]
assert len(pids) == 1, f"expected 1 launch for exec_once + XDG .desktop, got {pids}"
finally:
kill_pids_from_file(m_app)
# Wait briefly for previous process cleanup before testing pre-running XDG dedup
time.sleep(0.15)
# 2) Already-running process referenced by XDG .desktop must be skipped
write_text(os.path.join(conf_dir, "autostart"), "# empty\n")
write_text(
os.path.join(xdg_sys, "autostart", "app.desktop"),
f"[Desktop Entry]\nType=Application\nTryExec=anode_xdg_app\nExec=anode_xdg_app '{m_xdg_dup}'\n",
)
pre_app = subprocess.Popen([app_bin, m_pre_app], env=env)
try:
assert wait_until(lambda: os.path.exists(m_pre_app), timeout=3.0)
res2 = run_autostart(["start"], env)
assert res2.returncode == 0, f"start failed: {res2.stderr}"
time.sleep(0.35)
assert not os.path.exists(m_xdg_dup), "XDG .desktop launched duplicate of already-running app"
finally:
terminate(pre_app)
kill_pids_from_file(m_xdg_dup)
# 3) Polkit agent running on DISPLAY=:97 in sess-active must block duplicate on :97 even from stale XDG_SESSION_ID
os.remove(os.path.join(xdg_sys, "autostart", "app.desktop"))
write_text(os.path.join(conf_dir, "autostart"), f"exec_once lxpolkit '{m_polkit_new}'\n")
write_text(
os.path.join(xdg_sys, "autostart", "lxpolkit.desktop"),
f"[Desktop Entry]\nType=Application\nTryExec=lxpolkit\nExec=lxpolkit '{m_polkit_new}'\n",
)
active_env = dict(env)
active_env["DISPLAY"] = ":97"
active_env["XDG_SESSION_ID"] = "sess-active"
active_env["DBUS_SESSION_BUS_ADDRESS"] = "unix:path=/tmp/anode-active-dbus"
stale_env = dict(env)
stale_env["DISPLAY"] = ":97"
stale_env["XDG_SESSION_ID"] = "sess-stale"
stale_env["DBUS_SESSION_BUS_ADDRESS"] = "unix:path=/tmp/anode-stale-dbus"
stale_env["ANODE_FORCE_POLKIT_AGENT"] = "1"
pre_pk = subprocess.Popen([lxpolkit_bin, m_polkit_pre], env=active_env)
try:
assert wait_until(lambda: os.path.exists(m_polkit_pre), timeout=3.0)
res3 = run_autostart(["start"], stale_env)
assert res3.returncode == 0, f"polkit dedup run failed: {res3.stderr}"
time.sleep(0.35)
assert not os.path.exists(m_polkit_new), (
"duplicate polkit agent was started on same DISPLAY across different XDG_SESSION_ID"
)
finally:
terminate(pre_pk)
kill_pids_from_file(m_polkit_new)
# 4) Dead/invalid loginctl session must skip launching polkit agent
loginctl_stub = os.path.join(bin_dir, "loginctl")
write_text(
loginctl_stub,
"#!/bin/sh\n"
"if [ \"${1:-}\" = \"list-sessions\" ]; then\n"
" printf 'SESSION UID USER SEAT\\n c11 1000 lux seat0\\n'\n"
" exit 0\n"
"fi\n"
"exit 1\n",
0o755,
)
write_text(os.path.join(conf_dir, "autostart"), f"exec_once lxpolkit '{m_polkit_bad_sess}'\n")
write_text(
os.path.join(xdg_sys, "autostart", "lxpolkit.desktop"),
f"[Desktop Entry]\nType=Application\nTryExec=lxpolkit\nExec=lxpolkit '{m_polkit_bad_sess}'\n",
)
bad_sess_env = dict(env)
bad_sess_env["DISPLAY"] = ":98"
bad_sess_env["XDG_SESSION_ID"] = "sess-dead"
bad_sess_env.pop("ANODE_FORCE_POLKIT_AGENT", None)
res4 = run_autostart(["start"], bad_sess_env)
assert res4.returncode == 0, f"dead-session guard run failed: {res4.stderr}"
time.sleep(0.35)
try:
assert not os.path.exists(m_polkit_bad_sess), (
"polkit agent launched despite invalid loginctl session"
)
finally:
kill_pids_from_file(m_polkit_bad_sess)
if __name__ == "__main__":
test_directives_includes_once_and_reload()
test_exec_once_session_isolation()
@@ -519,4 +638,5 @@ if __name__ == "__main__":
test_dropins_desktop_filters_and_xdg_masking()
test_check_errors()
test_dwm_sighup_reload_xvfb()
test_xdg_desktop_dedup_and_polkit_guard()
print("test_autostart: OK")