carlos / platform Public

host, systemd: temporary not-up for an unobserved settle; whole-file guard

Astra round 2 on bounded-systemctl, the centralized fixes for its two
P2s (stopping rule 2; Paul chose this option).

settleUnit's deadline reached while systemctl show kept failing now
returns unobservedNotUp: one error that is a *release.UnitNotUpError
(errors.As) and is Temporary(). Adoption checks the two separately, so
the route still gets the rollback restart, whose reset-failed and
restart replace the process, and the version is no longer poisoned for
the rest of the pass over an observation that never happened. A failure
settle saw (failed, restarted, wrong executable, not ready) is a plain,
non-temporary UnitNotUpError, as before. TestTimedOutSettleRecovers-
WithoutPoisoning pins both through release.Pass.

The guard's local-shell rule now runs in one traversal of the whole
file, with every string binding in the file and package: function
literals, methods, package var initializers and init are covered. New
fixtures: a package-level func literal running sh -c "systemctl show",
one reaching a package-level command string through a var, a package
var initializer and init.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 0fa550f81cf512b4754c366cca30b84e0e1f1431 parent bd3a6f3
4 files changed, +118 −52
  • internal/host/settle_verdict_adopt_test.go +20 −0
  • internal/host/unitsettle.go +24 −5
  • internal/host/unitsettle_test.go +20 −3
  • internal/systemd/guard_test.go +54 −44
