oficina / equipo Public

Mac slice F wb fix 1 #2: no termination proof without the T cleanup

Whole-branch review round 1, finding 2. Run.Finish now reports
whether the user-temp cleanup is done, bounded by the stop's deadline
plus Executor.CleanLimit (3 minutes, the allowance Start already gave
a pending cleanup). Gate.Run, process.Wait, Start's failed-record
path and the credential launches count a run as terminated only with
its end proven and that cleanup done. A scan, check or Engineering
launch whose cleanup failed or is late is unterminated: the scan
ends secret_in_candidate unterminated and nothing is uploaded. The
pending record stays the attempt's own, so its Verify finishes it.

clean() no longer reports a TemporaryItems folder removed during the
run as a failure (nothing is left in it): that error was ignored
before and now decides termination.

Regressions: TestGateCleanupFailureIsUnterminated,
TestWaitCleanupPendingIsUnproven, TestWorkerEngineeringScanCleanupFailed.
Mutations: Gate.Run ignoring Finish, Wait ignoring it, no late check,
a clean error ignored; each fails a named test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 71cae523ec71337f16bacd2fb5346e4a55f38cc2 parent ada7147
6 files changed, +188 −17
  • internal/runner/mac/gate.go +5 −2
  • internal/runner/mac/launch.go +39 −13
  • internal/runner/mac/usertemp.go +5 −0
  • internal/runner/mac/wbfix1_test.go +75 −0
  • internal/runner/thin/maccreds.go +6 −2
  • internal/runner/thin/wbfix1_scan_test.go +58 −0
