settle: wait out a restart this pass did not make (#452)
On Tito production (IE, v0.28.15) carlos-stage rolled back a healthy titogo build, 2bf0a40, as "did not come up: systemd restarted it (new invocation)". titogo's control daemon had restarted the same unit at the same second as the staging pass. The refusal then stuck for the whole box until the next merge. settleUnit fixed on the first new invocation it saw and failed on any later one. A later invocation with NRestarts still 0 was not started by systemd's Restart=: a crash-restart carrying NRestarts already fails at once, on the check above the switch. So that second invocation is somebody else's restart, not a verdict on the build. The check now moves onto it and applies the same proof for the rest of the window (active, MainPID's exe under the new version's directory, Ready, or a Hold counted from the new invocation). A failed unit, a crash-restart, the old build still running at the deadline, or a build that never answers all still fail. Any failure after such a restart says "restarted N time(s) by something other than this pass, not by systemd's Restart=", so a clash no longer reads as a crash. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
settleUnit treats a second invocation with NRestarts 0 as a foreign restart: it re-baselines and keeps checking until the deadline instead of failing at once. Failure messages name the foreign restart.
Agent notes
All settleUnit callers benefit: agent checked restart, rollback restart, legacy and root restart requests. The activator and instancectl 'new invocation' hits are separate mechanisms and are unchanged.
internal/host/unitsettle.go+27 −1internal/host/unitsettle_test.go+105 −0
Intent
Fix carlos/platform#452 overnight while Paul is asleep: carlos-stage rolled back a healthy titogo release (2bf0a40) as "did not come up" because titogo-controld restarted the same unit at the same second as the staging pass ("systemd restarted it (new invocation)"). The staging/adopt "did it come up" check should survive a restart it didn't make: if the unit is active, running the new target, and healthy by whatever proof the check already uses, a restart by something else onto the same new build counts as a pass. If the invocation changes mid-check, keep checking for the remaining time instead of failing at once. Only fail when the new build genuinely doesn't come up (crash loop, or still running the old build at the deadline). The failure message should say when the extra invocation wasn't a crash-restart, or at least stop implying a crash. Fix the root cause in the check itself, not by special-casing titogo. Don't widen into a redesign. TDD; independent review from Astra via codex; push the branch and link it from a reply on #452. Do not merge, tag, release, roll, or touch production.
diff --git a/internal/host/unitsettle.go b/internal/host/unitsettle.go| index 54767a4b1..04d5def91 100644 |
| --- a/internal/host/unitsettle.go |
| +++ b/internal/host/unitsettle.go |
| @@ -178,6 +178,15 @@ var errUnitStopped = errors.New("unit was stopped during settle") |
| // InvocationID before the restart; "" means the unit was not running, and an |
| // inactive unit is then "nothing started", not a failure. Otherwise a unit |
| // that goes inactive/dead returns errUnitStopped. |
| +// |
| +// A second new invocation that systemd's own Restart= did not start |
| +// (NRestarts still 0) is somebody else restarting the unit: an operator, or |
| +// a supervisor of the app's own that restarts its units when their live link |
| +// moves (discussion #452, titogo's control daemon). That is not a verdict on |
| +// the build. The check moves onto the new invocation and keeps applying the |
| +// same proof (active, MainPID running the new build, Ready or Hold) for the |
| +// rest of the window; only a crash-restart, a failed unit, or a deadline that |
| +// still sees no healthy new build fails it. |
| func settleUnit(sys unitSystem, unit, beforeInvocation string, o settleOpts) error { |
| deadline := o.Now().Add(o.Timeout) |
| var inv string // the new invocation, once seen active |
| @@ -192,7 +201,14 @@ func settleUnit(sys unitSystem, unit, beforeInvocation string, o settleOpts) err |
| // treated like not-ready, and only a deadline that still sees it fails. |
| var wrongExe string |
| var binCheckSkipped bool // permission error on Exe; skip binary check for rest of settle |
| + // foreign counts invocations that neither this pass nor systemd's |
| + // Restart= started, for the failure message: an operator reading |
| + // "did not come up" must be able to tell a clash from a crash. |
| + var foreign int |
| notUp := func(reason string) error { |
| + if foreign > 0 { |
| + reason += fmt.Sprintf("; during the check it was also restarted %d time(s) by something other than this pass, not by systemd's Restart=", foreign) |
| + } |
| return &release.UnitNotUpError{Unit: unit, Reason: reason, Journal: sys.JournalTail(unit, 20)} |
| } |
| for { |
| @@ -227,7 +243,17 @@ func settleUnit(sys unitSystem, unit, beforeInvocation string, o settleOpts) err |
| case p.ActiveState == "failed": |
| return notUp("unit entered failed state") |
| case inv != "" && p.InvocationID != inv: |
| - return notUp("systemd restarted it (new invocation)") |
| + // Not systemd's Restart=: a fresh invocation carrying NRestarts |
| + // returned above. Somebody else restarted the unit (#452). Watch |
| + // the new invocation instead, from scratch: Hold is "active this |
| + // long in ONE invocation", so the clock starts again, and the |
| + // exe and Ready checks below judge the process now running. |
| + foreign++ |
| + log.Printf("adopt: %s: restarted by something other than this pass (invocation %q, ours %q); still checking the new build", unit, p.InvocationID, inv) |
| + inv, activeSince = "", time.Time{} |
| + if p.ActiveState == "active" && p.InvocationID != "" { |
| + inv, activeSince = p.InvocationID, o.Now() |
| + } |
| case inv != "" && p.NRestarts > 0: |
| return notUp(fmt.Sprintf("systemd restarted it %d time(s)", p.NRestarts)) |
| case inv == "" && p.ActiveState == "active" && p.InvocationID != beforeInvocation: |
diff --git a/internal/host/unitsettle_test.go b/internal/host/unitsettle_test.go| index a6b37e81b..c651d2b5a 100644 |
| --- a/internal/host/unitsettle_test.go |
| +++ b/internal/host/unitsettle_test.go |
| @@ -540,3 +540,108 @@ func TestRollbackRestartStopDuringSettleIsNotJudged(t *testing.T) { |
| t.Fatalf("restarts = %v, want the one reset-failed and restart only", f.restarts) |
| } |
| } |
| + |
| +// readyAfter fails its first n calls, then answers healthy: a socket that is |
| +// not listening yet on the first polls of an invocation. |
| +func readyAfter(n int) func() error { |
| + calls := 0 |
| + return func() error { |
| + calls++ |
| + if calls <= n { |
| + return errors.New("connect: no such file or directory") |
| + } |
| + return nil |
| + } |
| +} |
| + |
| +// Discussion #452: titogo's control daemon restarted the unit at the same |
| +// second as the staging pass. The pass fixed on its own invocation, saw a |
| +// second one it did not start, and rolled a healthy build back. A restart |
| +// that systemd did not make (NRestarts 0) onto the same new build is not a |
| +// crash: the check keeps watching the new invocation and passes on the |
| +// same proof it always used. |
| +func TestSettleSurvivesAForeignRestartOntoTheNewBuild(t *testing.T) { |
| + f := &fakeUnits{shows: []unitProps{ |
| + {ActiveState: "active", SubState: "running", InvocationID: "ours", MainPID: 7}, |
| + {ActiveState: "activating", SubState: "start", InvocationID: "theirs"}, |
| + {ActiveState: "active", SubState: "running", InvocationID: "theirs", MainPID: 8}, |
| + }, exe: map[int]string{7: "/opt/carlos/versions/new/titogo", 8: "/opt/carlos/versions/new/titogo"}} |
| + if err := settleUnit(f, "titogo-canary@x.service", "old", opts(readyAfter(1))); err != nil { |
| + t.Fatalf("settle = %v, want healthy: a restart by another supervisor onto the new build is a pass", err) |
| + } |
| +} |
| + |
| +// The no-probe form: Hold is "active this long in ONE invocation", so a |
| +// foreign restart starts the hold again rather than passing on time the |
| +// replaced invocation accrued. |
| +func TestSettleForeignRestartRestartsTheHold(t *testing.T) { |
| + f := &fakeUnits{shows: []unitProps{ |
| + {ActiveState: "active", InvocationID: "ours", MainPID: 7}, |
| + {ActiveState: "active", InvocationID: "ours", MainPID: 7}, |
| + {ActiveState: "active", InvocationID: "ours", MainPID: 7}, |
| + {ActiveState: "active", InvocationID: "theirs", MainPID: 8}, |
| + }, exe: map[int]string{7: "/opt/carlos/versions/new/titogo", 8: "/opt/carlos/versions/new/titogo"}} |
| + o := opts(nil) |
| + start := o.Now() |
| + if err := settleUnit(f, "titogo-canary@x.service", "old", o); err != nil { |
| + t.Fatalf("settle = %v, want healthy after a full hold in the new invocation", err) |
| + } |
| + // "theirs" is first seen on the 4th poll, at 1.5s. |
| + if got := o.Now().Sub(start); got < 1500*time.Millisecond+5*time.Second { |
| + t.Fatalf("settled after %v, want the 5s hold counted from the foreign invocation", got) |
| + } |
| +} |
| + |
| +// Waiting a foreign restart out is not a pass on its own: the new build must |
| +// still come up. Onto the OLD build, the deadline fails naming the exe, and |
| +// says the extra invocation was not a crash-restart. |
| +func TestSettleForeignRestartOntoTheOldBuildFails(t *testing.T) { |
| + f := &fakeUnits{shows: []unitProps{ |
| + {ActiveState: "active", InvocationID: "ours", MainPID: 7}, |
| + {ActiveState: "active", InvocationID: "theirs", MainPID: 8}, |
| + }, exe: map[int]string{7: "/opt/carlos/versions/new/titogo", 8: "/opt/carlos/versions/old/titogo"}} |
| + var nu *release.UnitNotUpError |
| + err := settleUnit(f, "titogo-canary@x.service", "old", opts(readyAfter(1))) |
| + if !errors.As(err, &nu) || !strings.Contains(nu.Reason, "running /opt/carlos/versions/old/titogo, not the new build") { |
| + t.Fatalf("settle = %v, want a deadline failure naming the old build", err) |
| + } |
| + if !strings.Contains(nu.Reason, "restarted 1 time(s) by something other than this pass") { |
| + t.Fatalf("reason = %q, want it to say the extra invocation was not this pass's or systemd's", nu.Reason) |
| + } |
| + if strings.Contains(nu.Reason, "systemd restarted it") { |
| + t.Fatalf("reason = %q reads as a crash-restart", nu.Reason) |
| + } |
| +} |
| + |
| +// A foreign restart onto a build that never answers fails at the deadline as |
| +// not-ready, carrying the same note. |
| +func TestSettleForeignRestartThenNeverReady(t *testing.T) { |
| + f := &fakeUnits{shows: []unitProps{ |
| + {ActiveState: "active", InvocationID: "ours", MainPID: 7}, |
| + {ActiveState: "active", InvocationID: "theirs", MainPID: 8}, |
| + }, exe: map[int]string{7: "/opt/carlos/versions/new/titogo", 8: "/opt/carlos/versions/new/titogo"}} |
| + var nu *release.UnitNotUpError |
| + err := settleUnit(f, "titogo-canary@x.service", "old", opts(func() error { return errors.New("dial: refused") })) |
| + if !errors.As(err, &nu) || !strings.Contains(nu.Reason, "not ready after 30s") || |
| + !strings.Contains(nu.Reason, "restarted 1 time(s) by something other than this pass") { |
| + t.Fatalf("settle = %v, want not-ready at the deadline noting the foreign restart", err) |
| + } |
| +} |
| + |
| +// A crash loop is still a crash loop after a foreign restart: systemd's own |
| +// Restart= bumps NRestarts, and that fails at once as it always did. |
| +func TestSettleCrashLoopAfterAForeignRestartStillFails(t *testing.T) { |
| + f := &fakeUnits{shows: []unitProps{ |
| + {ActiveState: "active", InvocationID: "ours", MainPID: 7}, |
| + {ActiveState: "active", InvocationID: "theirs", MainPID: 8}, |
| + {ActiveState: "activating", SubState: "auto-restart", InvocationID: "crash", NRestarts: 1}, |
| + }, exe: map[int]string{7: "/opt/carlos/versions/new/titogo", 8: "/opt/carlos/versions/new/titogo"}} |
| + var nu *release.UnitNotUpError |
| + err := settleUnit(f, "titogo-canary@x.service", "old", opts(func() error { return errors.New("refused") })) |
| + if !errors.As(err, &nu) || !strings.Contains(nu.Reason, "systemd restarted it 1 time(s)") { |
| + t.Fatalf("settle = %v, want the crash-restart caught", err) |
| + } |
| + if f.n != 3 { |
| + t.Fatalf("settle took %d polls, want it to fail on the 3rd (no waiting out a crash)", f.n) |
| + } |
| +} |