carlos / platform Public

host: the staging retry test no longer depends on its copies' order

TestStagingWriterRetriesWithANewCheck assumed a replica round copies one
object at a time. lease.MirrorPrefixKeep copies a round's objects side by
side (8 workers, two objects in this fixture), so:

- shortening the lease from inside the failing copy raced the sibling
  copy: it could pass its guard first (a second request reached the store,
  "2 copy requests reached the store, want 1", main red at e15fe610) or be
  refused first (the round would end on the refusal with no retry);
- the second half compared signs[0] and signs[1], which could be the two
  first-round copies in either order.

The lease is now cut short when the lane logs "retrying once", after every
first-round copy has returned (captureLogThen), and the assertions are per
object: the failed object is requested once and nothing after the retry is
announced, and the failed object's retry is signed after its first try.
The lease's until is written under its lock (it was a data race).

A probe holding the failing copy 20 ms before shortening the lease failed
the old test 10 of 10 and passes the new one 10 of 10. Under load (2 cores,
4 busy loops, nice 19): old 0 failures in 900 runs (-cpu 2,4,8, the window
is narrow here), new 0 in 200 (-cpu 1,2,4,8).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 2d8f5978350f44f6548a891af2d430bdb9edac55 parent 3997379
2 files changed, +83 −15
  • internal/host/caps_pass_test.go +43 −0
  • internal/host/movesphase1_test.go +40 −15
diff --git a/internal/host/caps_pass_test.go b/internal/host/caps_pass_test.go
index f530fc66a..73f7787df 100644
--- a/internal/host/caps_pass_test.go
+++ b/internal/host/caps_pass_test.go
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
+ "sync"
"testing"
"github.com/carlosframework/platform/internal/caps"
@@ -855,3 +856,45 @@ func TestCapabilityPassStampsDBPathEvenWhenConvergeIsRefused(t *testing.T) {
t.Errorf("DBPath = %q, want %q — a capability refusal is not a reason to stop replicating data", got.DBPath, want)
}
}
+
+// captureLogThen is captureLog that also calls fn, once, when a line
+// containing marker is logged, before the logging call returns: a test
+// can act at the moment the code under test announces a step.
+func captureLogThen(t *testing.T, marker string, fn func()) func() string {
+ t.Helper()
+ w := &markerWriter{marker: marker, fn: fn}
+ old := log.Writer()
+ flags := log.Flags()
+ log.SetOutput(w)
+ log.SetFlags(0)
+ t.Cleanup(func() { log.SetOutput(old); log.SetFlags(flags) })
+ return w.String
+}
+
+type markerWriter struct {
+ mu sync.Mutex
+ buf bytes.Buffer
+ marker string
+ fn func()
+ fired bool
+}
+
+func (w *markerWriter) Write(p []byte) (int, error) {
+ w.mu.Lock()
+ n, err := w.buf.Write(p)
+ fire := !w.fired && bytes.Contains(p, []byte(w.marker))
+ if fire {
+ w.fired = true
+ }
+ w.mu.Unlock()
+ if fire {
+ w.fn()
+ }
+ return n, err
+}
+
+func (w *markerWriter) String() string {
+ w.mu.Lock()
+ defer w.mu.Unlock()
+ return w.buf.String()
+}
diff --git a/internal/host/movesphase1_test.go b/internal/host/movesphase1_test.go
index a5559b936..4d6d03cec 100644
--- a/internal/host/movesphase1_test.go
+++ b/internal/host/movesphase1_test.go
@@ -428,27 +428,52 @@ func TestPreparingTouchesTheTargetRegionAndBoxFirst(t *testing.T) {
// A failed copy is retried only by calling again: the lane asks the guard
// again before the retry, and a lease past the bound by then ends the round
// with no second request.
+//
+// A round's copies run side by side (lease.MirrorPrefixKeep), so the test
+// pins nothing to their order. The lease is cut short when the lane
+// announces its retry, after every copy of the first round has returned:
+// cutting it from inside the failing copy raced the sibling copy, which
+// could pass its guard first (a second request, CI 2026-10-27) or be
+// refused first (the round ending on the refusal with no retry). And the
+// retry is told apart per object: only the object whose copy failed is
+// sure to be copied again.
func TestStagingWriterRetriesWithANewCheck(t *testing.T) {
h := newPhase1Harness(t)
h.start(t, "manual")
// The first copy fails, and by its retry the lease is too close to
- // its end.
- copies := 0
+ // its end. failAt runs under the fake's lock, and so does the
+ // announcement's read of failedKey.
+ copied := map[string]int{}
+ failedKey, announced, afterRetry := "", false, 0
h.f.failAt = func(op, key string) error {
if op != "copy" {
return nil
}
- copies++
- if copies == 1 {
- h.lease.until = h.clock.now.Add(time.Minute)
+ copied[key]++
+ if announced {
+ afterRetry++
+ }
+ if failedKey == "" {
+ failedKey = key
return errors.New("injected 503")
}
return nil
}
- logs := captureLog(t)
+ logs := captureLogThen(t, "retrying once", func() {
+ h.f.mu.Lock()
+ announced = true
+ h.f.mu.Unlock()
+ safe := h.clock.Now().Add(time.Minute)
+ h.lease.mu.Lock()
+ h.lease.until = safe
+ h.lease.mu.Unlock()
+ })
h.pass(t)
- if copies != 1 {
- t.Fatalf("%d copy requests reached the store, want 1", copies)
+ if failedKey == "" {
+ t.Fatal("no copy request reached the store")
+ }
+ if copied[failedKey] != 1 || afterRetry != 0 {
+ t.Fatalf("the failed object was requested %d times, %d requests after the retry was announced; want 1 and 0 (%v)", copied[failedKey], afterRetry, copied)
}
if !strings.Contains(logs(), "retrying once") || !strings.Contains(logs(), moves.ErrLeaseUnsafe.Error()) {
t.Fatalf("the retry did not go back to the guard:\n%s", logs())
@@ -457,23 +482,23 @@ func TestStagingWriterRetriesWithANewCheck(t *testing.T) {
// With the lease safe, the retry's request is signed at a new check.
h2 := newPhase1Harness(t)
h2.start(t, "manual")
- var signs []time.Time
- failed := false
+ var failedAt string
+ signs := map[string][]time.Time{}
h2.f.failAt = func(op, key string) error {
if op != "copy" {
return nil
}
calls := h2.f.at
- signs = append(signs, calls[len(calls)-1].SignAt)
- if !failed {
- failed = true
+ signs[key] = append(signs[key], calls[len(calls)-1].SignAt)
+ if failedAt == "" {
+ failedAt = key
return errors.New("injected 503")
}
return nil
}
h2.pass(t)
- if len(signs) < 2 || !signs[1].After(signs[0]) {
- t.Fatalf("the retry reused the first signature time: %v", signs)
+ if s := signs[failedAt]; len(s) < 2 || !s[1].After(s[0]) {
+ t.Fatalf("the retry of %s reused the first signature time: %v", failedAt, s)
}
}