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") } }