diff --git a/internal/host/settle_verdict_adopt_test.go b/internal/host/settle_verdict_adopt_test.go
index d8fb123cc..c38c8cc4a 100644
--- a/internal/host/settle_verdict_adopt_test.go
+++ b/internal/host/settle_verdict_adopt_test.go
@@ -10,6 +10,7 @@ import (
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/systemd"
)
// settleOutcomeThroughAdoption runs one adoption pass of v2 over two
@@ -93,3 +94,22 @@ func TestFailedSettleStillPoisonsAndRecovers(t *testing.T) {
t.Fatalf("live = %v, want %s back on v1", live, restarted[0])
}
}
+
+// A settle that timed out observing the unit, through adoption: it is still
+// not up, so the route gets the rollback restart (reset-failed and restart,
+// which replaces whatever runs) and never the plain start; and it is
+// temporary, so v2 is not poisoned and the second route still adopts it.
+func TestTimedOutSettleRecoversWithoutPoisoning(t *testing.T) {
+ timedOut := settleUnit(&showErrs{fakeUnits: &fakeUnits{}, err: &systemd.TimeoutError{Program: "systemctl", Verb: "show", Units: []string{"console-a.service"}, After: systemd.QueryTimeout}},
+ "console-a.service", "old", opts(func() error { return nil }))
+ restarted, recovered, started, live := settleOutcomeThroughAdoption(t, timedOut)
+ if len(restarted) != 2 {
+ t.Fatalf("restarted %v, want both routes: a timed-out observation must not poison v2 for the pass", restarted)
+ }
+ if !slices.Equal(recovered, restarted[:1]) || len(started) != 0 {
+ t.Fatalf("recovered %v, started %v; want the rollback restart for %s and never a plain start", recovered, started, restarted[0])
+ }
+ if live[restarted[0]] != "v1" || live[restarted[1]] != "v2" {
+ t.Fatalf("live = %v, want %s back on v1 and %s on v2", live, restarted[0], restarted[1])
+ }
+}
diff --git a/internal/host/unitsettle.go b/internal/host/unitsettle.go
index f0bdfbe62..7ad773f4d 100644
--- a/internal/host/unitsettle.go
+++ b/internal/host/unitsettle.go
@@ -178,6 +178,23 @@ type settleOpts struct {
// nothing and restarts nothing.
var errUnitStopped = errors.New("unit was stopped during settle")
+// unobservedNotUp is settleUnit's answer when its deadline passed while
+// systemctl show kept failing (refused, a D-Bus error, killed at its
+// QueryTimeout): ONE error that adoption reads two ways. It is a
+// *release.UnitNotUpError (errors.As), so adoption gives the route the
+// not-up recovery, whose reset-failed and restart replace whatever runs.
+// And it is temporary, because nothing was seen to fail, so adoption does
+// not poison the version for the rest of the pass. A failure settle did see
+// (failed, restarted, the wrong executable, not ready) is a plain
+// UnitNotUpError, not temporary.
+type unobservedNotUp struct {
+ *release.UnitNotUpError
+ show error // the last show's error
+}
+
+func (e *unobservedNotUp) Unwrap() []error { return []error{e.UnitNotUpError, e.show} }
+func (e *unobservedNotUp) Temporary() bool { return true }
+
// settleUnit watches unit after a try-restart. beforeInvocation is the
// InvocationID before the restart; "" means the unit was not running, and an
// inactive unit is then "nothing started", not a failure. Otherwise a unit
@@ -205,15 +222,17 @@ func settleUnit(sys unitSystem, unit, beforeInvocation string, o settleOpts) err
// One failed `systemctl show` says nothing about the build (a
// busy bus, a timed-out D-Bus call, a show killed at its
// QueryTimeout). Retried until the deadline, and only a deadline
- // that still cannot observe the unit fails, as UnitNotUpError:
- // its not-up recovery (reset-failed and restart) replaces
- // whatever runs. Reading this as an unknown outcome instead
- // belongs to the outcome-reconciliation follow-up (amadan
+ // that still cannot observe the unit fails: as a temporary
+ // UnitNotUpError (unobservedNotUp), so the not-up recovery
+ // (reset-failed and restart) replaces whatever runs and the
+ // version is not poisoned. Reading this as an unknown outcome
+ // instead belongs to the outcome-reconciliation follow-up (amadan
// discussion #1214): adoption would roll the symlink back and
// plain-start the unit, which leaves a new process that did come
// up running under the old link.
if !o.Now().Before(deadline) {
- return notUp(fmt.Sprintf("cannot observe %s: %v", unit, err))
+ return &unobservedNotUp{show: err, UnitNotUpError: &release.UnitNotUpError{
+ Unit: unit, Reason: fmt.Sprintf("cannot observe %s: %v", unit, err), Journal: sys.JournalTail(unit, 20)}}
}
o.Sleep(o.Poll)
continue
diff --git a/internal/host/unitsettle_test.go b/internal/host/unitsettle_test.go
index e8ec62f13..3adcf1c52 100644
--- a/internal/host/unitsettle_test.go
+++ b/internal/host/unitsettle_test.go
@@ -193,9 +193,11 @@ func TestSettleRetriesATransientShowError(t *testing.T) {
// unit is a UnitNotUpError ("cannot observe"), whether the show was refused
// or killed at its QueryTimeout: the not-up recovery (reset-failed and
// restart) then replaces whatever runs, so the restored symlink and the
-// running process agree. Bounding the query must not change that verdict;
-// reading it as an unknown outcome waits for the outcome-reconciliation
-// follow-up (amadan discussion #1214).
+// running process agree. The same error is temporary, because nothing was
+// seen to fail: adoption does not poison the version over it. A failure
+// settle did see (here, the unit failed) is a UnitNotUpError that is not
+// temporary. Reading the unobserved case as an unknown outcome instead
+// waits for the outcome-reconciliation follow-up (amadan discussion #1214).
func TestSettleShowErrorAtTheDeadline(t *testing.T) {
for name, showErr := range map[string]error{
"refused": errors.New("Access denied"),
@@ -207,9 +209,24 @@ func TestSettleShowErrorAtTheDeadline(t *testing.T) {
if !errors.As(err, &nu) || nu.Reason != "cannot observe console.service: "+showErr.Error() {
t.Errorf("%s: settle = %v, want a deadline UnitNotUpError saying the unit could not be observed", name, err)
}
+ if !temporary(err) {
+ t.Errorf("%s: settle = %v, want it temporary: nothing was seen to fail", name, err)
+ }
+ }
+ failed := settleUnit(&fakeUnits{shows: []unitProps{{ActiveState: "failed", SubState: "failed", InvocationID: "new"}}},
+ "console.service", "old", opts(func() error { return nil }))
+ var nu *release.UnitNotUpError
+ if !errors.As(failed, &nu) || temporary(failed) {
+ t.Errorf("failed unit: settle = %v, want a UnitNotUpError that is not temporary", failed)
}
}
+// temporary is the check adoption makes before it poisons a version.
+func temporary(err error) bool {
+ var t interface{ Temporary() bool }
+ return errors.As(err, &t) && t.Temporary()
+}
+
type showErrs struct {
*fakeUnits
err error
diff --git a/internal/systemd/guard_test.go b/internal/systemd/guard_test.go
index aab1bcc90..9d92bd6dc 100644
--- a/internal/systemd/guard_test.go
+++ b/internal/systemd/guard_test.go
@@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
+ "slices"
"sort"
"strconv"
"strings"
@@ -43,10 +44,12 @@ var remoteScripts = map[string]string{
// or a path ending in /systemctl or /journalctl, wherever it appears
// (exec.Command's name, a seam's first argument, a constant,
// "system"+"ctl");
-// - a local shell: a call whose arguments name a shell (sh, bash, dash,
-// by name or path) followed by an argument that holds systemctl or
-// journalctl as a word ("sh", "-c", "systemctl restart x"; the same
-// string in a variable; fmt.Sprintf("systemctl %s", u)).
+// - a local shell: a call anywhere in the file (a function, a method, a
+// function literal, a package var initializer, init) whose arguments
+// name a shell (sh, bash, dash, by name or path) followed by an
+// argument that holds systemctl or journalctl as a word ("sh", "-c",
+// "systemctl restart x"; the same string in a variable;
+// fmt.Sprintf("systemctl %s", u)).
//
// A sentence that only mentions a program ("run systemctl restart
// carlos-edge" in a message) is neither and passes. What the folding
@@ -120,6 +123,11 @@ const prog = "system" + "ctl"
const restartEdge = "systemctl restart carlos-edge.service"
var pkgScript = "journalctl -u carlos-edge.service"
var run func(context.Context, ...string) (string, error)
+var query = func() error { return exec.Command("sh", "-c", "systemctl show carlos-edge.service").Run() } // 13 local shell in a package-level func literal
+var edgeRestart = "systemctl restart carlos-edge.service"
+var bounce = func() error { return exec.Command("bash", "-c", edgeRestart).Run() } // 14 local shell, package-level literal through a var
+var atInit = exec.Command("dash", "-c", "journalctl -u carlos-edge.service") // 15 local shell in a package var initializer
+func init() { exec.Command("sh", "-c", "systemctl daemon-reload") } // 16 local shell in init
func f(ctx context.Context, unit string) {
exec.Command("systemctl", "show") // 1 program name
exec.CommandContext(ctx, "journalctl", "-u", "x") // 2 program name
@@ -151,8 +159,8 @@ func send(string) {}
t.Fatal(err)
}
got := unboundedRuns(t, []string{path})[0]
- if len(got) != 12 {
- t.Fatalf("caught %d, want 12:\n%s", len(got), strings.Join(got, "\n"))
+ if len(got) != 16 {
+ t.Fatalf("caught %d, want 16:\n%s", len(got), strings.Join(got, "\n"))
}
}
@@ -234,53 +242,55 @@ func unboundedRuns(t *testing.T, paths []string) [][]string {
}
return true
})
- // Local shells, per function so its variables resolve.
- for _, d := range f.Decls {
- fn, ok := d.(*ast.FuncDecl)
- if !ok || fn.Body == nil {
- continue
- }
- vals := map[string][]ast.Expr{}
- for k, v := range pkgVals {
- vals[k] = v
- }
- ast.Inspect(fn.Body, func(n ast.Node) bool {
- switch n := n.(type) {
- case *ast.AssignStmt:
+ // Local shells, in one traversal of the whole file: every call,
+ // wherever it is (a function, a method, a function literal, a
+ // package var initializer, init), with every string binding the
+ // file and its package make (const, var, := and =), joined without
+ // regard to scope. A name bound in two places resolves to both, so
+ // the union can only find more.
+ vals := map[string][]ast.Expr{}
+ for k, v := range pkgVals {
+ vals[k] = v
+ }
+ ast.Inspect(f, func(n ast.Node) bool {
+ switch n := n.(type) {
+ case *ast.AssignStmt:
+ if len(n.Lhs) == len(n.Rhs) {
for j, l := range n.Lhs {
- if id, ok := l.(*ast.Ident); ok && j < len(n.Rhs) && len(n.Lhs) == len(n.Rhs) {
+ if id, ok := l.(*ast.Ident); ok {
vals[id.Name] = append(vals[id.Name], n.Rhs[j])
}
}
- case *ast.ValueSpec:
- for j, id := range n.Names {
- if j < len(n.Values) {
- vals[id.Name] = append(vals[id.Name], n.Values[j])
- }
+ }
+ case *ast.ValueSpec:
+ // Package-level specs are in pkgVals already.
+ for j, id := range n.Names {
+ if j < len(n.Values) && !slices.Contains(pkgVals[id.Name], n.Values[j]) {
+ vals[id.Name] = append(vals[id.Name], n.Values[j])
}
}
+ }
+ return true
+ })
+ ast.Inspect(f, func(n ast.Node) bool {
+ call, ok := n.(*ast.CallExpr)
+ if !ok {
return true
- })
- ast.Inspect(fn.Body, func(n ast.Node) bool {
- call, ok := n.(*ast.CallExpr)
- if !ok {
- return true
- }
- shell := false
- for _, a := range call.Args {
- for _, v := range texts(a, vals, 0) {
- if shell && progWord.MatchString(v) {
- hit(a, "a local shell runs "+strconv.Quote(v))
- return true
- }
- if shellName(v) {
- shell = true
- }
+ }
+ shell := false
+ for _, a := range call.Args {
+ for _, v := range texts(a, vals, 0) {
+ if shell && progWord.MatchString(v) {
+ hit(a, "a local shell runs "+strconv.Quote(v))
+ return true
+ }
+ if shellName(v) {
+ shell = true
}
}
- return true
- })
- }
+ }
+ return true
+ })
out[i] = hits
}
return out