Mac slice F wb fix 3 #1, #2: hold the turn through Wait; pending boot
Whole-branch review round 3, findings 1 and 2. #1: a Mac launch now waits for the executor's Wait (stop, proof, cleanup) to return before it gives the turn back, also when its own wait has run out; it is then unterminated. That wait is derived from the executor's budgets (Executor.StopBudget: the settle limit plus CleanLimit) plus macStopMargin, replacing the fixed macStopLimit; macStopGrace names the 10 s grace. #2: Verify consults the attempt's own pending cleanup record for a reboot proof before it checks any old process id, so an attempt whose cleanup failed twice (the turn record settled in between) still clears after a reboot, with nothing signalled and the completion kept. Regressions: TestWorkerLaunchHoldsTheTurnThroughCleanup, TestCleanupFailedTwiceClearsAfterAReboot, TestAnotherAttemptsPendingProvesNothing. mac stays under 5000 lines (settleLimit via cmp.Or, slices.Contains for the gate's list).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+178
−22
internal/runner/mac/gate.go+2 −10internal/runner/mac/launch.go+11 −6internal/runner/mac/wbfix3_test.go+85 −0internal/runner/thin/wbfix3_launch_test.go+64 −0internal/runner/thin/worker_launch.go+16 −6
diff --git a/internal/runner/mac/gate.go b/internal/runner/mac/gate.go| index e42553ce..a31d7661 100644 |
| --- a/internal/runner/mac/gate.go |
| +++ b/internal/runner/mac/gate.go |
| @@ -7,6 +7,7 @@ import ( |
| "os" |
| "os/exec" |
| "path/filepath" |
| + "slices" |
| "strconv" |
| "strings" |
| "sync" |
| @@ -204,7 +205,7 @@ func (g *Gate) Run(ctx context.Context, attempt, dir string, argv []string, limi |
| res.Terminated = run.Stop() |
| if g.Booted != nil && g.Shutdown != nil { |
| for _, udid := range g.Booted(context.WithoutCancel(ctx)) { |
| - if !contains(before, udid) { |
| + if !slices.Contains(before, udid) { |
| g.Shutdown(context.WithoutCancel(ctx), udid) |
| } |
| } |
| @@ -241,15 +242,6 @@ func exitCode(err error) int { |
| return -1 |
| } |
| -func contains(list []string, s string) bool { |
| - for _, x := range list { |
| - if x == s { |
| - return true |
| - } |
| - } |
| - return false |
| -} |
| - |
| // 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. |
diff --git a/internal/runner/mac/launch.go b/internal/runner/mac/launch.go| index ea360a42..be277714 100644 |
| --- a/internal/runner/mac/launch.go |
| +++ b/internal/runner/mac/launch.go |
| @@ -95,12 +95,10 @@ func (x *Executor) own() *Own { |
| // cleanLimit bounds the user-temp cleanup (a simulator shutdown). |
| func (x *Executor) cleanLimit() time.Duration { return cmp.Or(x.CleanLimit, 3*time.Minute) } |
| -func (x *Executor) settleLimit() time.Duration { |
| - if x.Settle > 0 { |
| - return x.Settle |
| - } |
| - return 30 * time.Second |
| -} |
| +// StopBudget is the most a stopped run's Wait takes: the proof, then the cleanup. |
| +func (x *Executor) StopBudget() time.Duration { return x.settleLimit() + x.cleanLimit() } |
| + |
| +func (x *Executor) settleLimit() time.Duration { return cmp.Or(max(x.Settle, 0), 30*time.Second) } |
| // validMarker: a marker fits a record ("pg:<pgid>:<marker>") and a |
| // ps -E word. |
| @@ -549,6 +547,13 @@ func (x *Executor) Verify(id string) bool { |
| return false |
| } |
| } |
| + if held != nil && !rebooted { |
| + // The pending cleanup's record keeps the attempt's boot when its |
| + // turn's record is gone (settled, then the cleanup failed again). |
| + r, pending, err := x.Turns.readPending() |
| + b, berr := x.Turns.bootNow() |
| + rebooted = err == nil && pending && r.Attempt == attempt && proc.Rebooted(r.BootID, r.Began, b, berr) |
| + } |
| // A reboot is a boot-qualified clearance: the old boot's group id is |
| // this boot's to use, and nothing is signalled. |
| if !met || !rebooted && !proc.SettleMarked(x.procs(), x.own(), pgid, attempt, deadline) || !time.Now().Before(deadline) { |
diff --git a/internal/runner/mac/wbfix3_test.go b/internal/runner/mac/wbfix3_test.go| new file mode 100644 |
| index 00000000..e4d503dd |
| --- /dev/null |
| +++ b/internal/runner/mac/wbfix3_test.go |
| @@ -0,0 +1,85 @@ |
| +package mac |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "os/exec" |
| + "path/filepath" |
| + "syscall" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/runner/mac/proc" |
| +) |
| + |
| +// Whole-branch fix 3, #2: the cleanup fails, a same-boot proof settles |
| +// the turn's record but fails the cleanup again, and then the Mac |
| +// reboots. Only the pending cleanup record still carries the attempt's |
| +// boot: Verify checks its reboot proof before any old process id, so a |
| +// long-lived process now leading the old group number is never checked |
| +// or signalled, and Retry is allowed (the completion kept). |
| +func TestCleanupFailedTwiceClearsAfterAReboot(t *testing.T) { |
| + g, dir := gateFixture(t, true) |
| + _, temp := userTempFixture(t) |
| + g.UserTemp, g.Turns = temp, NewTurns(filepath.Join(t.TempDir(), "lock")) |
| + boot := proc.Boot{ID: "boot-1", Time: time.Now().Add(-time.Hour)} |
| + g.Turns.boot = func() (proc.Boot, error) { return boot, nil } |
| + g.QuietSimulators = func(context.Context) { _ = os.Chmod(temp, 0) } |
| + t.Cleanup(func() { _ = os.Chmod(temp, 0o700) }) |
| + res, id := run(g, dir, "checks-wb3-2", time.Minute, `touch `+quote(filepath.Join(temp, "left"))) |
| + if res.Terminated || !g.Turns.Pending() { |
| + t.Fatalf("%+v pending=%v", res, g.Turns.Pending()) |
| + } |
| + // A same-boot proof: the turn's record is settled, the cleanup fails |
| + // again. |
| + if g.Verify(id) { |
| + t.Fatal("proven with its cleanup failing again") |
| + } |
| + if !g.Turns.Pending() { |
| + t.Fatal("the pending cleanup went") |
| + } |
| + // The reboot; the old group number now leads a long-lived process. |
| + g.QuietSimulators = nil |
| + if err := os.Chmod(temp, 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + boot = proc.Boot{ID: "boot-2", Time: time.Now()} |
| + other := exec.Command("/bin/sleep", "60") |
| + other.SysProcAttr = &syscall.SysProcAttr{Setsid: true} |
| + if err := other.Start(); err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { _ = other.Process.Kill(); _ = other.Wait() }) |
| + _, attempt, _ := proc.ParseRecord(id) |
| + reused := RecordID(other.Process.Pid, attempt) |
| + if !g.Verify(reused) || g.Turns.Pending() || exists(filepath.Join(temp, "left")) { |
| + t.Fatal("a reboot did not clear the attempt whose cleanup failed twice") |
| + } |
| + if err := other.Process.Signal(syscall.Signal(0)); err != nil { |
| + t.Fatalf("the process now holding the old group number was signalled: %v", err) |
| + } |
| + if !g.Verify(reused) { |
| + t.Fatal("the reboot's completion was not kept for the next consumer") |
| + } |
| +} |
| + |
| +// Whole-branch fix 3, #2: only the attempt's own pending cleanup can |
| +// prove its reboot. Another attempt's, from an earlier boot, says |
| +// nothing of this one, whose marked process is still there. |
| +func TestAnotherAttemptsPendingProvesNothing(t *testing.T) { |
| + g, dir := gateFixture(t, true) |
| + g.Turns, g.Settle = NewTurns(filepath.Join(t.TempDir(), "lock")), 500*time.Millisecond |
| + boot := proc.Boot{ID: "boot-2", Time: time.Now()} |
| + g.Turns.boot = func() (proc.Boot, error) { return boot, nil } |
| + g.procs = withPhantom("checks-wb3-3") |
| + res, id := run(g, dir, "checks-wb3-3", time.Minute, `true`) |
| + if res.Terminated { |
| + t.Fatalf("%+v", res) |
| + } |
| + if err := g.Turns.writePending(pendingRecord{Attempt: "checks-old", Runner: ProcID{PID: 1 << 30, Start: 1}, Began: time.Now().Add(-2 * time.Hour), BootID: "boot-1"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if g.Verify(id) { |
| + t.Fatal("proven by another attempt's reboot proof, its own marked process still there") |
| + } |
| +} |
diff --git a/internal/runner/thin/wbfix3_launch_test.go b/internal/runner/thin/wbfix3_launch_test.go| new file mode 100644 |
| index 00000000..f21d18f5 |
| --- /dev/null |
| +++ b/internal/runner/thin/wbfix3_launch_test.go |
| @@ -0,0 +1,64 @@ |
| +//go:build testbuild |
| + |
| +package thin |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "path/filepath" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/runner/mac" |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// Whole-branch fix 3, #1: a launch keeps the Mac's turn, and its |
| +// recovery evidence, until the executor's Wait (stop, proof, cleanup) |
| +// has returned, even when that runs past the launcher's wait. Here the |
| +// simulator shutdown ignores its deadline and outlasts every bound: the |
| +// turn is never free while it runs, and the attempt ends unterminated |
| +// only once it has returned. |
| +func TestWorkerLaunchHoldsTheTurnThroughCleanup(t *testing.T) { |
| + x := newEngRig(t) |
| + x.note("edit:Swipe.swift|false|true") |
| + temp := filepath.Join(t.TempDir(), "T") |
| + if err := os.Mkdir(temp, 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + var stole, ended atomic.Bool |
| + x.w.Eng.XcodeExec = &mac.Executor{Turns: x.m.turns, UserTemp: temp, Settle: time.Second, CleanLimit: 300 * time.Millisecond, |
| + QuietSimulators: func(context.Context) { |
| + for deadline := time.Now().Add(2500 * time.Millisecond); time.Now().Before(deadline); time.Sleep(100 * time.Millisecond) { |
| + if u, ok := x.m.turns.TryTake(mac.OpCheck); ok { |
| + stole.Store(true) |
| + u.Give() |
| + } |
| + } |
| + ended.Store(true) |
| + }} |
| + x.w.Eng.Xcode = func() bool { return true } |
| + // The policy names the Mac's T by its real prefix; the executor |
| + // cleans the test's own. |
| + x.m.paths.UserTemp = mac.UserTempPrefix + "/wb/T" |
| + x.r.XcodeAccess = wire.XcodeAccessSandboxed |
| + // The launcher's wait (a 200 ms grace, then the budgets plus no |
| + // margin, 1.3 s) passes long |
| + // before the cleanup returns. |
| + was, grace := macStopMargin, macStopGrace |
| + macStopMargin, macStopGrace = 0, 200*time.Millisecond |
| + t.Cleanup(func() { macStopMargin, macStopGrace = was, grace }) |
| + rep := x.run(t) |
| + if !ended.Load() || stole.Load() { |
| + t.Fatalf("the cleanup was still running (%v) or the turn was taken during it (%v)", !ended.Load(), stole.Load()) |
| + } |
| + if rep.Termination || rep.State == wire.StepDone { |
| + t.Fatalf("report %+v", rep) |
| + } |
| + if u, ok := x.m.turns.TryTake(mac.OpCheck); !ok { |
| + t.Fatal("the turn was not given back") |
| + } else { |
| + u.Give() |
| + } |
| +} |
diff --git a/internal/runner/thin/worker_launch.go b/internal/runner/thin/worker_launch.go| index 5eef3f90..9d5a83d2 100644 |
| --- a/internal/runner/thin/worker_launch.go |
| +++ b/internal/runner/thin/worker_launch.go |
| @@ -19,10 +19,16 @@ import ( |
| "amadan.net/oficina/equipo/internal/wire" |
| ) |
| -// macStopLimit bounds how long a stopped Mac launch may take to prove its |
| -// end: the executor's settle (its grace inside it), then, with sandboxed |
| -// Xcode access, simctl shutdown all and the user-temp cleanup. |
| -var macStopLimit = 30*time.Second + simShutdownLimit + 30*time.Second |
| +// macStopMargin is what a stopped Mac launch may take beyond its |
| +// executor's own budgets (Executor.StopBudget: the 30 s termination |
| +// proof, then the user-temp cleanup's CleanLimit) before it counts as |
| +// unproven. Either way the launch waits for the executor's Wait to |
| +// return before it gives the turn back. |
| +var macStopMargin = 30 * time.Second |
| + |
| +// macStopGrace is how long a launch that ended its turn is given to |
| +// exit by itself before it is stopped. |
| +var macStopGrace = 10 * time.Second |
| // launch is Engineer.run on a Mac worker: the pinned CLI through the |
| // darwin launch (mac.Starter), its implement policy (or, for a question, |
| @@ -176,10 +182,11 @@ func (w *Worker) launch(ctx context.Context, r stepRow, tree workspace.TaskTree, |
| e.log("worker keychain", m.after(marker)) |
| return done(wire.ReasonEngineeringUnavailable) |
| } |
| - exited := make(chan error, 1) |
| + exited, waited := make(chan error, 1), make(chan struct{}) |
| go func() { |
| _, err := proc.Wait(context.Background()) |
| exited <- err |
| + close(waited) |
| }() |
| go func() { _, _ = io.Copy(io.Discard, io.LimitReader(proc.Stderr(), 1<<20)) }() |
| frames := make(chan []byte, 64) |
| @@ -206,7 +213,10 @@ func (w *Worker) launch(ctx context.Context, r stepRow, tree workspace.TaskTree, |
| var shell shellUse |
| res := e.converse(ctx, proc, session, policy, model, frames, logFile, limit, spec.prompt, nil, spec.stream, spec.activity, &shell) |
| res.session, res.refused, res.noShell = session, refused.Load(), shell.none() |
| - res.terminated = stopStep(func() { proc.Stdin().Close() }, exited, kill, ctx.Err() == nil && res.reason == "", 10*time.Second, macStopLimit) |
| + res.terminated = stopStep(func() { proc.Stdin().Close() }, exited, kill, ctx.Err() == nil && res.reason == "", macStopGrace, exec.StopBudget()+macStopMargin) |
| + // Past that wait too, the turn and the evidence stay until the stop, |
| + // its proof and the cleanup have returned (they are bounded there). |
| + <-waited |
| if !res.terminated { |
| // The record and the Keychain watch stay: a restart settles them. |
| m.keychain.Abandon(marker) |