hostedctl: deadline stops first; Reopen and acks wait out a restart
B2-1 (R26): each reaper pass sends the deadline stop of every recorded job past its stop time before replaying intents, so slow launch calls never delay it; step 4 repeats idempotently. B2-3: Reopen refuses (ErrConflict) until Refresh has re-based a job that predates the restart, then judges the corrected stop time. B2-4: an unacknowledged pause generation drained at or before OpenedAt is cancelled, before and after the first Refresh, so a late quiescent cannot authorise a suspend. The simulation model treats such an ack as never in time, so I5 catches a controller that accepts one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+180
−19
docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md+19 −0internal/hostedctl/pause.go+28 −14internal/hostedctl/pause_test.go+58 −0internal/hostedctl/reaper.go+32 −2internal/hostedctl/reaper_test.go+38 −0internal/hostedctl/resume.go+1 −1internal/hostedctl/sim_test.go+4 −2
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md| index 7f98b29b..0649323a 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md |
| @@ -935,3 +935,22 @@ Findings from the final whole-branch review (final-A, B1, B2, C), fixed under ru |
| | --- | --- | --- | --- | |
| | Drop both post-marker checks | `go test ./internal/hostedctl -run 'TestDispatchRechecksGatesAfterMarker$' -count=1` | `launch_test.go:672: provider launches = 1, want 0` in all three subtests | yes, passes | |
| | Drop only the stop-time check | same | `launch_test.go:675: att-1 = {state:waiting ... lastDispatch:{Int64:7200000000000 Valid:true} ... stop:6600000000000}, want abandoned with its dispatch marker` | yes, passes | |
| + |
| +### B2-1, B2-3, B2-4: deadline stops before replay, Reopen after reconciliation, pauses across a restart |
| + |
| +- B2-1 (R26): `reapOverdue` runs right after the pass's checkpoint and sends the deadline stop |
| + (`reapJob`) for every Ref'd, unproven, unrequested job at or past its stop time, before Replay. |
| + Step 4 repeats and finds the stop already asked for. |
| +- B2-3: `Reopen` refuses with `isolatedjob.ErrConflict` while `ageUnsettled`, then judges the |
| + re-based stop time. |
| +- B2-4: `expire` (now a method) also cancels an unacknowledged generation whose Drain is at or |
| + before `OpenedAt` after a restart. The simulation's model now counts an acknowledgement of a |
| + generation drained before the last restart as never in time, so I5 would catch a controller that |
| + accepted one. |
| + |
| +| Mutation | Command | Observed failure | Restored | |
| +| --- | --- | --- | --- | |
| +| Drop `reapOverdue` from the pass | `go test ./internal/hostedctl -run 'TestDeadlineStopBeforeSlowReplay$' -count=1` | `reaper_test.go:393: provider stops while the replay's launch call hung = [], want att-1's vm-0001` | yes, passes | |
| +| Drop the `ageUnsettled` refusal in `Reopen` | `go test ./internal/hostedctl -run 'TestReopenWaitsForRestartReconciliation$' -count=1` | `pause_test.go:236: Reopen before the first refresh = <nil>, want ErrConflict` | yes, passes | |
| +| Drop the restart case in `expire` | `go test ./internal/hostedctl -run 'TestPauseAckAcrossRestartCancelled$' -count=1` | `pause_test.go:209: refresh first false: Quiescent(1) after the restart = <nil>, want ErrGenerationCancelled` | yes, passes | |
| +| Same mutation, simulation | `EQUIPO_SIM_SEEDS=500 go test ./internal/hostedctl -run 'TestLifecycleSimulation' -count=1` | `sim_test.go:94: seed 12 failed at step 38: I5: a3 generation 1 is stored acknowledged; no acknowledgement of it came in time` (also seeds 50, 106) | yes, 500 seeds pass | |
diff --git a/internal/hostedctl/pause.go b/internal/hostedctl/pause.go| index eea4ca20..0e3993ca 100644 |
| --- a/internal/hostedctl/pause.go |
| +++ b/internal/hostedctl/pause.go |
| @@ -141,13 +141,22 @@ func notPausable(j *pauseJob) error { |
| } |
| // expire cancels j's open generation, inside tx, when it is draining with no |
| -// quiescent acknowledgement and more than PauseAckTimeout of controller time |
| -// has passed since Drain. Every call that touches a generation runs it |
| -// first, so cancellation needs no timer; the reaper may sweep too. A |
| -// generation acknowledged in time never expires. |
| -func expire(ctx context.Context, tx *sql.Tx, j *pauseJob, now T) error { |
| - if j.state != JobDraining || j.cancelled || j.quiescent || !j.started.Valid || |
| - now-T(j.started.Int64) <= T(PauseAckTimeout) { |
| +// quiescent acknowledgement and either more than PauseAckTimeout of |
| +// controller time has passed since Drain, or Drain came before this process |
| +// opened the store after a restart (at or before OpenedAt): the downtime is |
| +// unknown, so the time since Drain is too, and a late acknowledgement could |
| +// pass for an early one. Every call that touches a generation runs it first, |
| +// so cancellation needs no timer; the reaper may sweep too. A generation |
| +// acknowledged in time never expires. |
| +func (c *Controller) expire(ctx context.Context, tx *sql.Tx, j *pauseJob, now T) error { |
| + if j.state != JobDraining || j.cancelled || j.quiescent || !j.started.Valid { |
| + return nil |
| + } |
| + why := fmt.Sprintf("no quiescent acknowledgement within %v", PauseAckTimeout) |
| + switch { |
| + case c.store.Restarted() && T(j.started.Int64) <= c.store.OpenedAt(): |
| + why = "opened before the controller restarted and not acknowledged; the downtime is unknown" |
| + case now-T(j.started.Int64) <= T(PauseAckTimeout): |
| return nil |
| } |
| res, err := tx.ExecContext(ctx, `UPDATE jobs SET pause_cancelled = 1, revision = revision + 1 |
| @@ -160,8 +169,7 @@ func expire(ctx context.Context, tx *sql.Tx, j *pauseJob, now T) error { |
| } |
| j.cancelled = true |
| return appendAudit(tx, Audit{Account: j.account, Task: j.task, Attempt: j.attempt, Action: actionPauseCancelled, |
| - Reason: fmt.Sprintf("generation %d: no quiescent acknowledgement within %v; a late one is ignored", |
| - j.generation, PauseAckTimeout), At: now}) |
| + Reason: fmt.Sprintf("generation %d: %s; a late acknowledgement is ignored", j.generation, why), At: now}) |
| } |
| // Drain opens a new pause generation for attemptID and returns it: the |
| @@ -222,7 +230,7 @@ func (c *Controller) Quiescent(ctx context.Context, by hostedwire.KeyID, key hos |
| if r := notPausable(j); r != nil { |
| return r, nil |
| } |
| - if err := expire(ctx, tx, j, a.At); err != nil { |
| + if err := c.expire(ctx, tx, j, a.At); err != nil { |
| return nil, err |
| } |
| switch { |
| @@ -267,7 +275,7 @@ func (c *Controller) Suspend(ctx context.Context, by hostedwire.KeyID, key hoste |
| if r := notPausable(j); r != nil { |
| return r, nil |
| } |
| - if err := expire(ctx, tx, j, a.At); err != nil { |
| + if err := c.expire(ctx, tx, j, a.At); err != nil { |
| return nil, err |
| } |
| if !currentGen(generation, j) { |
| @@ -329,8 +337,11 @@ func (c *Controller) sendSuspend(ctx context.Context, actor hostedwire.KeyID, j |
| // Reopen records that the runner takes work again after a cancelled or |
| // abandoned pause of generation, the current one (else |
| // ErrGenerationCancelled). The job returns to JobRunning and its pause |
| -// fields are cleared; the generation counter stays. Once the job's stop |
| -// time has come admission never reopens (ErrPastDeadline). A job already |
| +// fields are cleared; the generation counter stays. A job whose times wait |
| +// for the first Refresh after a restart (ageUnsettled) gets |
| +// isolatedjob.ErrConflict, so its stop time is judged only once re-based. |
| +// Once the job's stop time has come admission never reopens |
| +// (ErrPastDeadline). A job already |
| // running at that generation gets nil; a suspending, suspended or resuming |
| // job gets isolatedjob.ErrConflict, as its VM is paused and needs Resume. |
| func (c *Controller) Reopen(ctx context.Context, by hostedwire.KeyID, key hostedwire.VerifyKey, attemptID string, generation int64) error { |
| @@ -339,12 +350,15 @@ func (c *Controller) Reopen(ctx context.Context, by hostedwire.KeyID, key hosted |
| if r := notPausable(j); r != nil { |
| return r, nil |
| } |
| - if err := expire(ctx, tx, j, a.At); err != nil { |
| + if err := c.expire(ctx, tx, j, a.At); err != nil { |
| return nil, err |
| } |
| if !currentGen(generation, j) { |
| return fmt.Errorf("%w: generation %d is not the current one (%d)", ErrGenerationCancelled, generation, j.generation), nil |
| } |
| + if c.ageUnsettled(j) { |
| + return fmt.Errorf("%w: the job's times wait for the first refresh after a restart; ask again", isolatedjob.ErrConflict), nil |
| + } |
| if pastStop(j, a.At) { |
| return fmt.Errorf("%w: %v ago; admission stays closed", ErrPastDeadline, time.Duration(a.At-T(j.stopT))), nil |
| } |
diff --git a/internal/hostedctl/pause_test.go b/internal/hostedctl/pause_test.go| index 53d3df33..660836ab 100644 |
| --- a/internal/hostedctl/pause_test.go |
| +++ b/internal/hostedctl/pause_test.go |
| @@ -189,6 +189,64 @@ func TestLateAckIgnored(t *testing.T) { |
| } |
| } |
| +// TestPauseAckAcrossRestartCancelled: drain, then a restart whose ten |
| +// minutes of downtime the wall clock hides. The old generation's |
| +// acknowledgement must be refused, before the first Refresh and after it, |
| +// and its Suspend too: controller time cannot tell it was late. A |
| +// generation opened after the restart is acknowledged as usual. |
| +func TestPauseAckAcrossRestartCancelled(t *testing.T) { |
| + t.Parallel() |
| + for _, refreshFirst := range []bool{false, true} { |
| + e := runningJob(t) |
| + g := e.drain() |
| + e.down() |
| + e.clk.AdvanceMono(10 * time.Minute) |
| + e.restart() |
| + if refreshFirst { |
| + e.refresh() |
| + } |
| + if err := e.c.Quiescent(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrGenerationCancelled) { |
| + t.Fatalf("refresh first %v: Quiescent(%d) after the restart = %v, want ErrGenerationCancelled", refreshFirst, g, err) |
| + } |
| + if err := e.c.Suspend(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrGenerationCancelled) { |
| + t.Fatalf("refresh first %v: Suspend(%d) = %v, want ErrGenerationCancelled", refreshFirst, g, err) |
| + } |
| + if p := e.pauseCols("att-1"); !p.cancelled || p.quiescent || p.state != JobDraining || e.fake.Calls(isolatedjob.OpSuspend) != 0 { |
| + t.Fatalf("refresh first %v: %+v, want the generation cancelled and no suspend", refreshFirst, p) |
| + } |
| + e.clk.AdvanceMono(time.Second) |
| + e.ack(e.drain()) |
| + } |
| +} |
| + |
| +// TestReopenWaitsForRestartReconciliation: a job drains at 1 h 40 min of |
| +// its 1 h 50 min to stop; a restart hides 15 minutes of downtime. Reopen |
| +// before the first Refresh must not open admission against the stale stop |
| +// time; once Refresh re-bases the job on the provider's age, Reopen sees |
| +// the stop time has passed. |
| +func TestReopenWaitsForRestartReconciliation(t *testing.T) { |
| + t.Parallel() |
| + e := runningJob(t) |
| + e.clk.AdvanceMono(100 * time.Minute) |
| + g := e.drain() |
| + e.down() |
| + e.clk.AdvanceMono(15 * time.Minute) |
| + e.restart() |
| + if err := e.c.Reopen(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, isolatedjob.ErrConflict) { |
| + t.Fatalf("Reopen before the first refresh = %v, want ErrConflict", err) |
| + } |
| + if p := e.pauseCols("att-1"); p.state != JobDraining { |
| + t.Fatalf("att-1 after the refused Reopen is %q, want draining", p.state) |
| + } |
| + e.refresh() |
| + if err := e.c.Reopen(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrPastDeadline) { |
| + t.Fatalf("Reopen after the re-base = %v, want ErrPastDeadline", err) |
| + } |
| + if p := e.pauseCols("att-1"); p.state != JobDraining { |
| + t.Fatalf("att-1 after Reopen past its stop time is %q, want draining", p.state) |
| + } |
| +} |
| + |
| func TestSuspendedTimeCountsToDeadline(t *testing.T) { |
| t.Parallel() |
| e := runningJob(t) |
diff --git a/internal/hostedctl/reaper.go b/internal/hostedctl/reaper.go| index 70f87a3e..500959c3 100644 |
| --- a/internal/hostedctl/reaper.go |
| +++ b/internal/hostedctl/reaper.go |
| @@ -206,7 +206,9 @@ func (c *Controller) Loop(ctx context.Context, every time.Duration) error { |
| // Reap runs one reaper pass (spec §10). Passes run one at a time. In order: |
| // |
| // 1. Checkpoint controller time. A failure ends the pass: the store is |
| -// needed for everything after it. |
| +// needed for everything after it. Then every recorded job past its |
| +// stop time gets its deadline stop (reapOverdue, R26), so the slow |
| +// launch calls of step 2 never hold it up. |
| // 2. Replay launch intents, with their gates (Replay), so a VM a lost |
| // launch reply made is claimed before anything looks for orphans. |
| // 3. Refresh. |
| @@ -241,7 +243,7 @@ func (c *Controller) reapPass(ctx context.Context, rep *ReapReport) error { |
| if err := c.store.Checkpoint(ctx); err != nil { |
| return fmt.Errorf("hostedctl: reap: clock checkpoint: %w", err) |
| } |
| - var errs []error |
| + errs := []error{c.reapOverdue(ctx, rep)} |
| n, err := c.replayCounted(ctx) |
| rep.Replayed += n |
| errs = append(errs, err) |
| @@ -283,6 +285,34 @@ func (c *Controller) replayCounted(ctx context.Context) (int, error) { |
| return n, errors.Join(rerr, err) |
| } |
| +// reapOverdue is step 1b (ruling R26): every job with a saved Ref, no proof |
| +// and no stop request whose stop time has come gets its deadline stop |
| +// (reapJob) before Replay, so launch calls, each up to |
| +// ProviderLaunchTimeout, never delay it. Re-basing after a restart only |
| +// moves stop times earlier, so this never stops a job early; step 4 finds |
| +// the stop already asked for. |
| +func (c *Controller) reapOverdue(ctx context.Context, rep *ReapReport) error { |
| + var ids []string |
| + err := c.store.Tx(ctx, func(tx *sql.Tx) error { |
| + var err error |
| + ids, err = textColumn(ctx, tx, `SELECT attempt_id FROM jobs WHERE vm_ref IS NOT NULL AND stop_proof_json IS NULL |
| + AND stop_requested_t IS NULL AND stop_t <= ? ORDER BY launched_t, attempt_id`, int64(c.store.Clock().Now())) |
| + return err |
| + }) |
| + if err != nil { |
| + return err |
| + } |
| + var errs []error |
| + for _, id := range ids { |
| + if err := ctx.Err(); err != nil { |
| + return errors.Join(append(errs, err)...) |
| + } |
| + _, err := c.reapJob(ctx, id, rep) |
| + errs = append(errs, err) |
| + } |
| + return errors.Join(errs...) |
| +} |
| + |
| // reapJobs is step 4: every job with a saved Ref and no proof, in launch |
| // order. It reads no List, so an incomplete one never holds up a deadline. |
| func (c *Controller) reapJobs(ctx context.Context, rep *ReapReport) error { |
diff --git a/internal/hostedctl/reaper_test.go b/internal/hostedctl/reaper_test.go| index dd427ae6..421442ed 100644 |
| --- a/internal/hostedctl/reaper_test.go |
| +++ b/internal/hostedctl/reaper_test.go |
| @@ -359,6 +359,44 @@ func TestDeadlineEnforcedDuringIncompleteLists(t *testing.T) { |
| } |
| } |
| +// TestDeadlineStopBeforeSlowReplay: a pass that finds a job past its stop |
| +// time sends its deadline stop before replaying intents, so a launch call |
| +// that hangs (up to ProviderLaunchTimeout each) never delays it (R26). |
| +func TestDeadlineStopBeforeSlowReplay(t *testing.T) { |
| + t.Parallel() |
| + e := newLaunchEnv(t) |
| + ref := e.launchWith("att-1", 2*time.Hour) |
| + e.fake.Inject(isolatedjob.OpLaunch, isolatedjob.FaultThrottle) |
| + q := request("att-2") |
| + q.MaxLifetime = 8 * time.Hour |
| + if v, err := e.c.Launch(e.ctx, "inst-1", instKey, q); err != nil || v.State != JobWaiting { |
| + t.Fatalf("throttled Launch(att-2) = (%+v, %v), want a waiting intent", v, err) |
| + } |
| + hold := &holdRunner{Fake: e.fake, entered: make(chan struct{}), release: make(chan struct{})} |
| + e.run = hold |
| + e.newController() |
| + e.clk.AdvanceMono(2 * time.Hour) // att-1 is past its stop time; att-2 is not |
| + e.audit() |
| + done := make(chan error, 1) |
| + go func() { _, err := e.c.Reap(e.ctx); done <- err }() |
| + select { |
| + case <-hold.entered: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the pass's replay never reached the provider") |
| + } |
| + stops := e.fake.StopsIssued() |
| + close(hold.release) |
| + if err := <-done; err != nil { |
| + t.Fatalf("Reap = %v", err) |
| + } |
| + if !slices.Equal(stops, []isolatedjob.Ref{ref}) { |
| + t.Fatalf("provider stops while the replay's launch call hung = %v, want att-1's %s", stops, ref) |
| + } |
| + if s := e.stopCols("att-1"); s.reason.String != reasonDeadline { |
| + t.Fatalf("att-1 stop reason = %v, want %s", s.reason, reasonDeadline) |
| + } |
| +} |
| + |
| func TestReapDrainsThenStops(t *testing.T) { |
| t.Parallel() |
| e := newLaunchEnv(t) |
diff --git a/internal/hostedctl/resume.go b/internal/hostedctl/resume.go| index 81e270f2..935f73e2 100644 |
| --- a/internal/hostedctl/resume.go |
| +++ b/internal/hostedctl/resume.go |
| @@ -466,7 +466,7 @@ func (c *Controller) mirrorLive(ctx context.Context, j jobRow, st isolatedjob.St |
| a := Audit{Account: p.account, Task: p.task, Attempt: p.attempt, At: now} |
| switch { |
| case p.state == JobDraining: |
| - return expire(ctx, tx, &p, now) |
| + return c.expire(ctx, tx, &p, now) |
| case p.state == JobSuspending && st == isolatedjob.StateSuspended: |
| if _, err := tx.ExecContext(ctx, `UPDATE jobs SET state = ?, revision = revision + 1 |
| WHERE attempt_id = ? AND state = ?`, JobSuspended, p.attempt, JobSuspending); err != nil { |
diff --git a/internal/hostedctl/sim_test.go b/internal/hostedctl/sim_test.go| index ce6d7af5..3517d247 100644 |
| --- a/internal/hostedctl/sim_test.go |
| +++ b/internal/hostedctl/sim_test.go |
| @@ -770,10 +770,12 @@ func (w *simWorld) drain(id string) (int64, error) { |
| } |
| // ack sends Quiescent for gen and records an in-time acknowledgement of |
| -// the current generation in the model's history. |
| +// the current generation in the model's history. A generation drained |
| +// before the last restart is never in time: the downtime is unknown. |
| func (w *simWorld) ack(id string, gen int64) error { |
| err := w.c.Quiescent(w.ctx, simActor, instKey, id, gen) |
| - if p := w.m.pause[id]; err == nil && p != nil && p.open && gen == p.gen && w.now()-p.drainedAt <= T(PauseAckTimeout) { |
| + if p := w.m.pause[id]; err == nil && p != nil && p.open && gen == p.gen && w.now()-p.drainedAt <= T(PauseAckTimeout) && |
| + !(w.restarted && p.drainedAt <= w.openedAt) { |
| p.acked = true |
| } |
| return err |