diff --git a/internal/runner/mac/gate.go b/internal/runner/mac/gate.go
index d96ffe26..e42553ce 100644
--- a/internal/runner/mac/gate.go
+++ b/internal/runner/mac/gate.go
@@ -211,8 +211,11 @@ func (g *Gate) Run(ctx context.Context, attempt, dir string, argv []string, limi
}
// Only once the run's end is proven: a process of it still alive
// could be writing T as it is cleaned. Unproven, or not finished,
- // the cleanup waits for this attempt's proof (Verify) or a restart.
- run.Finish(ctx)
+ // the cleanup waits for this attempt's proof (Verify) or a restart,
+ // and the run is not terminated: its end is proven only with T clean.
+ if !run.Finish(ctx) {
+ res.Terminated = false
+ }
res.Output = out.String()
return res
}
diff --git a/internal/runner/mac/launch.go b/internal/runner/mac/launch.go
index bf9a2cca..0b0841e6 100644
--- a/internal/runner/mac/launch.go
+++ b/internal/runner/mac/launch.go
@@ -44,8 +44,10 @@ type Executor struct {
Own *Own
// Grace is SIGTERM to SIGKILL (10 s); Settle bounds the whole stop
// and its proof (30 s); Poll is how often the tree under the leader
- // is read while it runs (250 ms).
- Grace, Settle, Poll time.Duration
+ // is read while it runs (250 ms); CleanLimit is how much longer than
+ // the stop's deadline the user-temp cleanup may take (3 minutes: a
+ // simulator shutdown).
+ Grace, Settle, Poll, CleanLimit time.Duration
// UserTemp is the runner user's T when the attempt may write it (a
// sandboxed check, Engineering with sandboxed Xcode access): what
// appears there while it runs is removed once its end is proven.
@@ -89,6 +91,15 @@ func (x *Executor) own() *Own {
return x.Turns.owned()
}
+// cleanLimit is how much past the stop's deadline the user-temp
+// cleanup may run (a simulator shutdown).
+func (x *Executor) cleanLimit() time.Duration {
+ if x.CleanLimit > 0 {
+ return x.CleanLimit
+ }
+ return 3 * time.Minute
+}
+
func (x *Executor) settleLimit() time.Duration {
if x.Settle > 0 {
return x.Settle
@@ -146,7 +157,7 @@ func (x *Executor) Start(ctx context.Context, c Command, marker string, record f
// cleanup on record: a runner killed from here on leaves it to the
// next process (a restart), never to another turn of this one.
if x.UserTemp != "" {
- if err := x.finishPending(ctx, turn, time.Now().Add(x.settleLimit()), time.Now().Add(x.settleLimit()+3*time.Minute)); err != nil {
+ if err := x.finishPending(ctx, turn, time.Now().Add(x.settleLimit()), time.Now().Add(x.settleLimit()+x.cleanLimit())); err != nil {
return nil, err
}
temp, err := openTemp(x.UserTemp, nil)
@@ -230,7 +241,9 @@ func (x *Executor) Start(ctx context.Context, c Command, marker string, record f
if !proven && turn != nil {
turn.unprovenEnd()
}
- r.Finish(context.WithoutCancel(ctx))
+ if !r.Finish(context.WithoutCancel(ctx)) {
+ proven = false
+ }
if !proven {
return nil, errors.Join(ErrRecord, ErrUnproven, err)
}
@@ -273,6 +286,9 @@ type Run struct {
temp *tempState
finishOnce sync.Once
+ // stopBy is the stop's deadline; cleaned: Finish's cleanup is done.
+ stopBy time.Time
+ cleaned bool
stdin io.WriteCloser
stdout, stderr io.ReadCloser
@@ -391,6 +407,7 @@ func (r *Run) stopWith(grace time.Duration) bool {
}
}
deadline := time.Now().Add(r.x.settleLimit())
+ r.stopBy = deadline
out := r.scope.Settle(r.own, deadline, grace, reaped, r.adopt)
if r.turn == nil {
return out == proc.Proven && time.Now().Before(deadline)
@@ -415,24 +432,33 @@ func (r *Run) stopWith(grace time.Duration) bool {
// Finish is the user-temp cleanup once the end is proven (the
// attempt's own, in its turn): simulators quieted, then what appeared in
-// T removed, then the record. Unproven or not cleaned, the record stays
-// for this attempt's proof later, or a restart.
-func (r *Run) Finish(ctx context.Context) {
+// T removed, then the record, all by the stop's deadline plus
+// CleanLimit. It reports whether the cleanup is done (nothing to clean
+// is done); the end is proven only then. Unproven, failed or late, the
+// record stays, the attempt's own, for its proof later (Verify) or a
+// restart.
+func (r *Run) Finish(ctx context.Context) bool {
r.finishOnce.Do(func() {
if r.temp == nil {
+ r.cleaned = true
return
}
defer r.temp.close()
if !r.proven {
return
}
+ by := r.stopBy.Add(r.x.cleanLimit())
+ qctx, cancel := context.WithDeadline(context.WithoutCancel(ctx), by)
+ defer cancel()
if r.x.QuietSimulators != nil {
- r.x.QuietSimulators(context.WithoutCancel(ctx))
+ r.x.QuietSimulators(qctx)
}
- if r.temp.clean() == nil {
- _ = r.x.Turns.dropPending()
+ if r.temp.clean() != nil || !time.Now().Before(by) {
+ return
}
+ r.cleaned = r.x.Turns.dropPending() == nil
})
+ return r.cleaned
}
// finishPending finishes the user-temp cleanup an earlier attempt left,
@@ -616,7 +642,7 @@ func (s *Starter) StartRecorded(ctx context.Context, l provider.Launch, argv []s
// process is a Run as thin's provider.Process: Wait returns once the
// leader has exited or the launch's ctx was cancelled, and the end is
-// proven (nil) or not (ErrUnproven), with the user-temp cleanup done.
+// proven with the user-temp cleanup done (nil) or not (ErrUnproven).
type process struct {
run *Run
stop func() bool
@@ -635,9 +661,9 @@ func (p *process) Wait(ctx context.Context) (provider.Exit, error) {
}
proven := p.run.Stop()
p.stop()
- p.run.Finish(ctx)
+ cleaned := p.run.Finish(ctx)
exit := provider.Exit{Code: exitCode(p.run.waitErr), ObservedAt: time.Now()}
- if !proven {
+ if !proven || !cleaned {
return exit, ErrUnproven
}
return exit, nil
diff --git a/internal/runner/mac/usertemp.go b/internal/runner/mac/usertemp.go
index 351226d6..41c83a32 100644
--- a/internal/runner/mac/usertemp.go
+++ b/internal/runner/mac/usertemp.go
@@ -205,6 +205,11 @@ func (s *tempState) clean() error {
continue
}
now, err := list(d.root, d.prefix)
+ if d.prefix != "" && errors.Is(err, fs.ErrNotExist) {
+ // TemporaryItems itself is gone (removed while the run
+ // lasted): nothing is left in it to clean.
+ continue
+ }
if err != nil {
errs = append(errs, err)
continue
diff --git a/internal/runner/mac/wbfix1_test.go b/internal/runner/mac/wbfix1_test.go
new file mode 100644
index 00000000..40c07fd0
--- /dev/null
+++ b/internal/runner/mac/wbfix1_test.go
@@ -0,0 +1,75 @@
+package mac
+
+import (
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+// Whole-branch fix 1, #2: a run's end is proven only with its user-temp
+// cleanup finished in time. A cleanup that failed or is still pending
+// leaves the run unterminated (Gate.Run, process.Wait), and the pending
+// cleanup stays the attempt's own, so its own proof (Verify) finishes it.
+
+// TestGateCleanupFailureIsUnterminated: the check passes, its cleanup of
+// T fails (T cannot be listed when it is cleaned): not terminated; once T
+// can be listed again the attempt's proof cleans it.
+func TestGateCleanupFailureIsUnterminated(t *testing.T) {
+ g, dir := gateFixture(t, true)
+ _, temp := userTempFixture(t)
+ g.UserTemp, g.Turns = temp, NewTurns(filepath.Join(t.TempDir(), "lock"))
+ g.QuietSimulators = func(context.Context) { _ = os.Chmod(temp, 0) }
+ t.Cleanup(func() { _ = os.Chmod(temp, 0o700) })
+ res, id := run(g, dir, "checks-wb-1", time.Minute, `touch `+quote(filepath.Join(temp, "left")))
+ if res.Code != 0 || res.Terminated || !g.Turns.Pending() {
+ t.Fatalf("a check whose cleanup failed: %+v pending=%v", res, g.Turns.Pending())
+ }
+ g.QuietSimulators = nil
+ if err := os.Chmod(temp, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ if !g.Verify(id) || exists(filepath.Join(temp, "left")) || g.Turns.Pending() {
+ t.Fatal("the attempt's proof did not finish its cleanup")
+ }
+}
+
+// TestWaitCleanupPendingIsUnproven: an Engineering launch (process.Wait)
+// whose cleanup outlasts its deadline is unproven; its pending cleanup
+// is still owned, and the attempt's proof finishes it.
+func TestWaitCleanupPendingIsUnproven(t *testing.T) {
+ _, temp := userTempFixture(t)
+ turns := NewTurns(filepath.Join(t.TempDir(), "lock"))
+ // The shutdown runs until it is told to stop, at the cleanup's
+ // deadline, and a moment past it.
+ x := &Executor{Turns: turns, UserTemp: temp, Grace: 300 * time.Millisecond, Settle: time.Second, CleanLimit: 300 * time.Millisecond,
+ QuietSimulators: func(ctx context.Context) {
+ select {
+ case <-ctx.Done():
+ case <-time.After(10 * time.Second):
+ }
+ time.Sleep(50 * time.Millisecond)
+ }}
+ u, err := turns.Take(context.Background(), OpEngineering)
+ if err != nil {
+ t.Fatal(err)
+ }
+ r, err := x.Start(u.Context(context.Background()), Command{Argv: []string{"/bin/sh", "-c", "touch " + quote(filepath.Join(temp, "late"))}, Env: []string{"PATH=/usr/bin:/bin"}, Dir: t.TempDir()}, "eng-wb-1", func(string) error { return nil })
+ if err != nil {
+ t.Fatal(err)
+ }
+ p := &process{run: r, stop: func() bool { return true }}
+ if _, err := p.Wait(context.Background()); !errors.Is(err, ErrUnproven) {
+ t.Fatalf("a launch whose cleanup is still pending: %v", err)
+ }
+ if !turns.Pending() {
+ t.Fatal("the pending cleanup went")
+ }
+ u.Give()
+ x.QuietSimulators = nil
+ if !x.Verify(r.ID()) || exists(filepath.Join(temp, "late")) || turns.Pending() {
+ t.Fatal("the attempt's proof did not finish its cleanup")
+ }
+}
diff --git a/internal/runner/thin/maccreds.go b/internal/runner/thin/maccreds.go
index f4345d52..62fbdf2f 100644
--- a/internal/runner/thin/maccreds.go
+++ b/internal/runner/thin/maccreds.go
@@ -417,7 +417,9 @@ func (m *macCreds) authStatus(ctx context.Context, b provider.Binding) (provider
case <-ctx.Done():
}
proven := run.Stop()
- run.Finish(context.WithoutCancel(ctx))
+ if !run.Finish(context.WithoutCancel(ctx)) {
+ proven = false
+ }
if !proven {
// Not success: the watch and the turn's record stay, and the
// Mac's connections are closed until its end is proven.
@@ -643,7 +645,9 @@ func (t *macTerminal) Stop() error {
t.once.Do(func() {
defer t.turn.Give()
proven := t.run.Stop()
- t.run.Finish(context.Background())
+ if !t.run.Finish(context.Background()) {
+ proven = false
+ }
t.pty.Close()
if !proven {
t.m.keychain.Abandon(t.marker)
diff --git a/internal/runner/thin/wbfix1_scan_test.go b/internal/runner/thin/wbfix1_scan_test.go
new file mode 100644
index 00000000..7ad32d5a
--- /dev/null
+++ b/internal/runner/thin/wbfix1_scan_test.go
@@ -0,0 +1,58 @@
+//go:build testbuild
+
+package thin
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "amadan.net/oficina/equipo/internal/runner"
+ "amadan.net/oficina/equipo/internal/runner/mac"
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// Whole-branch fix 1, #2: the scanner passes, but its user-temp cleanup
+// fails: the scan's end is not proven, so the attempt is unterminated
+// and nothing is uploaded. The cleanup stays pending for the scan's own
+// proof.
+func TestWorkerEngineeringScanCleanupFailed(t *testing.T) {
+ x := newEngRig(t)
+ x.note("edit:Swipe.swift|false|true")
+ runnerPath := previewScannerFixture(t)
+ binary, pin := previewScannerTool(runner.Config{Executable: runnerPath})
+ temp := filepath.Join(t.TempDir(), "T")
+ if err := os.Mkdir(temp, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = os.Chmod(temp, 0o700) })
+ g := &mac.Gate{Env: []string{"PATH=/usr/bin:/bin"}, ScratchHome: true, Lines: 10, Sandbox: wire.SandboxSeatbelt, UserTemp: temp, Turns: x.m.turns,
+ QuietSimulators: func(context.Context) { _ = os.Chmod(temp, 0) }}
+ var scan string
+ x.w.Eng.Leaks = func(ctx context.Context, stage, marker string, record func(string) error) (bool, error) {
+ return gateLeaks(ctx, g, binary, pin, stage, marker, func(id string) error { scan = id; return record(id) })
+ }
+ rep := x.run(t)
+ if rep.State != wire.StepFailed || rep.Reason != wire.ReasonSecretInCandidate || rep.Termination || rep.CandidateSHA != "" {
+ t.Fatalf("report %+v", rep)
+ }
+ if c := x.committed(); c.CandidateSHA != "" {
+ t.Fatalf("committed %+v", c)
+ }
+ for _, f := range x.outbox(t) {
+ if f.Type == wire.FrameBlob {
+ t.Fatalf("uploaded %s", f.ID)
+ }
+ }
+ if !x.m.turns.Pending() {
+ t.Fatal("the scan's cleanup is not pending")
+ }
+ g.QuietSimulators = nil
+ if err := os.Chmod(temp, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ if !g.Verify(scan) || x.m.turns.Pending() {
+ t.Fatal("the scan's proof did not finish its cleanup")
+ }
+}