Merge branch 'worktree-agent-ae57d5ca18be911ad'
This commit is contained in:
+12
-2
@@ -608,7 +608,9 @@ func piResult(data []byte) (string, error) {
|
||||
}
|
||||
|
||||
// InterruptPi expires the retained lease. The explicit extension aborts tools and
|
||||
// marks quiescence before shutting down; without that receipt recovery fails closed.
|
||||
// marks quiescence before shutting down. An owner whose process and process
|
||||
// group are both gone (a crash, a reboot) has nothing left to stop, so it
|
||||
// counts as interrupted without that receipt; otherwise recovery fails closed.
|
||||
func InterruptPi(ctx context.Context, reference string) error {
|
||||
if !filepath.IsAbs(reference) {
|
||||
return fmt.Errorf("invalid retained Pi reference")
|
||||
@@ -623,6 +625,12 @@ func InterruptPi(ctx context.Context, reference string) error {
|
||||
if json.Unmarshal(data, &owner) != nil || owner.PID <= 1 {
|
||||
return errors.Join(fmt.Errorf("invalid retained Pi owner"), ErrStageCleanup)
|
||||
}
|
||||
gone := func() bool {
|
||||
return errors.Is(syscall.Kill(owner.PID, 0), syscall.ESRCH) && errors.Is(syscall.Kill(-owner.PID, 0), syscall.ESRCH)
|
||||
}
|
||||
if gone() {
|
||||
return nil
|
||||
}
|
||||
old := time.Unix(0, 0)
|
||||
if err := os.Chtimes(filepath.Join(reference, "lease"), old, old); err != nil {
|
||||
return err
|
||||
@@ -630,7 +638,9 @@ func InterruptPi(ctx context.Context, reference string) error {
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
if data, err := os.ReadFile(filepath.Join(reference, "quiescent")); err == nil && string(data) == "quiescent" && errors.Is(syscall.Kill(owner.PID, 0), syscall.ESRCH) && errors.Is(syscall.Kill(-owner.PID, 0), syscall.ESRCH) {
|
||||
// The receipt is written just before a clean exit; a process that
|
||||
// died without it is still gone.
|
||||
if gone() {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
|
||||
@@ -42,3 +42,31 @@ func TestPiReceiptRequiresProcessExit(t *testing.T) {
|
||||
t.Fatal("confirmed process exit not accepted", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A Pi owner that crashed never wrote its quiescence receipt; with its process
|
||||
// and process group gone there is nothing left to stop, so recovery must not
|
||||
// wait out its timeout and fail.
|
||||
func TestPiOwnerGoneWithoutReceiptCountsAsInterrupted(t *testing.T) {
|
||||
cmd := exec.Command("sleep", "5")
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pid := cmd.Process.Pid
|
||||
if err := cmd.Process.Kill(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd.Wait()
|
||||
dir := t.TempDir()
|
||||
owner, _ := json.Marshal(map[string]any{"pid": pid})
|
||||
for name, data := range map[string][]byte{"owner.json": owner, "lease": []byte("lease")} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
if err := InterruptPi(ctx, dir); err != nil {
|
||||
t.Fatal("a crashed owner was not accepted as gone:", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user