Skip to content

The agent that grows with you

fix(update): posix marker ops fail closed like marker-claim.ps1 on identity and removal

Teknium58512c167148
diff --git a/scripts/desktop-update/marker.sh b/scripts/desktop-update/marker.sh
index 82c3aa14816..3c1ba2856e4 100644
--- a/scripts/desktop-update/marker.sh
+++ b/scripts/desktop-update/marker.sh
@@ -83,6 +83,10 @@ ident_state() { # pid recorded-ct -> 0 same process, 1 gone/reused, 2 alive but
ct_close "$want" "$have" 2.0
}
+# ident_state 0 is marker-claim.ps1's Test-ProcessIdentityExact: both creation times known and
+# within 2 s. Adopting a run, withdrawing a bridge and reporting `taken` need exactly that; an
+# alive pid whose creation time cannot be read is never proof of who it is (fail closed).
+
ident_alive() { # pid recorded-ct -> 0 iff that process may still be running (unknown counts)
ident_state "$1" "${2:-}"; [ $? -ne 1 ]
}
@@ -353,7 +357,7 @@ marker_claim_locked() {
fi
if [ -n "$HANDOFF_RUN" ]; then
if [ "$verdict" = live ] && [ "$M_PID" = "$DESKTOP_PID" ] && [ "$J_OWNER_STATE" -eq 2 ] \
- && [ -n "$M_CT" ] && [ "$M_RUN" = "$HANDOFF_RUN" ] && [ "$J_DELEGATE_STATE" -eq 1 ]; then
+ && ident_state "$M_PID" "$M_CT" && [ "$M_RUN" = "$HANDOFF_RUN" ] && [ "$J_DELEGATE_STATE" -eq 1 ]; then
STARTED_AT="$M_STARTED" # one acquisition time for the whole chain
marker_take replace "$M_STARTED" || return 2
log "adopted the Desktop's update marker (desktop pid $DESKTOP_PID -> $MY_PID, run $HANDOFF_RUN)"
@@ -532,6 +536,11 @@ fcntl.flock(fd, fcntl.LOCK_UN)' "$path"; rc=$? ;;
}
# ── helper ops for the Desktop (it never mutates the marker itself) ──────────
+marker_remove() { # 0 iff the marker is gone afterwards (rm -f exits 0 when it cannot unlink)
+ rm -f "$MARKER" 2>/dev/null
+ [ ! -e "$MARKER" ] && [ ! -L "$MARKER" ]
+}
+
marker_op_reclaim_locked() {
marker_read || { echo absent; return 0; }
if [ -z "$SEEN" ] && marker_young_empty; then echo busy; return 0; fi
@@ -540,7 +549,7 @@ marker_op_reclaim_locked() {
live|ours) echo "live $J_OWNER" ;;
*)
if checkout_lock_held; then echo held; return 0; fi
- rm -f "$MARKER" 2>/dev/null
+ marker_remove || { echo busy; return 0; }
log "reclaimed a dead update marker for the Desktop (pid ${M_PID:-?})"
echo reclaimed ;;
esac
@@ -550,9 +559,11 @@ marker_op_withdraw_locked() {
marker_read || { echo absent; return 0; }
marker_judge "$SEEN"
if [ -z "$M_PID" ] || [ "$M_RUN" != "$HANDOFF_RUN" ]; then echo foreign; return 0; fi
- if [ "$M_PID" != "$DESKTOP_PID" ] && [ "$J_OWNER_STATE" -eq 2 ]; then echo "taken $M_PID"; return 0; fi
- if [ "$M_PID" = "$DESKTOP_PID" ] && [ "$J_OWNER_STATE" -eq 2 ] && [ -n "$M_CT" ]; then
- rm -f "$MARKER" 2>/dev/null
+ if [ "$M_PID" != "$DESKTOP_PID" ] && [ "$M_PID" != "$MY_PID" ] && ident_state "$M_PID" "$M_CT"; then
+ echo "taken $M_PID"; return 0
+ fi
+ if [ "$M_PID" = "$DESKTOP_PID" ] && ident_state "$M_PID" "$M_CT"; then
+ marker_remove || { echo busy; return 0; }
log "withdrew the Desktop's hand-off marker (run $HANDOFF_RUN)"
echo withdrawn; return 0
fi
diff --git a/tests/scripts/desktop_update/test_desktop_update_posix_handoff_protocol.py b/tests/scripts/desktop_update/test_desktop_update_posix_handoff_protocol.py
index 427eddb1e0f..9963a6d4c1f 100644
--- a/tests/scripts/desktop_update/test_desktop_update_posix_handoff_protocol.py
+++ b/tests/scripts/desktop_update/test_desktop_update_posix_handoff_protocol.py
@@ -231,6 +231,63 @@ def test_withdraw_removes_only_this_desktops_bridge_and_reports_a_taker(tmp_path
assert not marker.exists()
+# ── parity with marker-claim.ps1: identity-exact and checked removals ────────
+
+
+def _sourced(marker: Path, desktop: int, run: str, body: str, *, blind_ct: bool) -> str:
+ """marker.sh on its own; blind_ct = every other process's creation time is unreadable
+ (proc_ct prints nothing: a ps/proc read that failed), our own stays known."""
+ blind = 'proc_ct() { [ "$1" = "$MY_PID" ] && printf "%s\\n" "$MY_CT"; }; ' if blind_ct else ""
+ return (f"log() {{ :; }}; MARKER={shlex.quote(str(marker))} INSTALL_ROOT={shlex.quote(str(marker.parent))} "
+ f"DESKTOP_PID={desktop} HANDOFF_RUN={shlex.quote(run)} STARTED_AT=$(date +%s) MARKER_CLAIMED=0; "
+ f". {shlex.quote(str(MARKER_SH))}; MY_CT=\"$(proc_ct $$)\"; {blind}"
+ f"printf %s {shlex.quote(body)} > \"$MARKER\"; ")
+
+
[email protected]("blind_ct", [False, True], ids=["ct-readable", "ct-unreadable"])
+def test_adopt_withdraw_and_taken_need_an_exact_identity(tmp_path, procs, blind_ct):
+ """marker-claim.ps1 adopts a run, withdraws a bridge and reports `taken` only for
+ Test-ProcessIdentityExact (both creation times known). posix accepted an alive pid whose
+ creation time it could not read: that pid may be a reuse, so it must fail closed."""
+ home = tmp_path / "home"; home.mkdir()
+ marker = home / ".hermes-update-in-progress"
+ desktop = subprocess.Popen(["sleep", "60"]); procs.append(desktop)
+ other = subprocess.Popen(["sleep", "60"]); procs.append(other)
+ now = int(time.time())
+ bridge = f"{desktop.pid}\n{now}\nct:{_ct(desktop.pid)}\nrun:desk-7\n"
+ taken = f"{other.pid}\n{now}\nct:{_ct(other.pid)}\nrun:desk-7\n"
+
+ def run(body: str, script: str) -> str:
+ code = _sourced(marker, desktop.pid, "desk-7", body, blind_ct=blind_ct) + script
+ return subprocess.run(["bash", "-c", code], capture_output=True, text=True, encoding="utf-8", timeout=30).stdout.strip()
+
+ adopt = run(bridge, 'marker_locked marker_claim_locked; echo "rc=$?"')
+ assert adopt.splitlines()[-1] == ("rc=1" if blind_ct else "rc=0"), adopt
+ assert run(taken, "marker_op withdraw") == ("foreign" if blind_ct else f"taken {other.pid}")
+ assert marker.read_text(encoding="utf-8-sig") == taken
+ assert run(bridge, "marker_op withdraw") == ("foreign" if blind_ct else "withdrawn")
+ assert marker.exists() is blind_ct
+
+
[email protected](os.geteuid() == 0, reason="root unlinks inside a read-only directory")
+def test_reclaim_reports_busy_when_the_dead_marker_cannot_be_removed(tmp_path):
+ """marker-claim.ps1 answers `busy` when Remove-MarkerLocked fails; posix printed `reclaimed`
+ after an `rm -f` that could not unlink, so the Desktop went on as if the marker were gone."""
+ home, install = _install(tmp_path)
+ marker = home / ".hermes-update-in-progress"
+ dead = f"99999999\n{int(time.time())}\nct:1.000\n"
+ marker.write_text(dead, encoding="utf-8")
+ Path(str(marker) + ".lock").touch()
+ home.chmod(0o555)
+ try:
+ assert _helper(tmp_path, home, install, "reclaim") == "busy"
+ assert marker.read_text(encoding="utf-8-sig") == dead
+ finally:
+ home.chmod(0o755)
+ assert _helper(tmp_path, home, install, "reclaim") == "reclaimed"
+ assert not marker.exists()
+
+
# ── R6: a survivor holding the checkout lock keeps the marker ───────────────