Repository navigation
agmsgd: the sync engine takes a lifetime lock; a new agmsgd ends only its own previous child - #2146
Merged
Merged
Conversation
…a new agmsgd ends only its own previous child (WIP)
…t/agmsgd-sync-lifetime-lock
…wrapper for the lifetime lock tests
…engine is recognised by its arguments in any order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 runtakes 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.remote-sync.mjs runwith--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 shortmkdirmutex 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.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 asheld_elsewhere.sync stopof 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
oncedoes 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. Aonceand aruncycle 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 newtests/test_remote_concurrent_cycles.batsis 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 serialoncethat 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)
ps -o command= -p <pid>on macOS, a/procread on Linux).pson 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>orremote.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
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 fourcheck-*.shgates at baseline.