carlos / platform Public

console: media repeat answers a store refusal dispatch made meanwhile

The repeat path reads the store record once, without the lock. When
its final lookup finds the row failed store_writes_denied or
store_changed (dispatch saw a newer store than that read), the door
answers 402 store_writes_denied or 409 store_changed with the door's
copy instead of 200, as the spec's order would (Astra fix round 2).
Every other row answers 200 as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 80c8896ad4c64fe6ab1aee2fd97ace97bfa3cd08 parent 89be502
2 files changed, +106 −1
  • internal/console/apimedia.go +13 −1
  • internal/console/apimedia_test.go +93 −0
diff --git a/internal/console/apimedia.go b/internal/console/apimedia.go
index 6e06b9efd..6b28ac589 100644
--- a/internal/console/apimedia.go
+++ b/internal/console/apimedia.go
@@ -199,7 +199,19 @@ func (s *Server) handleAPIMediaSubmit(w http.ResponseWriter, r *http.Request) {
return
}
if found {
- writeJSON(w, http.StatusOK, s.mediaAnswer(job))
+ // The repeat path read the store record once, without the lock. A
+ // dispatch that failed the row on a store refusal since then saw a
+ // newer store than that read: answer the refusal the ordered path
+ // would give, not 200 (Astra fix round 2). Any other row answers
+ // as it is.
+ switch {
+ case job.State == "failed" && job.ErrorCode == media.CodeStoreWritesDenied:
+ mediaRefuse(w, http.StatusPaymentRequired, media.CodeStoreWritesDenied, mediaText("media.store_writes_denied"))
+ case job.State == "failed" && job.ErrorCode == media.CodeStoreChanged:
+ mediaRefuse(w, http.StatusConflict, media.CodeStoreChanged, mediaText("media.job.store_changed"))
+ default:
+ writeJSON(w, http.StatusOK, s.mediaAnswer(job))
+ }
return
}
defer s.storeWalks.lock(acct.Sqid, app, "")()
diff --git a/internal/console/apimedia_test.go b/internal/console/apimedia_test.go
index a141c15ce..81c92bcf5 100644
--- a/internal/console/apimedia_test.go
+++ b/internal/console/apimedia_test.go
@@ -935,3 +935,96 @@ func TestNewServerRefusesBadMediaLimits(t *testing.T) {
t.Fatalf("default limits: %v", err)
}
}
+
+// pausingGrants holds the next HEAD of one key until released, so a test
+// can run dispatch while a repeat POST waits on its HEAD.
+type pausingGrants struct {
+ *countingGrants
+ key string
+ armed atomic.Bool
+ reached chan struct{}
+ release chan struct{}
+}
+
+func (g *pausingGrants) Head(ctx context.Context, bucket, key string) (media.ObjectHead, error) {
+ if key == g.key && g.armed.CompareAndSwap(true, false) {
+ close(g.reached)
+ <-g.release
+ }
+ return g.countingGrants.Head(ctx, bucket, key)
+}
+
+// TestRepeatPathAnswersAStoreRefusalDispatchMadeMeanwhile (Astra fix round
+// 2): a repeat POST reads the store record, finds its queued row and
+// pauses on the HEAD. Meanwhile the store changes and dispatch fails the
+// row on it. The repeat answers the door's refusal, not 200 with the row.
+func TestRepeatPathAnswersAStoreRefusalDispatchMadeMeanwhile(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ change func(f *mediaDoorFixture)
+ status int
+ code string
+ msg string
+ }{
+ {"store_writes_denied", func(f *mediaDoorFixture) {
+ if _, err := objstore.SetWritesDenied(context.Background(), f.rel, f.sqid, "titogo", "",
+ &objstore.WritesDenied{Reason: "storage grace ended", At: f.clock}); err != nil {
+ f.t.Fatal(err)
+ }
+ }, 402, "store_writes_denied", mediaText("media.store_writes_denied")},
+ // A bucket made again carries no tag: dispatch verifies it afresh,
+ // mints a new incarnation and fails the old row store_changed.
+ {"store_changed", func(f *mediaDoorFixture) { f.buckets.Tags[f.bucket] = map[string]string{} },
+ 409, "store_changed", mediaText("media.job.store_changed")},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f := newMediaDoor(t, "code-media-pause-"+tc.name)
+ g := &pausingGrants{countingGrants: f.grants, key: mediaTestKey, reached: make(chan struct{}), release: make(chan struct{})}
+ f.srv.cfg.MediaGrants = g
+ sub := f.submit(mediaTestKey)
+ if sub.status != http.StatusAccepted {
+ t.Fatalf("submit %d %s", sub.status, sub.raw)
+ }
+ g.armed.Store(true)
+ done := make(chan mediaReply, 1)
+ go func() {
+ status, header, raw := rawReq(t, http.MethodPost, f.url("titogo"), f.svc, `{"key":"`+mediaTestKey+`"}`)
+ done <- decodeMediaReply(t, status, header, raw)
+ }()
+ select {
+ case <-g.reached:
+ case <-time.After(10 * time.Second):
+ t.Fatal("the repeat POST never reached its HEAD")
+ }
+ tc.change(f)
+ if err := f.srv.media.Pass(context.Background(), f.clock); err != nil {
+ t.Fatal(err)
+ }
+ rows, err := f.st.db.Query(`SELECT state, error FROM media_jobs WHERE job_id = ?`, sub.job.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var state, code string
+ for rows.Next() {
+ if err := rows.Scan(&state, &code); err != nil {
+ t.Fatal(err)
+ }
+ }
+ rows.Close()
+ if state != "failed" || code != tc.code {
+ t.Fatalf("dispatch left the row %s %s, want failed %s", state, code, tc.code)
+ }
+ close(g.release)
+ var r mediaReply
+ select {
+ case r = <-done:
+ case <-time.After(10 * time.Second):
+ t.Fatal("the repeat POST did not answer")
+ }
+ wantMediaRefusal(t, r, tc.status, tc.code, tc.msg)
+ if n := len(f.engine.Executions()); n != 0 {
+ t.Fatalf("%d executions", n)
+ }
+ })
+ }
+}