carlos / platform Public

mail: one central app-wide pause under the app lock (ruling #1581)

Paul's ruling on #1581 (2026-11-30), option (a): mail.SetAppPause
decides where an app-wide operator pause goes (the hold over every
instance in per-instance mode, the app sender's own pause otherwise)
and writes it inside the same (account, app) lock SetPerInstance holds.
The pause door calls it; the console-side pausePerInstanceApp is gone.
A signal-controlled test races pause against the mode switch in all
four orders and checks the app always ends held.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 238fa49ecaf0d5b9255389ea94fecab504016d93 parent 463a6b3
4 files changed, +148 −46
  • internal/console/apiinstancesender.go +0 −34
  • internal/console/apimail.go +4 −12
  • internal/mail/perinstance.go +34 −0
  • internal/mail/perinstance_test.go +110 −0

CI cancelled — superseded by a newer push — run details

CI log
=== gate ===
go: downloading go1.26.10 (linux/arm64)
go version go1.26.10 linux/arm64
opentofu: arm64, tofu 1.13.9, aws 6.73.0, archive 2.8.1
  tofu: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/tofu_1.13.9_linux_arm64.tar.gz
  tofu: downloaded, digest verified
  aws: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/terraform-provider-aws_6.73.0_linux_arm64.zip
  aws: downloaded, digest verified
  archive: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/terraform-provider-archive_2.8.1_linux_arm64.zip
  archive: downloaded, digest verified
  installed in /tmp/carlos-opentofu-FQ543f
.amadan/test-gate.sh
go: downloading github.com/aws/aws-sdk-go-v2 v1.44.0
go: downloading github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.68.0
go: downloading github.com/aws/aws-sdk-go-v2/service/costexplorer v1.67.5
go: downloading github.com/aws/aws-sdk-go-v2/service/s3 v1.105.0
go: downloading github.com/aws/smithy-go v1.28.1
go: downloading golang.org/x/crypto/x509roots/fallback v0.0.0-20260209214922-2f26647a795e
go: downloading golang.org/x/net v0.62.0
go: downloading modernc.org/sqlite v1.55.0
go: downloading github.com/aws/aws-sdk-go-v2/config v1.32.6
go: downloading github.com/aws/aws-sdk-go-v2/service/sns v1.43.0
go: downloading github.com/prometheus/client_golang v1.17.0
go: downloading golang.org/x/sync v0.24.0
go: downloading golang.org/x/sys v0.49.0
go: downloading golang.org/x/crypto v0.58.0
go: downloading github.com/aws/aws-sdk-go-v2/service/route53 v1.64.1
go: downloading github.com/aws/aws-sdk-go-v2/service/sts v1.45.6
go: downloading github.com/pion/logging v0.2.4
go: downloading github.com/pion/stun/v3 v3.1.5
go: downloading github.com/pion/turn/v4 v4.1.4
go: downloading github.com/aws/aws-sdk-go-v2/service/ec2 v1.324.0
go: downloading github.com/pierrec/lz4/v4 v4.1.22
go: downloading github.com/lmittmann/tint v1.1.3
go: downloading github.com/pion/dtls/v3 v3.1.4
go: downloading github.com/pion/transport/v4 v4.0.2
go: downloading github.com/pion/randutil v0.1.0
go: downloading github.com/cespare/xxhash/v2 v2.3.0
go: downloading github.com/prometheus/common v0.45.0
go: downloading github.com/prometheus/procfs v0.12.0
go: downloading google.golang.org/protobuf v1.36.10
go: downloading github.com/klauspost/compress v1.20.2
go: downloading github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.38
go: downloading github.com/wlynxg/anet v0.0.5
go: downloading github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0
go: downloading golang.org/x/text v0.43.0
go: downloading modernc.org/sqlite v1.55.0
go: downloading github.com/klauspost/compress v1.20.2
go: downloading github.com/pierrec/lz4/v4 v4.1.22
go: downloading github.com/aws/aws-sdk-go-v2/service/ecs v1.91.0
go: downloading modernc.org/libc v1.74.1
go: downloading modernc.org/libc v1.74.1
go: downloading modernc.org/memory v1.11.0
go: downloading github.com/google/uuid v1.6.0
go: downloading github.com/dustin/go-humanize v1.0.1
go: downloading modernc.org/mathutil v1.7.1
go: downloading github.com/google/uuid v1.6.0
go: downloading github.com/dustin/go-humanize v1.0.1
go: downloading modernc.org/mathutil v1.7.1
go: downloading modernc.org/memory v1.11.0

amadan: job cancelled — a newer push superseded it
diff --git a/internal/console/apiinstancesender.go b/internal/console/apiinstancesender.go
index 9c65d97ec..56b4067a3 100644
--- a/internal/console/apiinstancesender.go
+++ b/internal/console/apiinstancesender.go
@@ -461,37 +461,3 @@ func joinSemi(parts []string) string {
}
return out
}
-
-// pausePerInstanceApp is the operator pause door's app-wide form for a
-// per-instance app: pause writes a manual hold over every instance; resume
-// lifts whatever hold is there. It reports false, writing nothing, for an
-// app not in per-instance mode.
-func (s *Server) pausePerInstanceApp(ctx context.Context, w http.ResponseWriter, acct *Account, app, addr string, paused bool) bool {
- sn, _, err := mail.GetSender(ctx, s.cfg.Releases, acct.Sqid, app, mail.SenderAppID)
- if err != nil {
- log.Printf("console: api email pause %s/%s: %v", acct.Sqid, app, err)
- jsonError(w, http.StatusBadGateway, "could not read the email record — try again")
- return true
- }
- if sn == nil || !sn.PerInstance {
- return false
- }
- var hold *mailwire.Pause
- if paused {
- hold = &mailwire.Pause{At: time.Now()} // manual: never lifts on its own
- }
- if _, err := mail.SetInstancesPause(ctx, s.claimMailStore(), acct.Sqid, app, hold); err != nil {
- log.Printf("console: api email pause %s/%s: instances hold: %v", acct.Sqid, app, err)
- jsonError(w, http.StatusBadGateway, "could not change the pause state — try again")
- return true
- }
- s.audit(acct, app, addr, "email-pause", fmt.Sprintf("every instance: %s (cli)",
- map[bool]string{true: "email paused", false: "email resumed"}[paused]))
- rec, _, err := mail.Get(ctx, s.cfg.Releases, acct.Sqid, app)
- if err != nil || rec == nil {
- writeJSON(w, http.StatusOK, map[string]any{"app": app, "instances_paused": paused})
- return true
- }
- s.mailWalkReply(ctx, w, acct, app, rec)
- return true
-}
diff --git a/internal/console/apimail.go b/internal/console/apimail.go
index a51b77d0d..958fca628 100644
--- a/internal/console/apimail.go
+++ b/internal/console/apimail.go
@@ -1674,17 +1674,6 @@ func (s *Server) handleAPIEmailPause(w http.ResponseWriter, r *http.Request) {
}
}
- // In per-instance mode no instance sends as the app sender, so an
- // app-wide pause or resume is the hold over every instance
- // (InstancesPause), which the mailer reads for each of them
- // (whole-branch review R1 P1-2). After the cap, so a combined request
- // applies both (R2 P2).
- if req.Paused != nil {
- if handled := s.pausePerInstanceApp(ctx, w, acct, app, addr, *req.Paused); handled {
- return
- }
- }
-
// I1: SetPaused runs ONLY when the caller actually asked for a pause or
// a resume. A cap-only raise reads the current record instead of
// writing one — it must not touch Active at all.
@@ -1695,7 +1684,10 @@ func (s *Server) handleAPIEmailPause(w http.ResponseWriter, r *http.Request) {
// by mailcounts.go's enforceMailLimits (I3, review follow-up). An
// empty PauseKind is exactly what makes a manual pause never
// auto-resume on the UTC day roll, same as a rate pause.
- rec, err = mail.SetPaused(ctx, s.claimMailStore(), s.cfg.MailProvisioner, acct.Sqid, app, *req.Paused, "", time.Now())
+ // SetAppPause decides the target under the app lock the
+ // per-instance switch holds (Paul's ruling on #1581): in
+ // per-instance mode, the hold over every instance.
+ rec, err = mail.SetAppPause(ctx, s.claimMailStore(), s.cfg.MailProvisioner, acct.Sqid, app, *req.Paused, time.Now())
if errors.Is(err, mail.ErrNotProvisioned) {
jsonError(w, http.StatusConflict, "no provisioned email credential to pause — enable email first")
return
diff --git a/internal/mail/perinstance.go b/internal/mail/perinstance.go
index a969b6e13..5c3cf039b 100644
--- a/internal/mail/perinstance.go
+++ b/internal/mail/perinstance.go
@@ -151,6 +151,33 @@ func claimUnserializedSetInstancesPause(ctx context.Context, s release.Store, ac
})
}
+// SetAppPause is the app-wide operator pause and resume (Paul's ruling on
+// #1581, 2026-11-30): where the pause goes is decided and written inside
+// the same (account, app) lock SetPerInstance holds, so a pause can never
+// land on the wrong target while the mode changes. In per-instance mode it
+// is the manual hold over every instance (InstancesPause), which the mailer
+// reads for each instance sender; otherwise it is the app sender's own
+// pause, as SetPaused writes it. A manual pause never lifts on its own.
+func SetAppPause(ctx context.Context, s release.Store, p Provisioner, acct, app string, paused bool, now time.Time) (*Record, error) {
+ defer lockMailApp(acct, app)()
+ sn, _, err := GetSender(ctx, s, acct, app, SenderAppID)
+ if err != nil {
+ return nil, err
+ }
+ if sn != nil && sn.PerInstance {
+ var hold *mailwire.Pause
+ if paused {
+ hold = &mailwire.Pause{At: now}
+ }
+ if _, err := setInstancesPauseAdmitted(ctx, s, acct, app, hold, nil); err != nil {
+ return nil, err
+ }
+ rec, _, err := Get(ctx, s, acct, app)
+ return rec, err
+ }
+ return SetPaused(ctx, s, p, acct, app, paused, "", now)
+}
+
// SetInstancesPauseIf writes (or, nil, clears) the app sender's hold over
// every instance sender of a per-instance app (decision 16 (a)), only when
// cond holds of the current hold as read inside the CAS. A nil cond always
@@ -158,6 +185,13 @@ func claimUnserializedSetInstancesPause(ctx context.Context, s release.Store, ac
func SetInstancesPauseIf(ctx context.Context, s release.Store, acct, app string, p *mailwire.Pause,
cond func(cur *mailwire.Pause) bool) (*Sender, error) {
defer lockMailApp(acct, app)()
+ return setInstancesPauseAdmitted(ctx, s, acct, app, p, cond)
+}
+
+// setInstancesPauseAdmitted is SetInstancesPauseIf for a caller holding the
+// app lock.
+func setInstancesPauseAdmitted(ctx context.Context, s release.Store, acct, app string, p *mailwire.Pause,
+ cond func(cur *mailwire.Pause) bool) (*Sender, error) {
if _, enabled := s.(ClaimMailStore); !enabled {
return claimUnserializedSetInstancesPause(ctx, s, acct, app, p, cond)
}
diff --git a/internal/mail/perinstance_test.go b/internal/mail/perinstance_test.go
index 7e1df1e85..e2f033d61 100644
--- a/internal/mail/perinstance_test.go
+++ b/internal/mail/perinstance_test.go
@@ -204,3 +204,113 @@ func TestSetPerInstanceRecordFailsClosedForOldMailers(t *testing.T) {
t.Fatalf("view %+v: an old mailer would not refuse", v)
}
}
+
+// Paul's ruling on #1581: an app-wide pause and the per-instance switch
+// serialize on the app lock, so in either order the pause lands where the
+// mode it meets says, and the app ends held. Both orders, signal-controlled.
+func TestAppPauseAndModeSwitchSerialize(t *testing.T) {
+ ctx := context.Background()
+ setup := func(t *testing.T) release.Store {
+ t.Helper()
+ s := release.NewDirStore(t.TempDir())
+ addrFixture(t, s)
+ for _, id := range []string{"a.shop.example", "b.shop.example"} {
+ if err := claimUnserializedRemoveInstanceSender(ctx, s, tAcct, tApp, id, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ }
+ reaped(t, s)
+ return s
+ }
+ held := func(t *testing.T, s release.Store, wantPerInstance bool) {
+ t.Helper()
+ sn, _, err := GetSender(ctx, s, tAcct, tApp, SenderAppID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if sn.PerInstance != wantPerInstance {
+ t.Fatalf("per-instance = %v, want %v", sn.PerInstance, wantPerInstance)
+ }
+ if wantPerInstance && (sn.InstancesPause == nil || sn.InstancesPause.Kind != "") {
+ t.Fatalf("per-instance app not held: %+v", sn.InstancesPause)
+ }
+ if !wantPerInstance {
+ if pz := EffectivePause(sn); pz == nil || pz.Kind == PauseKindPerInstance {
+ t.Fatalf("app not paused: %+v", pz)
+ }
+ }
+ }
+ // race parks first on its first write to the app sender (inside the
+ // lock), runs second until it reaches the lock, then releases first.
+ race := func(t *testing.T, s release.Store, first, second func(release.Store) error) {
+ t.Helper()
+ p := newPausing(s, "Put", "/email/senders/app.json", false)
+ done := make(chan error, 1)
+ go func() { done <- first(p) }()
+ select {
+ case <-p.arrived:
+ case err := <-done:
+ t.Fatalf("first finished without writing: %v", err)
+ case <-time.After(10 * time.Second):
+ t.Fatal("first never wrote")
+ }
+ reached := armLockAttempt(t)
+ out := make(chan error, 1)
+ go func() { out <- second(s) }()
+ <-reached
+ select {
+ case err := <-out:
+ t.Fatalf("second finished while first held the lock: %v", err)
+ default:
+ }
+ close(p.release)
+ if err := <-done; err != nil {
+ t.Fatalf("first: %v", err)
+ }
+ if err := <-out; err != nil {
+ t.Fatalf("second: %v", err)
+ }
+ lockAttemptHook = nil
+ }
+ pause := func(s release.Store) error {
+ _, err := SetAppPause(ctx, s, NewMemProvisioner("111111111111"), tAcct, tApp, true, time.Now())
+ return err
+ }
+ modeOn := func(s release.Store) error {
+ _, err := SetPerInstance(ctx, s, tAcct, tApp, true, "op", time.Now())
+ return err
+ }
+ modeOff := func(s release.Store) error {
+ _, err := SetPerInstance(ctx, s, tAcct, tApp, false, "op", time.Now())
+ return err
+ }
+
+ // Mode on first, pause second: the pause meets per-instance mode and
+ // holds every instance.
+ s := setup(t)
+ race(t, s, modeOn, pause)
+ held(t, s, true)
+
+ // Pause first, mode on second: the app's manual pause is carried onto
+ // every instance by the switch.
+ s = setup(t)
+ race(t, s, pause, modeOn)
+ held(t, s, true)
+
+ // Per-instance app: pause first (the hold), mode off second: the hold
+ // becomes the app sender's own pause.
+ s = setup(t)
+ if err := modeOn(s); err != nil {
+ t.Fatal(err)
+ }
+ race(t, s, pause, modeOff)
+ held(t, s, false)
+
+ // Mode off first, pause second: the pause meets an ordinary app.
+ s = setup(t)
+ if err := modeOn(s); err != nil {
+ t.Fatal(err)
+ }
+ race(t, s, modeOff, pause)
+ held(t, s, false)
+}