carlos / platform Public

lease, hafresh: bound set before renewals; null min_txid refused

Astra R1 on Tasks 11 and 11b: the renewer start order (P1) and a null
carlos_min_txid (P2). The skew finding is with Paul.

P1: startRenewerGen stored carlos_min_txid after newRenewer had started
its workers, so a renewal landing first could write the lock without
the bound. newRenewer now takes minTXID and stores it before either
worker starts. A seam (renewerStarted) holds the constructor until the
first renewal lands; TestRenewerFirstRenewalCarriesTheBound fails on
the old order and passes now.

P2: carlos_min_txid decoded straight into a uint64, so JSON null read
as no bound. It is now read raw: absent is no bound, and null or any
value that is not a non-negative integer refuses the lock as
malformed (TestHeldLockMinTXID).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 060d13acdd7bfacf19c8874136992efb0615351d parent 1deae8f
4 files changed, +76 −9
  • internal/hafresh/generations_test.go +2 −1
  • internal/hafresh/lock.go +12 −3
  • internal/lease/health.go +15 −5
  • internal/lease/renew_attempt_test.go +47 −0
diff --git a/internal/hafresh/generations_test.go b/internal/hafresh/generations_test.go
index fe8203672..b53973d2a 100644
--- a/internal/hafresh/generations_test.go
+++ b/internal/hafresh/generations_test.go
@@ -328,7 +328,8 @@ func TestHeldLockMinTXID(t *testing.T) {
t.Fatalf("a zero bound is written: %s", again)
}
// A value this binary cannot read is an error, never a missing bound.
- for _, bad := range []string{`"150"`, `-1`, `1.5`, `true`} {
+ // null is malformed too: absent is the only way to say "no bound".
+ for _, bad := range []string{`null`, `"150"`, `-1`, `1.5`, `true`, `{}`, `[]`} {
b := strings.Replace(string(body), "150", bad, 1)
if v, err := DecodeLock([]byte(b), ""); err == nil {
t.Errorf("%s decoded: %+v", b, v)
diff --git a/internal/hafresh/lock.go b/internal/hafresh/lock.go
index 7d9283726..25d17512d 100644
--- a/internal/hafresh/lock.go
+++ b/internal/hafresh/lock.go
@@ -149,19 +149,28 @@ func (l Lock) MarshalJSON() ([]byte, error) {
// UnmarshalJSON reads a lock body. A carlos_release_g record decodes into
// Release with Gen set. Both records at once, an empty or null
-// carlos_release_g, or a carlos_generation that is not a non-empty string
-// is an error: a generations field this binary cannot read is never read
+// carlos_release_g, a carlos_generation that is not a non-empty string, or
+// a carlos_min_txid that is present but not a non-negative integer (null
+// included) is an error: a generations field this binary cannot read is never read
// as absent, so the lock is never taken as an ordinary one.
func (l *Lock) UnmarshalJSON(b []byte) error {
var j struct {
lockJSON
ReleaseG json.RawMessage `json:"carlos_release_g"`
GenName json.RawMessage `json:"carlos_generation"`
+ MinTXID json.RawMessage `json:"carlos_min_txid"`
}
if err := json.Unmarshal(b, &j); err != nil {
return err
}
- *l = Lock{Generation: j.Generation, ExpiresAt: j.ExpiresAt, Owner: j.Owner, Release: j.Release, MinTXID: j.MinTXID}
+ *l = Lock{Generation: j.Generation, ExpiresAt: j.ExpiresAt, Owner: j.Owner, Release: j.Release}
+ if j.MinTXID != nil {
+ // Present: a non-negative integer. null is malformed, never "no
+ // bound": absent is the only way to write none.
+ if string(j.MinTXID) == "null" || json.Unmarshal(j.MinTXID, &l.MinTXID) != nil {
+ return errors.New("hafresh: carlos_min_txid is not a TXID")
+ }
+ }
if j.GenName != nil {
if err := json.Unmarshal(j.GenName, &l.GenName); err != nil || l.GenName == "" {
return errors.New("hafresh: carlos_generation is not a generation name")
diff --git a/internal/lease/health.go b/internal/lease/health.go
index 5a0ef4d91..77abcb0e8 100644
--- a/internal/lease/health.go
+++ b/internal/lease/health.go
@@ -140,9 +140,7 @@ func startRenewer(client lss3.S3API, leaser *lss3.Leaser, lease *litestream.Leas
// carlos_generation genName ("" for none) and carlos_min_txid minTXID (0
// for none): its renewals keep both, minTXID until clearMinTXID.
func startRenewerGen(client lss3.S3API, leaser *lss3.Leaser, lease *litestream.Lease, genName string, minTXID uint64, started time.Time, margin time.Duration) *renewer {
- rn := newRenewer(client, leaser, lease, genName, started, margin, pauseLimitFor(leaser.TTL), nil)
- rn.minTXID.Store(minTXID)
- return rn
+ return newRenewer(client, leaser, lease, genName, minTXID, started, margin, pauseLimitFor(leaser.TTL), nil)
}
// clearMinTXID drops carlos_min_txid from the renewals after this one: the
@@ -151,14 +149,26 @@ func (rn *renewer) clearMinTXID() { rn.minTXID.Store(0) }
// startRenewerPause is startRenewer with the pause limit given (0 = off).
func startRenewerPause(client lss3.S3API, leaser *lss3.Leaser, lease *litestream.Lease, started time.Time, margin, pause time.Duration, beforeWrite func()) *renewer {
- return newRenewer(client, leaser, lease, "", started, margin, pause, beforeWrite)
+ return newRenewer(client, leaser, lease, "", 0, started, margin, pause, beforeWrite)
}
-func newRenewer(client lss3.S3API, leaser *lss3.Leaser, lease *litestream.Lease, genName string, started time.Time, margin, pause time.Duration, beforeWrite func()) *renewer {
+// renewerStarted, when set, runs in newRenewer once both workers have
+// started and before it returns (a test seam for a renewal landing then;
+// nil in production).
+var renewerStarted func(*renewer)
+
+// newRenewer starts rn's workers. Everything a renewal writes (genName,
+// minTXID) is set before either starts: the first renewal can come before
+// this returns.
+func newRenewer(client lss3.S3API, leaser *lss3.Leaser, lease *litestream.Lease, genName string, minTXID uint64, started time.Time, margin, pause time.Duration, beforeWrite func()) *renewer {
ctx, cancel := context.WithCancel(context.Background())
rn := &renewer{ctx: ctx, client: client, leaser: leaser, genName: genName, beforeWrite: beforeWrite, lease: lease, until: started.Add(leaser.TTL - margin), margin: margin, pause: pause, seen: time.Now(), cancel: cancel, done: make(chan struct{}), watchDone: make(chan struct{}), changed: make(chan struct{}, 1), lost: make(chan struct{})}
+ rn.minTXID.Store(minTXID)
go rn.watch(ctx)
go rn.renew(ctx)
+ if renewerStarted != nil {
+ renewerStarted(rn)
+ }
return rn
}
diff --git a/internal/lease/renew_attempt_test.go b/internal/lease/renew_attempt_test.go
index b9a76bd57..6b97dc708 100644
--- a/internal/lease/renew_attempt_test.go
+++ b/internal/lease/renew_attempt_test.go
@@ -3,6 +3,7 @@ package lease
import (
"context"
"errors"
+ "strings"
"sync/atomic"
"testing"
"time"
@@ -267,3 +268,49 @@ func TestRenewAttemptUsesEarlierSafeUntil(t *testing.T) {
t.Error("expired handle retained SafeUntil")
}
}
+
+// Task 11b, R1: the first renewal of a generations lock carries its take
+// bound (carlos_min_txid), even one that lands before the renewer's
+// constructor returns. The seam holds the constructor until the renew
+// loop's first write has landed, so a bound set after the workers start
+// would be missing from it.
+func TestRenewerFirstRenewalCarriesTheBound(t *testing.T) {
+ const ttl = 300 * time.Millisecond // a renewal every 100ms
+ backend := &lockStore{}
+ leaser := lss3.NewLeaser()
+ leaser.SetClient(backend)
+ leaser.TTL = ttl
+ lock, err := leaser.AcquireLease(context.Background())
+ if err != nil {
+ t.Fatal(err)
+ }
+ backend.mu.Lock()
+ acquired := backend.puts
+ backend.mu.Unlock()
+ var first []byte
+ renewerStarted = func(*renewer) {
+ for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(time.Millisecond) {
+ backend.mu.Lock()
+ if backend.puts > acquired {
+ first = append([]byte(nil), backend.body...)
+ backend.mu.Unlock()
+ return
+ }
+ backend.mu.Unlock()
+ }
+ }
+ t.Cleanup(func() { renewerStarted = nil })
+ rn := startRenewerGen(backend, leaser, lock, "g-00000000000000000002", 77, time.Now(), marginForTTL(ttl))
+ renewerStarted = nil
+ t.Cleanup(func() {
+ if err := leaser.ReleaseLease(context.Background(), rn.stop()); err != nil {
+ t.Error(err)
+ }
+ })
+ if first == nil {
+ t.Fatal("fixture: no renewal landed while the constructor ran")
+ }
+ if !strings.Contains(string(first), `"carlos_min_txid":77`) || !strings.Contains(string(first), `"carlos_generation":"g-00000000000000000002"`) {
+ t.Fatalf("the first renewal wrote %s, want carlos_generation and carlos_min_txid 77", first)
+ }
+}