Skip to content

The agent that grows with you

fix(update): require delegate publication before releasing handoff child

Teknium450b53fab106
diff --git a/scripts/desktop-update/marker.sh b/scripts/desktop-update/marker.sh
index 2318129ffc4..82c3aa14816 100644
--- a/scripts/desktop-update/marker.sh
+++ b/scripts/desktop-update/marker.sh
@@ -412,22 +412,22 @@ marker_claim() { # FIRST action of the daemon. Sets MARKER_BODY/CLAIMED.
esac
}
-marker_add_delegate_locked() { # pid ct -> name the update child as line 4 under OUR claim
- marker_read || return 0
+marker_add_delegate_locked() { # pid ct -> zero ONLY when the delegate was published
+ marker_read || return 1
marker_judge "$SEEN"
if [ "$M_PID" != "$MY_PID" ] || [ "$J_OWNER_STATE" -ne 0 ]; then
- log "update marker is no longer ours; no delegate written"; return 0
+ log "update marker is no longer ours; no delegate written"; return 1
fi
- if [ -n "$M_DPID" ] && [ "$M_DPID" != "$1" ] && [ "$J_DELEGATE_STATE" -eq 2 ]; then return 0; fi
+ if [ -n "$M_DPID" ] && [ "$M_DPID" != "$1" ] && [ "$J_DELEGATE_STATE" -eq 2 ]; then return 1; fi
marker_replace "$(marker_canonical "$M_PID" "$M_STARTED_DIGITS" "$M_CT" "$1" "$2")"$'\n' \
&& log "update marker names update pid $1 (ct $2) as its delegate"
}
marker_add_delegate() { # pid -> line 4 `delegate:<pid> ct:<ct>` (C1 rule 6)
local ct
- [ "$MARKER_CLAIMED" -eq 1 ] || return 0
+ [ "$MARKER_CLAIMED" -eq 1 ] || return 1
ct="$(proc_ct "$1")"
- [ -n "$ct" ] || { log "WARNING: no creation time for update pid $1; marker names no delegate"; return 0; }
+ [ -n "$ct" ] || { log "WARNING: no creation time for update pid $1; marker names no delegate"; return 1; }
marker_locked marker_add_delegate_locked "$1" "$ct"
}
diff --git a/scripts/desktop-update/posix.sh b/scripts/desktop-update/posix.sh
index 39be9246fb9..2913120063d 100755
--- a/scripts/desktop-update/posix.sh
+++ b/scripts/desktop-update/posix.sh
@@ -1092,13 +1092,21 @@ run_update() { # streams straight into the log (a killed run keeps its output);
while [ ! -e "$go" ]; do kill -0 "$MY_PID" 2>/dev/null || exit 70; sleep 0.05; done
exec "${UPDATE_INVOKE[@]}" update --yes $GATEWAY_FLAG $KEEP_STASH "${TARGET_ARGS[@]}" ) >> "$LOG" 2>&1 <&0 &
pid=$!
- marker_add_delegate "$pid"
- : > "$go"
- while :; do
- wait "$pid"; CODE=$?
- [ "$CODE" -ne 127 ] || break # not our child any more
- kill -0 "$pid" 2>/dev/null || break # reaped: CODE is its exit status
- done
+ if marker_add_delegate "$pid"; then
+ : > "$go"
+ while :; do
+ wait "$pid"; CODE=$?
+ [ "$CODE" -ne 127 ] || break # not our child any more
+ kill -0 "$pid" 2>/dev/null || break # reaped: CODE is its exit status
+ done
+ else
+ # No go-file: the child is still our parked shell, not an updater. Never
+ # let a timed-out/refused marker write turn this custody barrier fail-open.
+ kill -KILL "$pid" 2>/dev/null || true
+ wait "$pid" 2>/dev/null || true
+ CODE=3
+ log "update aborted: could not publish the update child as our marker delegate"
+ fi
rm -f "$go" 2>/dev/null
for sig in HUP INT QUIT; do trap "on_signal $sig" "$sig"; done
OUT="$(tail -c +"$(( ${offset:-0} + 1 ))" "$LOG" 2>/dev/null)"
diff --git a/tests/scripts/desktop_update/test_handoff_delegate_publication.py b/tests/scripts/desktop_update/test_handoff_delegate_publication.py
new file mode 100644
index 00000000000..5fb582bb30d
--- /dev/null
+++ b/tests/scripts/desktop_update/test_handoff_delegate_publication.py
@@ -0,0 +1,75 @@
+"""Exercise the real handoff's publication barrier with owned inert processes."""
+from __future__ import annotations
+
+import os
+from pathlib import Path
+import shlex
+import signal
+import subprocess
+import time
+
+import pytest
+
+fcntl = pytest.importorskip("fcntl")
+pytestmark = pytest.mark.platforms("linux")
+
+from tests.scripts.desktop_update.test_desktop_update_posix_handoff_protocol import _env
+from tests.scripts.desktop_update.test_desktop_update_posix_marker import POSIX, _calls, _install
+
+
[email protected]("claim", ["missing", "foreign"])
+def test_delegate_publication_refuses_without_our_claim(tmp_path, claim):
+ marker = tmp_path / "marker"
+ child = subprocess.Popen(["sleep", "60"])
+ try:
+ if claim == "foreign":
+ marker.write_text(f"{child.pid}\n{int(time.time())}\n")
+ original = marker.read_bytes() if marker.exists() else None
+ prelude = (f"log() {{ :; }}; MARKER={shlex.quote(str(marker))}; "
+ "DESKTOP_PID=0 HANDOFF_RUN='' MARKER_CLAIMED=1; "
+ f". {shlex.quote(str(POSIX.with_name('marker.sh')))}; "
+ f"marker_add_delegate {child.pid}")
+ result = subprocess.run(["bash", "-c", prelude], cwd=tmp_path, timeout=20)
+ assert result.returncode != 0, "a missing/foreign claim is not a published delegate"
+ assert (marker.read_bytes() if marker.exists() else None) == original
+ finally:
+ child.terminate()
+ child.wait(timeout=10)
+
+
+def test_sidecar_timeout_never_releases_the_parked_update(tmp_path):
+ # The installer fixture is an inert executable witness, not a real Hermes
+ # update. Production bash, locks, child creation and barrier all run unchanged.
+ home, install = _install(tmp_path)
+ marker = home / ".hermes-update-in-progress"
+ log = home / "logs" / "desktop-update-handoff.log"
+ script = subprocess.Popen(
+ ["bash", str(POSIX), "--daemonized", "--no-ui", "--install-root", str(install)],
+ env=_env(tmp_path, home), cwd=tmp_path, stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL, start_new_session=True)
+ lock_fd = None
+ try:
+ deadline = time.monotonic() + 30
+ while not (marker.exists() and marker.read_text().splitlines()[0] == str(script.pid)):
+ assert script.poll() is None and time.monotonic() < deadline
+ time.sleep(0.005)
+ lock_fd = os.open(str(marker) + ".lock", os.O_CREAT | os.O_RDWR, 0o600)
+ fcntl.flock(lock_fd, fcntl.LOCK_EX)
+ while "update marker lock is busy" not in (log.read_text() if log.exists() else ""):
+ assert script.poll() is None and time.monotonic() < deadline
+ time.sleep(0.01)
+ # Lock timeout is the actual production 10-second deadline, not a
+ # shortened/mocked predicate. Let the caller process the refused write.
+ deadline = time.monotonic() + 5
+ while script.poll() is None and "hermes update exit code:" not in log.read_text():
+ assert time.monotonic() < deadline
+ time.sleep(0.02)
+ assert not any(call.startswith("update --yes") for call in _calls(tmp_path)), _calls(tmp_path)
+ assert "hermes update exit code: 3" in log.read_text()
+ assert "delegate:" not in marker.read_text()
+ finally:
+ if lock_fd is not None:
+ os.close(lock_fd)
+ if script.poll() is None:
+ os.killpg(script.pid, signal.SIGKILL) # windows-footgun: ok — Linux process-group fixture
+ script.wait(timeout=10)