oficina / equipo Public

hostedctl: recheck launch gates after the dispatch marker commits

B1: dispatch checked its gates, then waited for the store to record its
dispatch marker; the stop time could pass or the audit fail meanwhile.
Once the marker commits, dispatch checks the stop time (abandoning the
intent) and the transient gates (leaving it waiting) again, in memory,
immediately before the provider call. A test hook holds the store at the
marker while each gate is invalidated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 4554729bd073c09102f198a258e24f672a7521af parent c53d99b
4 files changed, +108 −2
  • docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md +14 −0
  • internal/hostedctl/controller.go +4 −0
  • internal/hostedctl/launch.go +45 −2
  • internal/hostedctl/launch_test.go +45 −0
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
index cf2f9e27..7f98b29b 100644
--- a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
+++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
@@ -921,3 +921,17 @@ Findings from the final whole-branch review (final-A, B1, B2, C), fixed under ru
| Accept a mismatched binding (`bound != ns && false`) | `go test ./internal/hostedctl -run 'TestProviderNamespaceBound$' -count=1` | `store_test.go:605: New for another namespace = (&{...test:account-2:region-1...}, <nil>), want ErrProviderMismatch` | yes, passes |
| Skip the fingerprint check in `dispatch` | `go test ./internal/hostedctl -run 'TestReplayRefusesChangedProviderConfig$' -count=1` | `launch_test.go:555: provider launches = 2, want only the lost one` | yes, passes |
| Leave the version out of `awsjob.Config.Fingerprint` | `go test ./internal/isolatedjob/awsjob -run 'TestFingerprint$' -count=1` | `runner_test.go:252: small and large share a fingerprint, though their versions differ` | yes, passes |
+
+### B1: gates rechecked after the dispatch marker commits
+
+- After `markDispatch` commits, `dispatch` reads controller time once more, in memory: at or past
+ the intent's stop time it abandons the intent (`abandonDue`); a failed `creatable` (launches off,
+ image dropped, audit failed or stale) leaves it waiting; only then the provider call.
+- `TestDispatchRechecksGatesAfterMarker`: a test hook (`beforeMark`) holds the store so the marker's
+ transaction queues; the test invalidates one gate (stop time passes, audit fails, audit goes
+ stale), then releases the store. No provider launch in any case; the marker is recorded.
+
+| Mutation | Command | Observed failure | Restored |
+| --- | --- | --- | --- |
+| 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 |
diff --git a/internal/hostedctl/controller.go b/internal/hostedctl/controller.go
index a6cc4683..fefbf893 100644
--- a/internal/hostedctl/controller.go
+++ b/internal/hostedctl/controller.go
@@ -64,6 +64,10 @@ type Controller struct {
attempts attemptLocks // per-attempt locks (attemptlock.go)
reap reapState // the reaper's in-memory state (reaper.go)
ownership ownershipLock // dispatches against the orphan reaper's final check (reaper.go)
+ // beforeMark, when set, runs in dispatch after its gates pass and just
+ // before the dispatch marker is recorded. Tests use it to hold the store
+ // there; production leaves it nil.
+ beforeMark func()
}
// New builds a controller. It refuses a nil dependency, including a nil
diff --git a/internal/hostedctl/launch.go b/internal/hostedctl/launch.go
index c1f3ac04..dbd45a21 100644
--- a/internal/hostedctl/launch.go
+++ b/internal/hostedctl/launch.go
@@ -418,7 +418,9 @@ func checkReplaces(ctx context.Context, tx *sql.Tx, by hostedwire.KeyID, attempt
// raises an AlertConfigChanged alert. Then, outside the
// transaction and just before the call, it checks the transient gates
// (creatable); a failure is returned as gate with the waiting view and no
-// provider call. Otherwise it calls the provider and settles the answer.
+// provider call. Otherwise it records the dispatch marker (markDispatch,
+// R19), checks the stop time and the transient gates again once the marker
+// has committed, and only then calls the provider and settles the answer.
func (c *Controller) dispatch(ctx context.Context, actor hostedwire.KeyID, attempt string, replay bool) (view JobView, gate, err error) {
// The attempt's lock (R18) is held from the gate check to the settled
// answer, so a Stop cannot land between them.
@@ -429,6 +431,7 @@ func (c *Controller) dispatch(ctx context.Context, actor hostedwire.KeyID, attem
// wait for it never lets a gate that passed go stale before the call.
defer c.ownership.rlock()()
var spec isolatedjob.LaunchSpec
+ var stopAt T // the intent's stop time; the attempt lock keeps it still
ready, changed := false, false
err = c.store.Tx(ctx, func(tx *sql.Tx) error {
ready, changed = false, false
@@ -456,7 +459,7 @@ func (c *Controller) dispatch(ctx context.Context, actor hostedwire.KeyID, attem
view, err = abandonIntent(ctx, tx, actor, j, replay, reason, now)
return err
}
- view = j.view(now)
+ view, stopAt = j.view(now), T(j.stopT)
ready = true
return nil
})
@@ -472,9 +475,21 @@ func (c *Controller) dispatch(ctx context.Context, actor hostedwire.KeyID, attem
if gate := c.creatable(spec.Image, c.store.Clock().Now()); gate != nil {
return view, gate, nil
}
+ if c.beforeMark != nil {
+ c.beforeMark()
+ }
if err := c.markDispatch(ctx, attempt); err != nil {
return JobView{}, nil, err
}
+ // The marker's transaction may have queued for the store while the stop
+ // time passed or the audit failed or went stale, so both are checked
+ // again once it commits, in memory, immediately before the call.
+ if now := c.store.Clock().Now(); now >= stopAt {
+ view, err = c.abandonDue(ctx, actor, attempt, replay)
+ return view, nil, err
+ } else if gate := c.creatable(spec.Image, now); gate != nil {
+ return view, gate, nil
+ }
// R21: bounded, so no launch call this process makes can outlive the
// Ref-less proof fence (ReflessProofDelay) that starts at its dispatch.
lctx, cancel := context.WithTimeout(ctx, ProviderLaunchTimeout)
@@ -672,6 +687,34 @@ func abandonIntent(ctx context.Context, tx *sql.Tx, actor hostedwire.KeyID, j jo
Action: action, Reason: reason, At: now})
}
+// abandonDue abandons attempt's waiting intent, in a transaction of its own,
+// once dispatch finds its stop time come after the dispatch marker. An
+// intent no longer waiting is returned as it stands.
+func (c *Controller) abandonDue(ctx context.Context, actor hostedwire.KeyID, attempt string, replay bool) (JobView, error) {
+ var view JobView
+ err := c.store.Tx(ctx, func(tx *sql.Tx) error {
+ j, found, err := loadJob(ctx, tx, attempt)
+ if err != nil {
+ return err
+ }
+ if !found {
+ return fmt.Errorf("hostedctl: launch intent %s is missing", attempt)
+ }
+ now := c.store.Clock().Now()
+ reason := abandonReason(j, now)
+ if j.state != JobWaiting || j.ref.Valid || reason == "" {
+ view = j.view(now)
+ return nil
+ }
+ view, err = abandonIntent(ctx, tx, actor, j, replay, reason+"; found after the dispatch was recorded", now)
+ return err
+ })
+ if err != nil {
+ return JobView{}, err
+ }
+ return view, nil
+}
+
// alertConfigChanged sends the AlertConfigChanged alert for an intent
// abandoned because the provider settings changed, even if the caller has
// gone. The abandonment is already committed and audited.
diff --git a/internal/hostedctl/launch_test.go b/internal/hostedctl/launch_test.go
index ffb3aba2..1a248f68 100644
--- a/internal/hostedctl/launch_test.go
+++ b/internal/hostedctl/launch_test.go
@@ -633,6 +633,51 @@ func TestLaunchRechecksGatesBeforeDispatch(t *testing.T) {
}
}
+// TestDispatchRechecksGatesAfterMarker: dispatch passes its gates, then
+// waits for the store to record its dispatch marker. A gate that fails
+// while the marker waits must still stop the provider call: the stop time
+// passing abandons the intent; a failed or stale audit leaves it waiting.
+func TestDispatchRechecksGatesAfterMarker(t *testing.T) {
+ t.Parallel()
+ for _, tc := range []struct {
+ name string
+ invalidate func(e *launchEnv)
+ state string
+ }{
+ {"stop time passes", func(e *launchEnv) { e.clk.AdvanceMono(2 * time.Hour) }, JobAbandoned},
+ {"audit fails", func(e *launchEnv) { e.fake.SetAudit(isolatedjob.NetworkAudit{}, nil); _ = e.c.RunAudit(e.ctx) }, JobWaiting},
+ {"audit goes stale", func(e *launchEnv) { e.clk.AdvanceMono(AuditMaxAge + time.Nanosecond) }, JobWaiting},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+ e := newLaunchEnv(t)
+ held, release, blocker := make(chan struct{}), make(chan struct{}), make(chan error, 1)
+ e.c.beforeMark = func() { // hold the store, so the marker's transaction queues behind it
+ go func() { blocker <- e.store.Tx(e.ctx, func(*sql.Tx) error { close(held); <-release; return nil }) }()
+ <-held
+ }
+ done := make(chan error, 1)
+ go func() { _, err := e.c.Launch(e.ctx, "inst-1", instKey, request("att-1")); done <- err }()
+ select {
+ case <-held:
+ case <-time.After(5 * time.Second):
+ t.Fatal("dispatch did not reach its marker within 5 s")
+ }
+ tc.invalidate(e)
+ close(release)
+ if err := errors.Join(<-blocker, <-done); err != nil {
+ t.Fatalf("Launch = %v", err)
+ }
+ if n := e.fake.Calls(isolatedjob.OpLaunch); n != 0 {
+ t.Fatalf("provider launches = %d, want 0", n)
+ }
+ if s := e.stopCols("att-1"); s.state != tc.state || !s.lastDispatch.Valid {
+ t.Fatalf("att-1 = %+v, want %s with its dispatch marker", s, tc.state)
+ }
+ })
+ }
+}
+
// leakRunner wraps the fake provider. While fail is set, Launch returns the
// error fail builds from the spec instead of launching.
type leakRunner struct {