Darwin review: record before running, bounded output, darwin vet
Astra round 1, findings 3 and 7 and the vet note: - The gate records the attempt's marker before it starts anything and runs nothing if that fails; it records the group once the leader exists and kills it if that fails. The worker records the marker as it begins an attempt, and its git processes carry it too. Recovery never takes an attempt with no record as ended: it is settled as UnrecordedStep (no namespace left on Linux, no marked process on a worker). A marker-only record is settled by the marker, never by killing a group id of 0. - Gate output is bounded on every write: 200 lines of at most 4 KB, with or without newlines. - Linux-only tests carry a linux build tag; make vet also vets the runner's packages for darwin/arm64.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
21 files changed,
+286
−43
Makefile+1 −0internal/runner/containment/budget_test.go+2 −0internal/runner/containment/egress_test.go+2 −0internal/runner/containment/helper_version_test.go+2 −0internal/runner/containment/isolation_boundary_test.go+2 −0internal/runner/containment/isolation_test.go+2 −0internal/runner/containment/linux_test.go+2 −0internal/runner/containment/mounts_test.go+2 −0internal/runner/containment/recovery_test.go+2 −0internal/runner/containment/tool_test.go+2 −0internal/runner/mac/gate.go+45 −23internal/runner/mac/gate_test.go+85 −3internal/runner/mac/probe.go+2 −2internal/runner/setup/account_terminal_test.go+2 −0internal/runner/thin/mac.go+2 −2internal/runner/thin/mac_test.go+37 −0internal/runner/thin/run.go+21 −2internal/runner/thin/step.go+24 −1internal/runner/thin/step_test.go+27 −0internal/runner/thin/worker.go+21 −9internal/runner/thin/worker_test.go+1 −1
Intent
diff --git a/Makefile b/Makefile| index 1ebee7d2..5d20f142 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -89,6 +89,7 @@ perf: |
| vet: |
| go vet ./... |
| go vet -tags testbuild ./internal/runner/thin ./internal/headless |
| + GOOS=darwin GOARCH=arm64 go vet ./cmd/equipo-runner/... ./cmd/equipo-tool-proxy/... ./internal/runner/... ./internal/wire/... ./internal/release/... |
| fmt-check: |
| @out=$$(gofmt -l .); if [ -n "$$out" ]; then echo "gofmt needed:"; echo "$$out"; exit 1; fi |
diff --git a/internal/runner/containment/budget_test.go b/internal/runner/containment/budget_test.go| index a8de5e95..f071ce48 100644 |
| --- a/internal/runner/containment/budget_test.go |
| +++ b/internal/runner/containment/budget_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/containment/egress_test.go b/internal/runner/containment/egress_test.go| index 1a706e23..99bef2e0 100644 |
| --- a/internal/runner/containment/egress_test.go |
| +++ b/internal/runner/containment/egress_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/containment/helper_version_test.go b/internal/runner/containment/helper_version_test.go| index 703685a5..d37c23cc 100644 |
| --- a/internal/runner/containment/helper_version_test.go |
| +++ b/internal/runner/containment/helper_version_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/containment/isolation_boundary_test.go b/internal/runner/containment/isolation_boundary_test.go| index 5cf638d9..4396f1de 100644 |
| --- a/internal/runner/containment/isolation_boundary_test.go |
| +++ b/internal/runner/containment/isolation_boundary_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/containment/isolation_test.go b/internal/runner/containment/isolation_test.go| index e62a56b2..85165d64 100644 |
| --- a/internal/runner/containment/isolation_test.go |
| +++ b/internal/runner/containment/isolation_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/containment/linux_test.go b/internal/runner/containment/linux_test.go| index 7ed0fc3e..58db2a10 100644 |
| --- a/internal/runner/containment/linux_test.go |
| +++ b/internal/runner/containment/linux_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/containment/mounts_test.go b/internal/runner/containment/mounts_test.go| index 86c8373e..a606c4f9 100644 |
| --- a/internal/runner/containment/mounts_test.go |
| +++ b/internal/runner/containment/mounts_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/containment/recovery_test.go b/internal/runner/containment/recovery_test.go| index 49e494a0..3e8adb5e 100644 |
| --- a/internal/runner/containment/recovery_test.go |
| +++ b/internal/runner/containment/recovery_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/containment/tool_test.go b/internal/runner/containment/tool_test.go| index 50831ba4..c8eeee12 100644 |
| --- a/internal/runner/containment/tool_test.go |
| +++ b/internal/runner/containment/tool_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package containment |
| import ( |
diff --git a/internal/runner/mac/gate.go b/internal/runner/mac/gate.go| index fbf93a64..3d4f3026 100644 |
| --- a/internal/runner/mac/gate.go |
| +++ b/internal/runner/mac/gate.go |
| @@ -1,6 +1,7 @@ |
| package mac |
| import ( |
| + "bytes" |
| "context" |
| "errors" |
| "os" |
| @@ -67,9 +68,11 @@ type Gate struct { |
| // (cli.ts), so the exit code alone could turn a killed gate into a pass. |
| const statusScript = `"$@"; s=$?; printf '%s\n' "$s" > "$EQUIPO_GATE_STATUS"; exit "$s"` |
| -// Run runs argv in dir. record gets the attempt's process record before |
| -// it can do anything, so a restarted runner can prove its end. |
| -func (g *Gate) Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(string)) Result { |
| +// Run runs argv in dir. record persists the attempt's process record: |
| +// its marker before anything starts (a restarted runner then sweeps by |
| +// the marker alone), and its group once the leader exists. Nothing runs |
| +// if the first record fails; the group is killed if the second does. |
| +func (g *Gate) Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(string) error) Result { |
| res := Result{Code: -1, Sandbox: g.Sandbox} |
| unavailable := func() Result { res.Reason, res.Terminated = "checks_unavailable", true; return res } |
| if len(argv) == 0 || strings.ContainsAny(attempt, " \n\x00") { |
| @@ -96,6 +99,9 @@ func (g *Gate) Run(ctx context.Context, attempt, dir string, argv []string, limi |
| defer os.Remove(settings) |
| cmdline = append([]string{g.SRT, "--settings", settings, "--"}, cmdline...) |
| } |
| + if err := record(RecordID(0, attempt)); err != nil { |
| + return unavailable() |
| + } |
| var before []string |
| if g.Booted != nil { |
| before = g.Booted(ctx) |
| @@ -117,9 +123,14 @@ func (g *Gate) Run(ctx context.Context, attempt, dir string, argv []string, limi |
| return unavailable() |
| } |
| pgid := cmd.Process.Pid |
| - record(RecordID(pgid, attempt)) |
| exited := make(chan error, 1) |
| go func() { exited <- cmd.Wait() }() |
| + if err := record(RecordID(pgid, attempt)); err != nil { |
| + _ = syscall.Kill(-pgid, syscall.SIGKILL) |
| + <-exited |
| + res.Reason, res.Terminated, res.Output = "checks_unavailable", g.settle(pgid, attempt, true), out.String() |
| + return res |
| + } |
| if g.Caffeinate != "" { |
| // Outside the group: it ends by itself when the gate's leader does. |
| awake := exec.Command(g.Caffeinate, "-i", "-w", strconv.Itoa(pgid)) |
| @@ -187,9 +198,11 @@ func (g *Gate) Verify(id string) bool { |
| } |
| num, attempt, _ := strings.Cut(rest, ":") |
| pgid, err := strconv.Atoi(num) |
| - if err != nil || pgid <= 1 || attempt == "" { |
| + if err != nil || pgid < 0 || pgid == 1 || attempt == "" { |
| return false |
| } |
| + // pgid 0: the marker was recorded, the group not yet; the marker |
| + // alone settles it. |
| return g.settle(pgid, attempt, false) |
| } |
| @@ -213,15 +226,15 @@ func (g *Gate) settle(pgid int, attempt string, own bool) bool { |
| if err != nil { |
| return false |
| } |
| - ours := own |
| + ours := own && pgid > 1 |
| for _, p := range marked { |
| - ours = ours || p.PGID == pgid |
| + ours = ours || pgid > 1 && p.PGID == pgid |
| _ = syscall.Kill(p.PID, syscall.SIGKILL) |
| } |
| if ours { |
| _ = syscall.Kill(-pgid, syscall.SIGKILL) |
| } |
| - gone := syscall.Kill(-pgid, 0) == syscall.ESRCH || !ours |
| + gone := !ours || syscall.Kill(-pgid, 0) == syscall.ESRCH |
| if gone && len(marked) == 0 { |
| return true |
| } |
| @@ -267,7 +280,9 @@ func contains(list []string, s string) bool { |
| return false |
| } |
| -// tail keeps the last max lines written to it. |
| +// tail keeps the last max lines written to it, each at most |
| +// maxLineBytes (the rest of a longer line is dropped), so what it holds |
| +// is bounded however much a gate prints, with or without newlines. |
| type tail struct { |
| mu sync.Mutex |
| max int |
| @@ -276,29 +291,36 @@ type tail struct { |
| partial []byte |
| } |
| +const maxLineBytes = 4 << 10 |
| + |
| func (t *tail) Write(p []byte) (int, error) { |
| t.mu.Lock() |
| defer t.mu.Unlock() |
| - t.partial = append(t.partial, p...) |
| - for { |
| - i := strings.IndexByte(string(t.partial), '\n') |
| + n := len(p) |
| + for len(p) > 0 { |
| + i := bytes.IndexByte(p, '\n') |
| + chunk := p |
| + if i >= 0 { |
| + chunk = p[:i] |
| + } |
| + if room := maxLineBytes - len(t.partial); room > 0 { |
| + t.partial = append(t.partial, chunk[:min(len(chunk), room)]...) |
| + } |
| if i < 0 { |
| break |
| } |
| - t.lines = append(t.lines, string(t.partial[:i])) |
| - if t.watch != nil { |
| - t.watch(string(t.partial[:i])) |
| - } |
| - t.partial = t.partial[i+1:] |
| + line := string(t.partial) |
| + t.partial = t.partial[:0] |
| + t.lines = append(t.lines, line) |
| if len(t.lines) > t.max { |
| - t.lines = t.lines[len(t.lines)-t.max:] |
| + t.lines = append(t.lines[:0:0], t.lines[len(t.lines)-t.max:]...) |
| } |
| + if t.watch != nil { |
| + t.watch(line) |
| + } |
| + p = p[i+1:] |
| } |
| - if len(t.partial) > 64<<10 { |
| - t.lines = append(t.lines, string(t.partial)) |
| - t.partial = nil |
| - } |
| - return len(p), nil |
| + return n, nil |
| } |
| func (t *tail) String() string { |
diff --git a/internal/runner/mac/gate_test.go b/internal/runner/mac/gate_test.go| index 8f641249..cbb47a03 100644 |
| --- a/internal/runner/mac/gate_test.go |
| +++ b/internal/runner/mac/gate_test.go |
| @@ -3,6 +3,7 @@ package mac |
| import ( |
| "context" |
| "encoding/json" |
| + "errors" |
| "os" |
| "os/exec" |
| "path/filepath" |
| @@ -52,7 +53,7 @@ func gateFixture(t *testing.T, sandboxed bool) (*Gate, string) { |
| func run(g *Gate, dir, attempt string, limit time.Duration, script string) (Result, string) { |
| var id string |
| - res := g.Run(context.Background(), attempt, dir, []string{"/bin/bash", "-c", script}, limit, func(s string) { id = s }) |
| + res := g.Run(context.Background(), attempt, dir, []string{"/bin/bash", "-c", script}, limit, func(s string) error { id = s; return nil }) |
| return res, id |
| } |
| @@ -129,7 +130,7 @@ func TestGateCancelAndTimeoutProveTheEnd(t *testing.T) { |
| var res Result |
| done := make(chan struct{}) |
| go func() { |
| - res = g.Run(ctx, "checks-d-1", dir, []string{"/bin/bash", "-c", stubborn}, time.Minute, func(string) {}) |
| + res = g.Run(ctx, "checks-d-1", dir, []string{"/bin/bash", "-c", stubborn}, time.Minute, func(string) error { return nil }) |
| close(done) |
| }() |
| time.Sleep(700 * time.Millisecond) |
| @@ -158,10 +159,11 @@ func TestGateShutsDownWhatItBooted(t *testing.T) { |
| booted, down := []string{"A"}, []string{} |
| g.Booted = func(context.Context) []string { mu.Lock(); defer mu.Unlock(); return slices.Clone(booted) } |
| g.Shutdown = func(_ context.Context, u string) { mu.Lock(); down = append(down, u); mu.Unlock() } |
| - res := g.Run(context.Background(), "checks-e-1", dir, []string{"/bin/sh", "-c", "true"}, time.Minute, func(string) { |
| + res := g.Run(context.Background(), "checks-e-1", dir, []string{"/bin/sh", "-c", "true"}, time.Minute, func(string) error { |
| mu.Lock() |
| booted = append(booted, "B") |
| mu.Unlock() |
| + return nil |
| }) |
| if res.Code != 0 || !slices.Equal(down, []string{"B"}) { |
| t.Fatalf("%+v shut down %v", res, down) |
| @@ -241,3 +243,83 @@ func TestGateUnprovenWhileMarkedRemain(t *testing.T) { |
| t.Fatalf("proven with a marked process left: %+v", res) |
| } |
| } |
| + |
| +// Retention is bounded on every write: a gate that prints without end, |
| +// with or without newlines, holds at most max lines of maxLineBytes. |
| +func TestTailIsBounded(t *testing.T) { |
| + tl := &tail{max: 200} |
| + chunk := []byte(strings.Repeat("x", 1<<20)) |
| + for range 64 { |
| + _, _ = tl.Write(chunk) |
| + } |
| + if len(tl.partial) > maxLineBytes || len(tl.String()) > maxLineBytes { |
| + t.Fatalf("a line without a newline held %d bytes", len(tl.partial)) |
| + } |
| + line := []byte(strings.Repeat("y", 10<<10) + "\n") |
| + for range 5000 { |
| + _, _ = tl.Write(line) |
| + } |
| + held := 0 |
| + for _, l := range tl.lines { |
| + held += len(l) |
| + } |
| + if len(tl.lines) > 200 || held > 200*maxLineBytes || cap(tl.lines) > 400 { |
| + t.Fatalf("held %d lines, %d bytes, cap %d", len(tl.lines), held, cap(tl.lines)) |
| + } |
| +} |
| + |
| +// The record comes first: if the marker cannot be kept, nothing runs; if |
| +// the group cannot be, the group is killed at once and the end proven. |
| +func TestGateRecordsBeforeRunning(t *testing.T) { |
| + g, dir := gateFixture(t, false) |
| + ran := filepath.Join(filepath.Dir(dir), "ran") |
| + script := "touch " + ran + "; sleep 60" |
| + var ids []string |
| + res := g.Run(context.Background(), "checks-h-1", dir, []string{"/bin/bash", "-c", script}, time.Minute, func(id string) error { |
| + ids = append(ids, id) |
| + return errors.New("disk full") |
| + }) |
| + if _, err := os.Stat(ran); !os.IsNotExist(err) || res.Code == 0 || res.Reason != "checks_unavailable" || !slices.Equal(ids, []string{"pg:0:checks-h-1"}) { |
| + t.Fatalf("ran without a record: %+v %v", res, ids) |
| + } |
| + ids = nil |
| + start := time.Now() |
| + res = g.Run(context.Background(), "checks-h-2", dir, []string{"/bin/bash", "-c", script}, time.Minute, func(id string) error { |
| + ids = append(ids, id) |
| + if len(ids) == 2 { |
| + return errors.New("disk full") |
| + } |
| + return nil |
| + }) |
| + if res.Reason != "checks_unavailable" || !res.Terminated || time.Since(start) > 20*time.Second || len(ids) != 2 || ids[0] != "pg:0:checks-h-2" || !strings.HasPrefix(ids[1], "pg:") { |
| + t.Fatalf("group not stopped when its record failed: %+v %v", res, ids) |
| + } |
| + if left, _ := Marked("checks-h-2"); len(left) != 0 { |
| + t.Fatalf("left %v", left) |
| + } |
| +} |
| + |
| +// After a restart an attempt that recorded only its marker (no group) is |
| +// settled by the marker: its processes are killed, then it has ended. |
| +func TestGateVerifyByMarkerAlone(t *testing.T) { |
| + g, _ := gateFixture(t, false) |
| + c := exec.Command("/bin/sleep", "60") |
| + c.Env = []string{"EQUIPO_ATTEMPT=checks-i-1"} |
| + c.SysProcAttr = &syscall.SysProcAttr{Setsid: true} |
| + if err := c.Start(); err != nil { |
| + t.Fatal(err) |
| + } |
| + waited := make(chan error, 1) |
| + go func() { waited <- c.Wait() }() |
| + if !g.Verify(RecordID(0, "checks-i-1")) { |
| + t.Fatal("marker-only record not settled") |
| + } |
| + select { |
| + case <-waited: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("marked process survived") |
| + } |
| + if g.Verify("") || g.Verify("pg:-1:x") { |
| + t.Fatal("an empty or malformed record taken as ended") |
| + } |
| +} |
diff --git a/internal/runner/mac/probe.go b/internal/runner/mac/probe.go| index fc4f216b..eb73dee6 100644 |
| --- a/internal/runner/mac/probe.go |
| +++ b/internal/runner/mac/probe.go |
| @@ -331,7 +331,7 @@ func probeP2(ctx context.Context, h ProbeHost, caps wire.Capabilities) (wire.Pro |
| script := `cd project && ` + xb + ` -project Hostile.xcodeproj -target HostileScript -configuration Debug build SYMROOT="$TMPDIR/sym" OBJROOT="$TMPDIR/obj" |
| cd ../package && s=Hostile-Package; ` + xb + ` -list 2>/dev/null | grep -q Hostile-Package || s=Hostile |
| ` + xb + ` test -scheme "$s" -destination 'platform=macOS' -only-testing:HostileTests/HostileTests/testHostile -derivedDataPath "$TMPDIR/dd" -skipPackagePluginValidation` |
| - res := h.Gate(true).Run(ctx, "probe-p2-"+secret[:8], root, []string{"/bin/bash", "-c", script}, 20*time.Minute, func(string) {}) |
| + res := h.Gate(true).Run(ctx, "probe-p2-"+secret[:8], root, []string{"/bin/bash", "-c", script}, 20*time.Minute, func(string) error { return nil }) |
| mu.Lock() |
| defer mu.Unlock() |
| srtRan := res.Code == 0 && strings.Contains(res.Output, "** BUILD SUCCEEDED **") && strings.Contains(res.Output, "** TEST SUCCEEDED **") |
| @@ -462,7 +462,7 @@ func probeP6(ctx context.Context, h ProbeHost, caps wire.Capabilities, srtRan bo |
| xb := quote(h.Tools.Xcodebuild) |
| script := `cd package && s=Hostile-Package; ` + xb + ` -list 2>/dev/null | grep -q Hostile-Package || s=Hostile |
| exec ` + xb + ` test -scheme "$s" -destination ` + quote(destination) + ` -only-testing:HostileTests/HostileTests/testWait -derivedDataPath "$TMPDIR/dd" -skipPackagePluginValidation` |
| - res := g.Run(run, attempt, root, []string{"/bin/bash", "-c", script}, wait, func(string) {}) |
| + res := g.Run(run, attempt, root, []string{"/bin/bash", "-c", script}, wait, func(string) error { return nil }) |
| mu.Lock() |
| defer mu.Unlock() |
| took := time.Since(stopped).Round(time.Second) |
diff --git a/internal/runner/setup/account_terminal_test.go b/internal/runner/setup/account_terminal_test.go| index 3c35ecb0..5ccf8acb 100644 |
| --- a/internal/runner/setup/account_terminal_test.go |
| +++ b/internal/runner/setup/account_terminal_test.go |
| @@ -1,3 +1,5 @@ |
| +//go:build linux |
| + |
| package runnersetup |
| import ( |
diff --git a/internal/runner/thin/mac.go b/internal/runner/thin/mac.go| index 690b2713..8cd063b9 100644 |
| --- a/internal/runner/thin/mac.go |
| +++ b/internal/runner/thin/mac.go |
| @@ -32,7 +32,7 @@ type macGate struct{ g *mac.Gate } |
| func (m macGate) Sandbox() string { return m.g.Sandbox } |
| -func (m macGate) Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(string)) gateResult { |
| +func (m macGate) Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(string) error) gateResult { |
| r := m.g.Run(ctx, attempt, dir, argv, limit, record) |
| return gateResult{output: r.Output, reason: r.Reason, code: r.Code, terminated: r.Terminated, sandbox: r.Sandbox} |
| } |
| @@ -62,7 +62,7 @@ func (m *macChecks) pick() *mac.Gate { |
| func (m *macChecks) Sandbox() string { return m.pick().Sandbox } |
| -func (m *macChecks) Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(string)) gateResult { |
| +func (m *macChecks) Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(string) error) gateResult { |
| return macGate{m.pick()}.Run(ctx, attempt, dir, argv, limit, record) |
| } |
diff --git a/internal/runner/thin/mac_test.go b/internal/runner/thin/mac_test.go| index 1b6b82e5..9fbb04f6 100644 |
| --- a/internal/runner/thin/mac_test.go |
| +++ b/internal/runner/thin/mac_test.go |
| @@ -260,3 +260,40 @@ func TestMacHostPaths(t *testing.T) { |
| t.Fatalf("gate env %v", h.env) |
| } |
| } |
| + |
| +// A worker's attempt has its marker on record before it starts anything, |
| +// and an attempt a crashed runner left with no record is never taken as |
| +// ended: recovery asks the worker to settle it by its marker. |
| +func TestWorkerRecordsBeforeRunning(t *testing.T) { |
| + x := newWorkerRig(t) |
| + x.chunk(t, "c1", x.header(), x.pack, 0, 1, "") |
| + var seen string |
| + x.w.Ready = func(context.Context) error { return nil } |
| + gate := &recordingGate{stubGate: x.gate, before: func() { |
| + _ = x.w.State.db.QueryRow("SELECT coalesce(ns_id, '') FROM steps WHERE step_id=?", x.r.StepID).Scan(&seen) |
| + }} |
| + x.w.Gate = gate |
| + if err := x.w.State.AcceptDispatch(context.Background(), "d1", x.r.DispatchCommand); err != nil { |
| + t.Fatal(err) |
| + } |
| + _ = x.run() |
| + if seen != mac.RecordID(0, "checks/"+stepKey(x.r)) { |
| + t.Fatalf("recorded before the gate: %q", seen) |
| + } |
| + if workerRecord(UnrecordedStep(x.r.key())) != mac.RecordID(0, "checks/"+stepKey(x.r)) { |
| + t.Fatal("an unrecorded attempt is not settled by its marker") |
| + } |
| + if workerRecord("") != "" { |
| + t.Fatal("empty record") |
| + } |
| +} |
| + |
| +type recordingGate struct { |
| + *stubGate |
| + before func() |
| +} |
| + |
| +func (g *recordingGate) Run(ctx context.Context, a, dir string, argv []string, l time.Duration, record func(string) error) gateResult { |
| + g.before() |
| + return g.stubGate.Run(ctx, a, dir, argv, l, record) |
| +} |
diff --git a/internal/runner/thin/run.go b/internal/runner/thin/run.go| index f6df8118..b6ce394f 100644 |
| --- a/internal/runner/thin/run.go |
| +++ b/internal/runner/thin/run.go |
| @@ -2,6 +2,8 @@ package thin |
| import ( |
| "context" |
| + "crypto/sha256" |
| + "encoding/hex" |
| "errors" |
| "log/slog" |
| "os" |
| @@ -17,6 +19,7 @@ import ( |
| "amadan.net/oficina/equipo/internal/runner" |
| "amadan.net/oficina/equipo/internal/runner/containment" |
| + "amadan.net/oficina/equipo/internal/runner/mac" |
| "amadan.net/oficina/equipo/internal/runner/provider" |
| "amadan.net/oficina/equipo/internal/wire" |
| ) |
| @@ -452,7 +455,7 @@ func verifiedExit(ctx context.Context, m *containment.Manager, id string) bool { |
| if err != nil { |
| return false |
| } |
| - if id == "" { |
| + if _, unrecorded := unrecordedKey(id); id == "" || unrecorded { |
| // No namespace recorded for the attempt: verified only when none |
| // of this runner's namespaces remains. |
| return len(records) == 0 |
| @@ -483,6 +486,22 @@ type workerSetup struct { |
| welcomed func(wire.Welcome, *Worker, context.CancelCauseFunc) |
| } |
| +// workerRecord is the process record Verify settles: an attempt that |
| +// recorded no group is settled by its marker alone. An empty record is |
| +// never an ended attempt. |
| +func workerRecord(id string) string { |
| + if key, ok := unrecordedKey(id); ok { |
| + return mac.RecordID(0, attemptMarker(key)) |
| + } |
| + return id |
| +} |
| + |
| +// attemptMarker is the EQUIPO_ATTEMPT value of a worker's attempt. |
| +func attemptMarker(key string) string { |
| + sum := sha256.Sum256([]byte(key)) |
| + return "checks/" + hex.EncodeToString(sum[:8]) |
| +} |
| + |
| // ErrUpdated ends a worker that replaced its binary with the instance's |
| // release: the service manager starts the new one. |
| var ErrUpdated = errors.New("runner updated to the instance's release") |
| @@ -531,7 +550,7 @@ func runWorker(ctx context.Context, c runner.Config, o Options, platform wire.Pl |
| } |
| } |
| }, |
| - Verify: func(_ context.Context, id string) bool { return id == "" || w.gate.Verify(id) }, TerminationKind: wire.TerminationProcessGroup} |
| + Verify: func(_ context.Context, id string) bool { return w.gate.Verify(workerRecord(id)) }, TerminationKind: wire.TerminationProcessGroup} |
| link.Steps = steps |
| if err := steps.Start(ctx); err != nil { |
| return err |
diff --git a/internal/runner/thin/step.go b/internal/runner/thin/step.go| index 19cb3ae3..984822dd 100644 |
| --- a/internal/runner/thin/step.go |
| +++ b/internal/runner/thin/step.go |
| @@ -323,6 +323,17 @@ func (s *State) proveTermination(ctx context.Context, p pendingStep) error { |
| }) |
| } |
| +const unrecordedPrefix = "step:" |
| + |
| +// UnrecordedStep names an attempt that recorded no namespace or process |
| +// group before the runner stopped. Verify must prove it ended on its own |
| +// terms: no namespace of this runner left (Linux), no process carrying |
| +// its marker (a worker). |
| +func UnrecordedStep(key string) string { return unrecordedPrefix + key } |
| + |
| +// unrecordedKey is the step key of an UnrecordedStep record. |
| +func unrecordedKey(id string) (string, bool) { return strings.CutPrefix(id, unrecordedPrefix) } |
| + |
| // RecoverSteps settles attempts a previous process left running. Its |
| // namespaces died with it and startup verified their exits, so an |
| // attempt that had not committed has ended: failed (or cancelled), with |
| @@ -349,10 +360,19 @@ func (s *State) RecoverSteps(ctx context.Context, verify func(context.Context, s |
| rows.Close() |
| proven := map[string]bool{} |
| for _, r := range all { |
| - proven[r.key()] = verify == nil || ns[r.key()] == "" || verify(ctx, ns[r.key()]) |
| + // An attempt that recorded nothing is never taken as ended: verify |
| + // is asked about the step itself (UnrecordedStep), and says so only |
| + // once nothing of it can be left. |
| + if ns[r.key()] == "" { |
| + ns[r.key()] = UnrecordedStep(r.key()) |
| + } |
| + proven[r.key()] = verify == nil || verify(ctx, ns[r.key()]) |
| } |
| return s.tx(ctx, func(t *sql.Tx) error { |
| for _, r := range all { |
| + if _, err := t.Exec("UPDATE steps SET ns_id=? WHERE step_id=? AND attempt=? AND coalesce(ns_id, '')=''", ns[r.key()], r.StepID, r.Attempt); err != nil { |
| + return err |
| + } |
| rep := wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, BaseSHA: r.Base, Termination: proven[r.key()]} |
| if rep.Termination { |
| rep.TerminationKind = terminationKind(ns[r.key()]) |
| @@ -716,6 +736,9 @@ func terminationKind(record string) string { |
| if strings.HasPrefix(record, "pg:") { |
| return wire.TerminationProcessGroup |
| } |
| + if strings.HasPrefix(record, unrecordedPrefix) { |
| + return "" |
| + } |
| return wire.TerminationNamespace |
| } |
diff --git a/internal/runner/thin/step_test.go b/internal/runner/thin/step_test.go| index 7dbb78b6..4d61e3e3 100644 |
| --- a/internal/runner/thin/step_test.go |
| +++ b/internal/runner/thin/step_test.go |
| @@ -350,3 +350,30 @@ func TestSyncHoldsPendingTermination(t *testing.T) { |
| t.Fatalf("acked, proof still pending: %+v", h) |
| } |
| } |
| + |
| +// An attempt that recorded nothing before the crash is not ended by |
| +// default: verify is asked about it by name (UnrecordedStep), and while |
| +// unproven it stays pending under that record. |
| +func TestStepRecoverUnrecorded(t *testing.T) { |
| + ctx := context.Background() |
| + s, err := OpenState(ctx, t.TempDir()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer s.Close() |
| + s.AcceptDispatch(ctx, "f1", dispatchFor("tk9", "s9", 1)) |
| + r, _, _ := s.nextStep(ctx) |
| + var asked []string |
| + if err := s.RecoverSteps(ctx, func(_ context.Context, id string) bool { asked = append(asked, id); return false }); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(asked) != 1 || asked[0] != UnrecordedStep(r.key()) { |
| + t.Fatalf("asked %v", asked) |
| + } |
| + if reps := outbox(t, s); len(reps) != 1 || reps[0].Termination { |
| + t.Fatalf("an unrecorded attempt counted as ended: %+v", reps) |
| + } |
| + if p, _ := s.pendingSteps(ctx); len(p) != 1 || p[0].ns != UnrecordedStep(r.key()) { |
| + t.Fatalf("pending %+v", p) |
| + } |
| +} |
diff --git a/internal/runner/thin/worker.go b/internal/runner/thin/worker.go| index 028e4807..f26752ef 100644 |
| --- a/internal/runner/thin/worker.go |
| +++ b/internal/runner/thin/worker.go |
| @@ -11,6 +11,7 @@ import ( |
| "sync" |
| "time" |
| + "amadan.net/oficina/equipo/internal/runner/mac" |
| "amadan.net/oficina/equipo/internal/runner/workspace" |
| "amadan.net/oficina/equipo/internal/wire" |
| ) |
| @@ -25,9 +26,10 @@ import ( |
| type GateRunner interface { |
| // Sandbox is what the runner reports in hello (wire.Sandbox*). |
| Sandbox() string |
| - // Run runs argv in dir. record is called with the attempt's process |
| - // record before it runs (so a restart can verify its end). |
| - Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(id string)) gateResult |
| + // Run runs argv in dir. record persists the attempt's process record |
| + // before it runs (so a restart can verify its end); a record that |
| + // cannot be kept stops the run. |
| + Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(id string) error) gateResult |
| // Verify reports whether a recorded attempt has ended for sure. |
| Verify(id string) bool |
| } |
| @@ -68,8 +70,9 @@ type Worker struct { |
| Ready func(context.Context) error |
| // mu is held while an attempt runs, so an update swaps the binary |
| - // only between steps. |
| - mu sync.Mutex |
| + // only between steps; marker is that attempt's EQUIPO_ATTEMPT. |
| + mu sync.Mutex |
| + marker string |
| } |
| func (w *Worker) root() string { return filepath.Join(w.Engine.O.Runner.StateDir, "worker") } |
| @@ -114,6 +117,15 @@ func (w *Worker) Exec(ctx context.Context, r stepRow, progress func(wire.StepRep |
| return end(wire.StepCancelled, "cancelled") |
| } |
| } |
| + // The attempt's marker is on record before anything of it runs: every |
| + // process it starts (git here, the gate later) carries it, so a |
| + // restarted runner can find them even if no group was recorded. |
| + w.marker = "checks/" + stepKey(r) |
| + defer func() { w.marker = "" }() |
| + if err := w.State.setStepNamespace(ctx, r, mac.RecordID(0, w.marker)); err != nil { |
| + w.Engine.log("worker record", err) |
| + return end(wire.StepFailed, "workspace_refused") |
| + } |
| if err := w.Engine.lease(ctx, r); err != nil { |
| return end(wire.StepCancelled, leaseReason(err)) |
| } |
| @@ -158,8 +170,8 @@ func (w *Worker) Exec(ctx context.Context, r stepRow, progress func(wire.StepRep |
| if limit <= 0 { |
| limit = DefaultMaxStep |
| } |
| - res := w.Gate.Run(ctx, "checks/"+stepKey(r), tree.Dir, g.argv, limit, func(id string) { |
| - _ = w.State.setStepNamespace(context.WithoutCancel(ctx), r, id) |
| + res := w.Gate.Run(ctx, "checks/"+stepKey(r), tree.Dir, g.argv, limit, func(id string) error { |
| + return w.State.setStepNamespace(context.WithoutCancel(ctx), r, id) |
| }) |
| rep.TerminationKind, rep.ChecksSandbox = wire.TerminationProcessGroup, res.sandbox |
| rep.Gate, rep.Output, rep.LogTail = g.name, res.output, lastLines(res.output, stepTailLines, wire.MaxLogTail) |
| @@ -233,7 +245,7 @@ func (w *Worker) git(ctx context.Context, stdin io.Reader, args ...string) (stri |
| for k, v := range workspace.GitEnvironment() { |
| cmd.Env = append(cmd.Env, k+"="+v) |
| } |
| - cmd.Env = append(cmd.Env, "HOME=/nonexistent") |
| + cmd.Env = append(cmd.Env, "HOME=/nonexistent", "EQUIPO_ATTEMPT="+w.marker) |
| out, err := cmd.Output() |
| return strings.TrimSpace(string(out)), err |
| } |
| @@ -271,7 +283,7 @@ func (w *Worker) checkout(ctx context.Context, t workspace.TaskTree, sha string) |
| for k, v := range workspace.GitEnvironment() { |
| cmd.Env = append(cmd.Env, k+"="+v) |
| } |
| - cmd.Env = append(cmd.Env, append(env, "HOME=/nonexistent")...) |
| + cmd.Env = append(cmd.Env, append(env, "HOME=/nonexistent", "EQUIPO_ATTEMPT="+w.marker)...) |
| cmd.Dir = t.Dir |
| return cmd.Run() |
| } |
diff --git a/internal/runner/thin/worker_test.go b/internal/runner/thin/worker_test.go| index c9887195..a697e9db 100644 |
| --- a/internal/runner/thin/worker_test.go |
| +++ b/internal/runner/thin/worker_test.go |
| @@ -24,7 +24,7 @@ type stubGate struct { |
| } |
| func (g *stubGate) Sandbox() string { return wire.SandboxNone } |
| -func (g *stubGate) Run(_ context.Context, _, dir string, argv []string, _ time.Duration, record func(string)) gateResult { |
| +func (g *stubGate) Run(_ context.Context, _, dir string, argv []string, _ time.Duration, record func(string) error) gateResult { |
| g.ran = append(g.ran, argv) |
| g.dirs = append(g.dirs, dir) |
| record("pg:1:stub") |