Serialize installs and uninstalls per app
An in-process per-app lock (TryLock) covers Install's whole run and Uninstall. A concurrent Install returns nil; a concurrent Uninstall returns ErrBusy. Uninstall of an app with no row returns ErrNoInstall without calling Platform.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2 files changed,
+98
−6
internal/install/install.go+42 −6internal/install/install_test.go+56 −0
diff --git a/internal/install/install.go b/internal/install/install.go| index 8449b73..0e78bf3 100644 |
| --- a/internal/install/install.go |
| +++ b/internal/install/install.go |
| @@ -13,6 +13,7 @@ import ( |
| "fmt" |
| "log/slog" |
| "net/http" |
| + "sync" |
| "time" |
| "amadan.net/rastrillo/rastrillo/crypto" |
| @@ -47,6 +48,25 @@ type Installer struct { |
| pc platform.Client |
| probe func(ctx context.Context, origin string) error |
| logger *slog.Logger |
| + |
| + // locks serializes work per app. Base is one process, so this is enough. |
| + mu sync.Mutex |
| + locks map[string]*sync.Mutex |
| +} |
| + |
| +// lockFor returns app's lock. |
| +func (in *Installer) lockFor(app string) *sync.Mutex { |
| + in.mu.Lock() |
| + defer in.mu.Unlock() |
| + if in.locks == nil { |
| + in.locks = map[string]*sync.Mutex{} |
| + } |
| + l := in.locks[app] |
| + if l == nil { |
| + l = new(sync.Mutex) |
| + in.locks[app] = l |
| + } |
| + return l |
| } |
| // New returns an Installer. db must be the writer handle. probe waits until |
| @@ -94,6 +114,11 @@ func (in *Installer) Install(ctx context.Context, app string) error { |
| if in.cfg.AssertionKey == nil { |
| return errors.New("BASE_ASSERTION_KEY is not set") |
| } |
| + lock := in.lockFor(app) |
| + if !lock.TryLock() { |
| + return nil // another Install or Uninstall of this app is running |
| + } |
| + defer lock.Unlock() |
| label, host := HostFor(in.cfg, app) |
| claimed, err := store.ClaimInstall(ctx, in.db, app, host) |
| if err != nil { |
| @@ -135,24 +160,35 @@ func (in *Installer) fail(ctx context.Context, app, msg string, cause error) err |
| // Uninstall removes the instance's route, then forgets the install. The |
| // app's data is kept by Platform. It returns ErrBusy, touching nothing, while |
| -// the app is installing. |
| +// the app is installing, and store.ErrNoInstall if it is not installed. |
| func (in *Installer) Uninstall(ctx context.Context, app string) error { |
| + lock := in.lockFor(app) |
| + if !lock.TryLock() { |
| + return ErrBusy |
| + } |
| + defer lock.Unlock() |
| all, err := store.Installs(ctx, in.db) |
| if err != nil { |
| return err |
| } |
| - for _, i := range all { |
| - if i.App == app && i.State == "installing" { |
| - return ErrBusy |
| + var row *store.Install |
| + for i := range all { |
| + if all[i].App == app { |
| + row = &all[i] |
| } |
| } |
| + if row == nil { |
| + return store.ErrNoInstall |
| + } |
| + if row.State == "installing" { |
| + return ErrBusy |
| + } |
| _, host := HostFor(in.cfg, app) |
| if err := in.pc.DeleteInstance(ctx, app, host); err != nil { |
| in.logger.Error("uninstall failed", "app", app, "err", err) |
| return errors.New(msgRemove) |
| } |
| - // The delete itself refuses an installing row, closing the gap between |
| - // the check above and now. |
| + // The store refuses an installing row too, as a second guard. |
| busy, err := store.DeleteInstall(ctx, in.db, app) |
| if err != nil { |
| return err |
diff --git a/internal/install/install_test.go b/internal/install/install_test.go| index 36bc592..a1397ed 100644 |
| --- a/internal/install/install_test.go |
| +++ b/internal/install/install_test.go |
| @@ -305,3 +305,59 @@ func TestInstallFailsWhenRowVanishes(t *testing.T) { |
| t.Fatal("want an error when the row is gone") |
| } |
| } |
| + |
| +func TestUninstallMissingRowMakesNoPlatformCall(t *testing.T) { |
| + in, pf, _, _ := setup(t, nil) |
| + if err := in.Uninstall(context.Background(), "scratchpad"); !errors.Is(err, store.ErrNoInstall) { |
| + t.Fatalf("Uninstall = %v, want ErrNoInstall", err) |
| + } |
| + if len(pf.Calls()) != 0 { |
| + t.Fatalf("calls = %+v", pf.Calls()) |
| + } |
| +} |
| + |
| +func TestUninstallRefusedWhileReinstallingFailedRow(t *testing.T) { |
| + in, pf, db, _ := setup(t, nil) |
| + ctx := context.Background() |
| + if ok, err := store.ClaimInstall(ctx, db, "scratchpad", "acme-scratchpad.bab.oncarlos.com"); err != nil || !ok { |
| + t.Fatal(ok, err) |
| + } |
| + if err := store.SetInstallState(ctx, db, "scratchpad", "failed", "x"); err != nil { |
| + t.Fatal(err) |
| + } |
| + bc := &blockingClient{Client: pf, entered: make(chan struct{}), release: make(chan struct{})} |
| + in.pc = bc |
| + done := make(chan error, 1) |
| + go func() { done <- in.Install(ctx, "scratchpad") }() |
| + <-bc.entered |
| + if err := in.Uninstall(ctx, "scratchpad"); !errors.Is(err, ErrBusy) { |
| + t.Fatalf("Uninstall = %v, want ErrBusy", err) |
| + } |
| + for _, c := range pf.Calls() { |
| + if c.Method == "DeleteInstance" { |
| + t.Fatal("DeleteInstance called during reinstall") |
| + } |
| + } |
| + close(bc.release) |
| + if err := <-done; err != nil { |
| + t.Fatal(err) |
| + } |
| + if s := state(t, db, "scratchpad"); s.State != "installed" { |
| + t.Errorf("row = %+v", s) |
| + } |
| +} |
| + |
| +func TestInstalledWriteFailsRowEndsFailed(t *testing.T) { |
| + in, _, db, _ := setup(t, nil) |
| + _, err := db.Exec(`CREATE TRIGGER no_installed BEFORE UPDATE ON installs WHEN NEW.state = 'installed' |
| + BEGIN SELECT RAISE(ABORT, 'nope'); END`) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := in.Install(context.Background(), "scratchpad"); err == nil { |
| + t.Fatal("want an error") |
| + } |
| + if s := state(t, db, "scratchpad"); s.State != "failed" || s.Error == "" { |
| + t.Fatalf("row = %+v", s) |
| + } |
| +} |