console: serialize app setting writes with unpublish and unlink
Astra's final review, Important 4. The protection and analytics switches now go through appSettingWrite: it reads the app's lifetime and link at admission, takes the per-app lock that unpublish, unlink and every lifecycle transition take, and writes only if the same app lifetime and link still hold the name. A write admitted before a removal therefore lands before it (and is deleted with the app's objects) or answers 404 and writes nothing: no telemetry.json outlives its app, strands the name or lands on a replacement app of the same name. Barrier test: TestSettingWriteDelayedPastUnpublish, both switches, the name removed and the name republished while the write waits.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+152
−4
internal/console/analyticsview.go+6 −2internal/console/apilinks_test.go+85 −0internal/console/claim_route_writes.go+38 −0internal/console/links_store.go+12 −0internal/console/protectionsetting.go+6 −2internal/console/server.go+5 −0
diff --git a/internal/console/analyticsview.go b/internal/console/analyticsview.go| index 51fd60861..2b33ec349 100644 |
| --- a/internal/console/analyticsview.go |
| +++ b/internal/console/analyticsview.go |
| @@ -10,6 +10,7 @@ package console |
| import ( |
| "context" |
| "crypto/subtle" |
| + "errors" |
| "log" |
| "math" |
| "net/http" |
| @@ -135,9 +136,12 @@ func (s *Server) handleAppAnalyticsSetting(w http.ResponseWriter, r *http.Reques |
| http.Redirect(w, r, back, http.StatusSeeOther) |
| return |
| } |
| - if err := s.updateAppTelemetry(r.Context(), acct, app, "analytics-setting", func(st *telemetry.Settings) { |
| + if err := s.appSettingWrite(r.Context(), acct, app, "analytics-setting", func(st *telemetry.Settings) { |
| mergeAnalytics(st, count, door) |
| - }); err != nil { |
| + }); errors.Is(err, errSettingAppGone) { |
| + http.NotFound(w, r) |
| + return |
| + } else if err != nil { |
| log.Printf("console: analytics setting %s/%s: %v", acct.Sqid, app, err) |
| s.setFlash(w, "err", "The setting could not be saved — try again in a moment.") |
| http.Redirect(w, r, back, http.StatusSeeOther) |
diff --git a/internal/console/apilinks_test.go b/internal/console/apilinks_test.go| index ae1090b05..b15851ce2 100644 |
| --- a/internal/console/apilinks_test.go |
| +++ b/internal/console/apilinks_test.go |
| @@ -1749,6 +1749,91 @@ func TestLinkedAppEdgeSettingsLand(t *testing.T) { |
| } |
| } |
| +// TestSettingWriteDelayedPastUnpublish is the barrier for the settings and |
| +// removal race (Astra final review, Important 4): a protection or analytics |
| +// switch admitted for a linked app, held after admission until the app has |
| +// been unpublished (and, in "replaced", published again as a new app of the |
| +// same name), writes no telemetry.json and answers 404; the removed name can |
| +// be published again. |
| +func TestSettingWriteDelayedPastUnpublish(t *testing.T) { |
| + doors := []struct { |
| + name, path string |
| + form url.Values |
| + }{ |
| + {"protection", "/apps/shop/protection/setting", url.Values{"mode": {"record"}}}, |
| + {"analytics", "/apps/shop/analytics/setting", url.Values{"count": {"edge"}}}, |
| + } |
| + for _, door := range doors { |
| + for _, replaced := range []bool{false, true} { |
| + name := door.name + "/removed" |
| + if replaced { |
| + name = door.name + "/replaced" |
| + } |
| + t.Run(name, func(t *testing.T) { |
| + fx := newLinkFixture(t, "tunnel.example.com") |
| + browser := signInPaul(t, fx) |
| + l := mintLink(t, fx, browser, "shop-home") |
| + if status, out := publish(t, fx, l.token, "shop"); status != http.StatusCreated { |
| + t.Fatalf("publish = %d %v", status, out) |
| + } |
| + arrived, proceed := make(chan struct{}), make(chan struct{}) |
| + var once sync.Once |
| + fx.srv.appSettingHook = func(sqid, app string) { |
| + if app == "shop" { |
| + once.Do(func() { close(arrived) }) |
| + <-proceed |
| + } |
| + } |
| + form := url.Values{"csrf": {sessionToken(t, fx, browser)}} |
| + for k, v := range door.form { |
| + form[k] = v |
| + } |
| + done := make(chan int, 1) |
| + go func() { |
| + res, err := browser.PostForm(fx.u(door.path), form) |
| + if err != nil { |
| + done <- 0 |
| + return |
| + } |
| + res.Body.Close() |
| + done <- res.StatusCode |
| + }() |
| + select { |
| + case <-arrived: |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("the setting write never reached the barrier") |
| + } |
| + if status, body := linkDoor(t, fx, http.MethodDelete, "/api/link/publications/shop", l.token, ""); status != http.StatusNoContent { |
| + t.Fatalf("unpublish while the write waits = %d %s", status, body) |
| + } |
| + if replaced { |
| + if status, out := publish(t, fx, l.token, "shop"); status != http.StatusCreated { |
| + t.Fatalf("publish again while the write waits = %d %v", status, out) |
| + } |
| + } |
| + close(proceed) |
| + select { |
| + case status := <-done: |
| + if status != http.StatusNotFound { |
| + t.Fatalf("the delayed setting write answered %d, want 404", status) |
| + } |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("the delayed setting write never answered") |
| + } |
| + if keys, err := fx.rel.List(context.Background(), release.AppPrefix(fx.sqid, "shop")); err != nil || len(keys) != 0 { |
| + t.Fatalf("objects under the prefix after the delayed write: %v, %v", keys, err) |
| + } |
| + if replaced { |
| + return |
| + } |
| + if status, out := publish(t, fx, l.token, "shop"); status != http.StatusCreated { |
| + t.Fatalf("publish the name again = %d %v, want 201", status, out) |
| + } |
| + }) |
| + } |
| + } |
| +} |
| + |
| // TestRepublishAfterAnEdgeSetting: removing a publication releases its name, |
| // even after an allowed edge setting wrote the app's telemetry.json under |
| // its prefix; a name whose prefix holds an object that was never a linked |
diff --git a/internal/console/claim_route_writes.go b/internal/console/claim_route_writes.go| index f540d5008..6c927a802 100644 |
| --- a/internal/console/claim_route_writes.go |
| +++ b/internal/console/claim_route_writes.go |
| @@ -36,6 +36,44 @@ func (s *Server) updateAppTelemetry(ctx context.Context, acct *Account, app, act |
| }) |
| } |
| +// errSettingAppGone is appSettingWrite's refusal: the app the request was |
| +// admitted for is gone, or its name now belongs to another app lifetime. |
| +var errSettingAppGone = errors.New("console: the app is gone or replaced since this request was admitted") |
| + |
| +// appSettingWrite is updateAppTelemetry for the protection and analytics |
| +// switches (Astra final review, Important 4). It takes the app lock that |
| +// unpublish, unlink and every lifecycle transition take, and writes only if |
| +// the app is still the one the request was admitted for: the same lifetime |
| +// and the same link. So a switch admitted just before an unpublish either |
| +// lands before the removal (which then deletes its telemetry.json with the |
| +// app's other objects) or finds the app gone and writes nothing: no setting |
| +// outlives its app, strands the name (a republish refuses an occupied |
| +// prefix) or lands on a replacement app of the same name. The caller has |
| +// resolved the app (accountApp); a gone app is errSettingAppGone. |
| +func (s *Server) appSettingWrite(ctx context.Context, acct *Account, app, action string, fn func(*telemetry.Settings)) error { |
| + life, link, live, err := s.store.appIdentity(acct.ID, app) |
| + if err != nil { |
| + return err |
| + } |
| + if !live { |
| + return errSettingAppGone |
| + } |
| + if s.appSettingHook != nil { |
| + s.appSettingHook(acct.Sqid, app) |
| + } |
| + // LOCK ORDER (applocks.go): the app lock, then the app mutation inside |
| + // updateAppTelemetry, as unpublish takes them. |
| + defer s.lockApp(acct.Sqid, app)() |
| + nowLife, nowLink, live, err := s.store.appIdentity(acct.ID, app) |
| + if err != nil { |
| + return err |
| + } |
| + if !live || nowLife != life || nowLink != link { |
| + return errSettingAppGone |
| + } |
| + return s.updateAppTelemetry(ctx, acct, app, action, fn) |
| +} |
| + |
| // ConfirmChangeContext is ConfirmChange as a write of the change's own app. |
| // The caller has already proved the change belongs to accountID/app |
| // (pendingScoped); the armed gate refuses the write if it does not. |
diff --git a/internal/console/links_store.go b/internal/console/links_store.go| index a66dee124..eb19770aa 100644 |
| --- a/internal/console/links_store.go |
| +++ b/internal/console/links_store.go |
| @@ -395,6 +395,18 @@ func (s *Store) appLinkID(accountID int64, app string) (string, error) { |
| return id, err |
| } |
| +// appIdentity reads the live app row app in accountID: its lifetime id |
| +// (equal values are the same app lifetime) and its link ("" for an |
| +// ordinary app). live is false when there is no live row. |
| +func (s *Store) appIdentity(accountID int64, app string) (lifetime, linkID string, live bool, err error) { |
| + err = s.db.QueryRow(`SELECT lifetime, linked_link_id FROM apps WHERE account_id = ? AND app = ? AND deleted_at = 0`, |
| + accountID, app).Scan(&lifetime, &linkID) |
| + if errors.Is(err, sql.ErrNoRows) { |
| + return "", "", false, nil |
| + } |
| + return lifetime, linkID, err == nil, err |
| +} |
| + |
| // LinkedApps lists linkID's linked apps by name. Linked rows are never |
| // trashed (every trash statement skips them), so every row is live. |
| func (s *Store) LinkedApps(linkID string) ([]linkedApp, error) { |
diff --git a/internal/console/protectionsetting.go b/internal/console/protectionsetting.go| index f389c2c33..983ce319e 100644 |
| --- a/internal/console/protectionsetting.go |
| +++ b/internal/console/protectionsetting.go |
| @@ -10,6 +10,7 @@ package console |
| import ( |
| "context" |
| "crypto/subtle" |
| + "errors" |
| "log" |
| "net/http" |
| "net/url" |
| @@ -76,9 +77,12 @@ func (s *Server) handleAppProtectionSetting(w http.ResponseWriter, r *http.Reque |
| http.Redirect(w, r, back, http.StatusSeeOther) |
| return |
| } |
| - if err := s.updateAppTelemetry(r.Context(), acct, app, "protection-setting", func(st *telemetry.Settings) { |
| + if err := s.appSettingWrite(r.Context(), acct, app, "protection-setting", func(st *telemetry.Settings) { |
| setProtectionMode(st, mode) |
| - }); err != nil { |
| + }); errors.Is(err, errSettingAppGone) { |
| + http.NotFound(w, r) |
| + return |
| + } else if err != nil { |
| log.Printf("console: protection setting %s/%s: %v", acct.Sqid, app, err) |
| s.setFlash(w, "err", "The setting could not be saved — try again in a moment.") |
| http.Redirect(w, r, back, http.StatusSeeOther) |
diff --git a/internal/console/server.go b/internal/console/server.go| index 4513ff355..f17e608ce 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -1407,6 +1407,11 @@ type Server struct { |
| // (apilinks.go). Tests only: the barrier between the apps row and the |
| // forward row. |
| linkPublishHook func(linkID, stage string) |
| + // appSettingHook, when non-nil, is called by appSettingWrite after it |
| + // has read the app it was admitted for and before it takes the app |
| + // lock. Tests only: the barrier that delays a setting write past an |
| + // unpublish. |
| + appSettingHook func(sqid, app string) |
| // homeLink is the home side's link state (homelink.go): the link |
| // request in flight, a pending unlink, and the lock that serializes the |
| // home link doors. |