Skip to content

agmsgd: the sync engine takes a lifetime lock; a new agmsgd ends only its own previous child - #2146

Merged
fujibee merged 5 commits into
integration/1.10-cloudfrom
feat/agmsgd-sync-lifetime-lock
Dec 1, 2026
Merged

fujibee merged 5 commits into
integration/1.10-cloudfrom
feat/agmsgd-sync-lifetime-lock

Conversation

@fujibee

@fujibee fujibee commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Fourth step of running the sync engine inside agmsgd: the writer fence. Until now two engines for one team were kept apart only by check-then-start (the pidfile, and the supervisor's held_elsewhere). This makes the engine itself refuse to be a second one, and lets a new agmsgd end the engine its dead predecessor left behind.

Decided 2026-11-30: running engines are handed over to agmsgd in agmsgd mode; an engine started elsewhere exits with a stated line naming agmsgd and the unit to disable; standalone stays an explicit mode.

What changes

  • scripts/internal/engine-lock.mjs: the engine's lifetime lock, run/remote-sync.<team>.lifetime (0600, published complete with link(2) from a staged file so it is never visible half written, one-line JSON: pid, token, host, start time, command, and the supervising agmsgd's pid and start time when agmsgd started it). remote-sync.mjs run takes it before it does anything else, for every launcher alike (agmsgd's child, a session start, a user unit, an old script that reaches this code). If a live engine for the team holds it, this one ends with a stated line and exit 14 instead of becoming a second writer. It never waits.
  • Stale rule (a hard kill or Windows kill leaves the file): the holder counts only if its pid is alive AND its command line is this team's engine (remote-sync.mjs run with --team <team> among its arguments, in any order, with or without other options); a reused pid does not hold the lock; an unreadable command line counts as held (a live pid is never replaced on a guess). A stale record is replaced under a short mkdir mutex with a token compare, so two contenders replacing it at once cannot both win. The lock is released on every ordinary exit, only if the token is still ours.
  • agmsgd: the supervised child carries AGMSG_SYNC_SUPERVISED_BY=<pid>:<start>, which lands in the lock record. A new agmsgd that finds an engine whose lock names a supervisor that is no longer running ends that engine (SIGTERM, SIGKILL after the grace), then starts its own. An engine whose lock names no supervisor, or a supervisor that is still alive, is never touched: it stays foreign. Exit 14 from a child shows as held_elsewhere.
  • sync stop of agmsgd already ended every child and adopted none (agmsgd: supervise the sync engine as one child process per connected team #2130); this closes the hard-kill case.

What is not changed, and why

once does not take the lifetime lock. It is what maintenance commands (reprocess, resync, quarantine, fetch round trips) call, often while an engine runs; making it refuse would break those flows. A once and a run cycle for one team are kept apart by the storage driver's SQLite transactions and the wire-id idempotence, exactly as before this PR (the team config lock guards only binding alignment, not cycles). The new tests/test_remote_concurrent_cycles.bats is a first check of that, not a proof of it: it starts four cycles at once and counts what the fixture server received, but it does not yet assert each call's result, show that the calls overlapped, or leave out the serial once that runs before the count. A follow-up makes it prove the overlap and assert every call; until then it does not demonstrate the guarantee.

Spawn count (counted from the code, not measured)

  • Engine start: no process added; a few file operations (stage, link, unlink, and on contention one read plus one ps -o command= -p <pid> on macOS, a /proc read on Linux).
  • Daemon poll per team: no process added in the steady state (one lock-file read). Deciding whether a predecessor's engine is to be ended reads its command line once: one ps on macOS, and one more per poll tick while it is being ended.

Stops and refusals

An engine that finds a live engine for its team ends with exit 14 and a line naming the holder; recovery is to stop that engine (remote.sh sync stop <team> or remote.sh sync mode standalone). A new agmsgd ends only an engine whose lock names a dead agmsgd as its supervisor. Both follow the 2026-11-30 decision above.

Not done here

  • Windows: the stop of an engine is still a hard kill; the stale rule above is what recovers its lock, covered by unit tests with fake liveness, not run on Windows.
  • Engines of older versions do not take the lock; the supervisor's pidfile check remains for them.
  • No end-to-end run against a real agmsgd and a live server.

Tests

tests/engine_lifetime_lock.test.mjs (10: second engine refused then allowed after release, per-team independence, hard-kill stale record replaced, reused pid, unreadable command line, the real engine releasing on exit, a new agmsgd ending its dead predecessor's child, a foreign or live-supervised engine never ended, restart sequence, a record never visible half written and an unreadable fresh one not stolen, an engine with other options in any argument order); tests/test_remote_concurrent_cycles.bats (1, a first check only, see above); the four check-*.sh gates at baseline.

@fujibee
fujibee merged commit 6a1de2e into integration/1.10-cloud Dec 1, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant