carlos / platform Public

console: the submission reap takes the switch door's locks

P2-3 (Fable's final review of b0aba39b0, ruled 2026-11-14): the week-7
reap ran under no lock the switch door holds. It reads the app sender,
deletes the app-level user's keys and the user, then CAS-writes the
record; a switch-off that ran in between re-delivered SMTP from that
same user, and the reap deleted it, noticed only by its refused write.
Each app's reap now takes the account lock, then the app lock
(beginCopiesMutation), the door's own order, and reads the record under
them.

TestSubmissionChannelSimulation gains a step that starts a whole
switch-off inside a reap paused before its deletes, and checks, for that
step, that every SMTP key the switch-off delivered is one AWS still
holds, active. Watched to fail with the locks removed (the default
bundle switched off onto a deleted user's key).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a90e743d97935b2698f630d1024e4c11f29f4621 parent 81f68b1
2 files changed, +132 −18
  • internal/console/apisubmission.go +43 −18
  • internal/console/submission_sim_test.go +89 −0
diff --git a/internal/console/apisubmission.go b/internal/console/apisubmission.go
index 7df76c774..4d5146934 100644
--- a/internal/console/apisubmission.go
+++ b/internal/console/apisubmission.go
@@ -471,30 +471,55 @@ func (s *Server) reapSubmittedAppUsers(ctx context.Context, acct *Account, now t
}
var errs []error
for _, app := range apps {
- sn, _, err := mail.GetSender(ctx, s.claimMailStore(), acct.Sqid, app, mail.SenderAppID)
- if err != nil {
- errs = append(errs, fmt.Errorf("%s: %w", app, err))
- continue
- }
- if !mail.Switched(sn) || sn.Provisioned == nil {
- continue
- }
- counts := func(a, b, day string) (map[string]int64, error) {
- return s.mailCallerCounts(ctx, a, b, day, s.store.RecordMailCallerSend)
- }
- reaped, err := mail.ReapAppUserAfterSubmission(ctx, s.claimMailStore(), s.cfg.MailProvisioner, acct.Sqid, app, counts, now)
- if err != nil {
+ if err := s.reapSubmittedAppUser(ctx, acct, app, now); err != nil {
errs = append(errs, fmt.Errorf("%s: %w", app, err))
- continue
- }
- if reaped {
- s.audit(acct, app, "", "email-submission-reap", "reaped the app-level mail user after a week of submission")
- s.bumpSerial(ctx)
}
}
return errors.Join(errs...)
}
+// reapSubmittedAppUser is one app's reap, under the switch door's locks:
+// the account lock, then the app lock (beginCopiesMutation), the door's own
+// order (Fable's final review, P2-3, ruled 2026-11-14). The reap reads the
+// sender record, deletes the app-level user's keys and the user, and only
+// then writes the record. A switch-off that ran in between re-delivered
+// SMTP from that same user and turned the switch off; the reap went on to
+// delete the user it had just delivered, and only its last write, refused
+// by the record's changed etag, noticed. Holding both locks for the whole
+// reap means the switch door runs before it or after it, never inside it.
+func (s *Server) reapSubmittedAppUser(ctx context.Context, acct *Account, app string, now time.Time) error {
+ defer s.lockAccount(acct.Sqid)()
+ lctx, unlockApp, ref := s.beginCopiesMutation(ctx, acct, app)
+ if ref != nil {
+ if ref.Status == http.StatusConflict {
+ // The app went, or was renamed, since the listing: the next
+ // pass sees it as it is.
+ return nil
+ }
+ return fmt.Errorf("app lock: %s", ref.Msg)
+ }
+ defer unlockApp()
+ sn, _, err := mail.GetSender(lctx, s.claimMailStore(), acct.Sqid, app, mail.SenderAppID)
+ if err != nil {
+ return err
+ }
+ if !mail.Switched(sn) || sn.Provisioned == nil {
+ return nil
+ }
+ counts := func(a, b, day string) (map[string]int64, error) {
+ return s.mailCallerCounts(lctx, a, b, day, s.store.RecordMailCallerSend)
+ }
+ reaped, err := mail.ReapAppUserAfterSubmission(lctx, s.claimMailStore(), s.cfg.MailProvisioner, acct.Sqid, app, counts, now)
+ if err != nil {
+ return err
+ }
+ if reaped {
+ s.audit(acct, app, "", "email-submission-reap", "reaped the app-level mail user after a week of submission")
+ s.bumpSerial(lctx)
+ }
+ return nil
+}
+
// tenantResourceLister is the provisioner's live read of a tenant's
// associations (gap G8): the ARNs of the identities and configuration sets
// associated with the SES tenant named. The AWS provisioner and the
diff --git a/internal/console/submission_sim_test.go b/internal/console/submission_sim_test.go
index bd2898ca7..b275e06b7 100644
--- a/internal/console/submission_sim_test.go
+++ b/internal/console/submission_sim_test.go
@@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"sync"
+ "sync/atomic"
"testing"
"time"
@@ -260,17 +261,33 @@ func (o *simOverlay) ListDirs(ctx context.Context, prefix string) ([]string, err
// makes, one of which an armed step fails.
var simProvisionerFaults = []string{"EnsureUser", "MintKey", "ReapKeys", "DeleteUser", "SetKeyActive", "DeleteKey"}
+// simHookedProvisioner is the in-memory AWS with a hook run before each
+// ReapKeys, so a step can pause a door inside its own work.
+type simHookedProvisioner struct {
+ *mail.MemProvisioner
+ beforeReapKeys atomic.Pointer[func(user string)]
+}
+
+func (p *simHookedProvisioner) ReapKeys(ctx context.Context, user string, keep []string) error {
+ if h := p.beforeReapKeys.Load(); h != nil {
+ (*h)(user)
+ }
+ return p.MemProvisioner.ReapKeys(ctx, user, keep)
+}
+
type channelSim struct {
t *testing.T
w *submissionWorld
rng *rand.Rand
faults *simFaultStore
prov *mail.MemProvisioner
+ hooked *simHookedProvisioner
team string // a team credential of scope alta
hosts []string
envs []string // owned environments: "team" and the made ones
reported map[string]string
dry bool // a dry pass: no bookkeeping, no honesty checks
+ liveKeys bool // check also checks that a switch-off's SMTP keys work
nextID int
steps []string
}
@@ -300,6 +317,8 @@ func newChannelSim(t *testing.T, seed int64) *channelSim {
hosts: []string{submissionHost}, envs: []string{"team"}, reported: map[string]string{}}
sim.faults = &simFaultStore{base: w.fx.srv.cfg.Releases, prefix: "apps/bab/titogo/"}
w.fx.srv.cfg.Releases = sim.faults
+ sim.hooked = &simHookedProvisioner{MemProvisioner: sim.prov}
+ w.fx.srv.cfg.MailProvisioner = sim.hooked
sim.boxTick(true, false)
return sim
}
@@ -433,6 +452,11 @@ func (sim *channelSim) declared(version string) bool {
// check asserts the invariant after a step. wasSwitched is the switch
// before it: a step that turned it off must leave every instance on SMTP.
+// With liveKeys set, each of those SMTP keys must also be one AWS still
+// holds, active, for a user it still has (P2-3 of Fable's final review: a
+// reap that ran around a switch-off deleted the user the switch-off had
+// just delivered). liveKeys is set for that step only: the other steps'
+// faults can leave a key that is not live (reported with P2-3).
// While switched, every instance resolves to a declared build (and a box
// that has not adopted yet serves a declared one too), and its bundle
// holds a From the box's mailer sends.
@@ -469,6 +493,9 @@ func (sim *channelSim) check(wasSwitched bool) {
bad = append(bad, fmt.Sprintf("%s (environment %q) was switched off without %s", rec.Host, rec.Environment, k))
}
}
+ if key := vars[user].Value; sim.liveKeys && key != "" && !sim.keyLive(key) {
+ bad = append(bad, fmt.Sprintf("%s (environment %q) was switched off onto key %s, which no user holds active", rec.Host, rec.Environment, key))
+ }
}
}
if len(bad) > 0 {
@@ -476,6 +503,17 @@ func (sim *channelSim) check(wasSwitched bool) {
}
}
+// keyLive reports whether the in-memory AWS holds key, active, for a user
+// it still has.
+func (sim *channelSim) keyLive(key string) bool {
+ for _, u := range sim.prov.Users {
+ if k, ok := u.Keys[key]; ok && k.Active && !u.Deleted {
+ return true
+ }
+ }
+ return false
+}
+
// provisionerState is the in-memory AWS's state, for a dry pass to put
// back.
type provisionerState struct {
@@ -711,6 +749,43 @@ func (sim *channelSim) reap() string {
return "ok"
}
+// reapAroundSwitchOff is the hourly reap paused inside its own work (Fable's
+// final review, P2-3): it has read the app sender and is about to delete
+// the app-level user's keys when a whole switch-off is started. The
+// switch-off is given a moment to land. With the reap holding the account
+// and app locks, as the switch door does, it cannot: it runs once the reap
+// is done, and mints the user again. A reap that held neither went on to
+// delete the user the switch-off had just delivered. reached says whether
+// the reap got that far.
+func (sim *channelSim) reapAroundSwitchOff() (out string, reached bool) {
+ user := mail.UserName("bab", "titogo")
+ var off string
+ var wg sync.WaitGroup
+ hook := func(u string) {
+ if u != user {
+ return
+ }
+ sim.hooked.beforeReapKeys.Store(nil)
+ reached = true
+ done := make(chan struct{})
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ defer close(done)
+ off = sim.switchOff()
+ }()
+ select {
+ case <-done:
+ case <-time.After(simRaceWait):
+ }
+ }
+ sim.hooked.beforeReapKeys.Store(&hook)
+ defer sim.hooked.beforeReapKeys.Store(nil)
+ out = sim.reap()
+ wg.Wait()
+ return out + " | switch-off in between: " + off, reached
+}
+
// raceSwitchOn is the switch-on door with another door started between
// its preconditions and its switch (P2-1 and its kin). The other door is
// given a moment to land; with the app lock held it cannot, and it runs,
@@ -895,6 +970,20 @@ func (sim *channelSim) scripted() {
sim.do(fmt.Sprintf("switch on, team's write #%d failing", n), false, sim.switchOn)
}
sim.do("switch on", false, sim.switchOn)
+ // P2-3 of Fable's final review: a switch-off started inside a reap
+ // paused before its deletes.
+ reached := false
+ sim.liveKeys = true
+ sim.do("the hourly reap, a switch-off started inside it", false, func() string {
+ out, r := sim.reapAroundSwitchOff()
+ reached = r
+ return out
+ })
+ sim.liveKeys = false
+ if !reached {
+ sim.t.Fatalf("the reap never reached the app user's keys, so the step proved nothing\nsteps:\n %s", strings.Join(sim.steps, "\n "))
+ }
+ sim.do("switch on", false, sim.switchOn)
for _, env := range slices.Clone(sim.envs) {
if env != "team" {
sim.do("release "+env, false, func() string { return sim.release(env) })