Seed copy: release the account lock when store provisioning panics (branch review R1)
runFrom took the account lock around instanceStoreProvisionFor and released it after the call, so a panic, which runFrom's recover turns into an abort, kept the lock and every later account-locked operation waited for good. The lock is now released by a defer in a closure.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+45
−3
internal/console/apiinstanceseed.go+11 −3internal/console/apiinstanceseed_test.go+34 −0
diff --git a/internal/console/apiinstanceseed.go b/internal/console/apiinstanceseed.go| index 279f708ca..e3dae747c 100644 |
| --- a/internal/console/apiinstanceseed.go |
| +++ b/internal/console/apiinstanceseed.go |
| @@ -450,9 +450,17 @@ func (s *Server) runFrom(j *fromCopy) { |
| // (fleet secrets key spec §5.2.1). This goroutine is an entry point: |
| // the door that started it, which may hold the lock through |
| // planLock, answered before it ran and holds nothing it waits on. |
| - unlockAcct := s.lockAccount(acct.Sqid) |
| - storeRec, ok, err := s.instanceStoreProvisionFor(ctx, held, acct, app, c.rec.Environment, c.principal, j.id) |
| - unlockAcct() |
| + // Released by a defer, so a panic in the provisioning, which the |
| + // recover above turns into an abort, does not keep it. |
| + var ( |
| + storeRec *objstore.Record |
| + ok bool |
| + err error |
| + ) |
| + func() { |
| + defer s.lockAccount(acct.Sqid)() |
| + storeRec, ok, err = s.instanceStoreProvisionFor(ctx, held, acct, app, c.rec.Environment, c.principal, j.id) |
| + }() |
| if !ok { |
| reason := held.message(failed) |
| switch { |
diff --git a/internal/console/apiinstanceseed_test.go b/internal/console/apiinstanceseed_test.go| index 9de1ff5e3..3088400ca 100644 |
| --- a/internal/console/apiinstanceseed_test.go |
| +++ b/internal/console/apiinstanceseed_test.go |
| @@ -625,6 +625,40 @@ func TestFromPanicAbortsTheSeedAndSparesTheConsole(t *testing.T) { |
| f.requireClean(t) |
| } |
| +// A panic in the seed's store provisioning, which runs under the account |
| +// lock (fleet secrets key spec §5.2.1), aborts the seed and releases the |
| +// lock: the next account-locked operation on the account goes ahead. |
| +func TestFromProvisioningPanicReleasesTheAccountLock(t *testing.T) { |
| + f := newFromFixture(t, 3) |
| + enableInstances(t, f.fx, fromApp, fromManifest(true)) |
| + real := f.fx.srv.cfg.StoreProvisioner |
| + f.fx.srv.cfg.StoreProvisioner = panickingProvisioner{real} |
| + requireAborted(t, f.copy(t, fromBody), fromAbortedReason) |
| + f.requireClean(t) |
| + |
| + locked := make(chan struct{}) |
| + go func() { |
| + f.fx.srv.lockAccount(f.fx.sqid)() |
| + close(locked) |
| + }() |
| + select { |
| + case <-locked: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the account lock was still held 5 s after the copy's panic") |
| + } |
| + // And the door's own next copy, which takes the same lock, runs to ready. |
| + f.fx.srv.cfg.StoreProvisioner = real |
| + requireReady(t, f.copy(t, `{"host":"c2.shop.example.com","from":{"host":"main.shop.example.com"}}`)) |
| + f.requireClean(t) |
| +} |
| + |
| +// panickingProvisioner panics where the grant walk first reaches the backend. |
| +type panickingProvisioner struct{ objstore.Provisioner } |
| + |
| +func (panickingProvisioner) EnsureBucket(context.Context, string) (string, error) { |
| + panic("a provisioner fault") |
| +} |
| + |
| func TestFromUploadFailureAborts(t *testing.T) { |
| f := newFromFixture(t, 3) |
| f.fx.srv.cfg.SeedData = failingPuts{f.data} |