carlos / platform Public

Region HA dead-holder backoff: every holder dial goes through the gate (Astra R3 READY AFTER FIXES, P2-1; census approved at c88f58a9; the coordinator's reversed ruling). forwardDialGate now serializes a holder address's TCP dials: at most one is in flight, always. After a dial connects, the next waiter takes the gate and dials while the rest keep waiting on it; a burst toward a healthy holder dials one at a time. A dial that fails answers every one of its waiters with its error, whenever it fails (a black-holed dial fails at its deadline, when its standing window for later arrivals has also run out), and later arrivals until its forwardDialTimeout has run out. Each caller's own deadline (forwardTransport's forwardDialTimeout) bounds its whole wait, queueing and dial together: the dial in flight is detached from its caller's cancellation but keeps its deadline. No connection is shared across TLS server names. TestForwardDialGate gains: after a success the next waiter takes the gate (second dial held: at most one at once; it fails: the remaining waiters share its error with no further dial), a healthy burst dials one at a time, and a waiter's deadline bounds its queueing. TestDeadHolderGuardRealWiring unchanged and green. Census: the edge envelope re-pinned in the same overlay; unit rows and seal unchanged (8f6a1865). Not released.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 0de2bcd891283e3d6887fd1a54bfcdab4f6bdf07 parent 68c172f
3 files changed, +120 −29
  • internal/edge/forwardha.go +25 −13
  • internal/edge/forwardha_test.go +94 −15
  • internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv +1 −1

Intent

Human

<task-notification> <task-id>b8nmzdih2</task-id> <tool-use-id>toolu_01QtUQPttQwrqosLhFKaBsRF</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/953e7524-2e06-4308-b9ce-fd1b38579971/tasks/b8nmzdih2.output</output-file> <status>completed</status> <summary>Background command "Wait for Astra's final re-review" completed (exit code 0)</summary> </task-notification>

diff --git a/internal/edge/forwardha.go b/internal/edge/forwardha.go
index d6395046c..c2bfd4bd1 100644
--- a/internal/edge/forwardha.go
+++ b/internal/edge/forwardha.go
@@ -270,15 +270,18 @@ func (e *Edge) forwardTransport(dial string) *http.Transport {
return tr
}
-// forwardDialGate coalesces one holder address's TCP dials: at most one is
-// in flight. A dial that arrives while one is in flight waits for its
-// outcome, within its own deadline. When that dial fails, every waiter fails
-// with its error at once, and so does every dial that arrives until the
-// failed one's forwardDialTimeout has run out: a box that refuses at once
-// costs no more dials than one that does not answer. When it connects,
-// each waiter dials its own connection. The dial in flight runs detached
-// from its own caller's cancellation, so a visitor hanging up does not fail
-// the others; forwardDialTimeout bounds it.
+// forwardDialGate serializes one holder address's TCP dials: at most one is
+// in flight, always. A dial that arrives while one is in flight waits for
+// its outcome. A failure answers every waiter at once with its error, and
+// every dial that arrives until the failed one's forwardDialTimeout has run
+// out: a box that refuses at once costs no more dials than one that does
+// not answer. A success lets the next waiter take the gate and dial, while
+// the rest wait on that one; a burst toward a healthy holder dials one at a
+// time, which a same-region, millisecond dial affords. Each caller's own
+// deadline (forwardTransport's forwardDialTimeout) bounds its whole wait,
+// queueing and dial together. The dial in flight is detached from its
+// caller's cancellation, not from its deadline, so a visitor hanging up
+// does not fail the others.
type forwardDialGate struct {
mu sync.Mutex
last *forwardDialCall
@@ -291,8 +294,12 @@ type forwardDialCall struct {
}
func (g *forwardDialGate) dial(ctx context.Context, connect func(context.Context) (net.Conn, error)) (net.Conn, error) {
- g.mu.Lock()
- if c := g.last; c != nil {
+ for {
+ g.mu.Lock()
+ c := g.last
+ if c == nil {
+ break // g.mu held: take the gate
+ }
select {
case <-c.done:
if c.err != nil && time.Now().Before(c.until) {
@@ -307,16 +314,21 @@ func (g *forwardDialGate) dial(ctx context.Context, connect func(context.Context
return nil, ctx.Err()
}
if c.err != nil {
- return nil, c.err
+ return nil, c.err // the dial waited on failed: so do its waiters
}
- return connect(ctx)
+ continue // it connected: take the gate, or wait on whoever did
}
+ break // settled and not a standing failure: take the gate
}
c := &forwardDialCall{done: make(chan struct{})}
g.last = c
g.mu.Unlock()
start := time.Now()
dctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), forwardDialTimeout)
+ if d, ok := ctx.Deadline(); ok {
+ cancel()
+ dctx, cancel = context.WithDeadline(context.WithoutCancel(ctx), d)
+ }
defer cancel()
conn, err := connect(dctx)
g.mu.Lock()
diff --git a/internal/edge/forwardha_test.go b/internal/edge/forwardha_test.go
index 5190ef645..ef18d4d4b 100644
--- a/internal/edge/forwardha_test.go
+++ b/internal/edge/forwardha_test.go
@@ -724,45 +724,124 @@ func TestForwardDialGate(t *testing.T) {
t.Fatalf("after the failure's time: %d dials, want 2", n)
}
})
- t.Run("a success lets each waiter dial", func(t *testing.T) {
- g := &forwardDialGate{}
+ // countingConnect is a connect that records the most dials in flight at
+ // once; each dial n waits on hold(n), then answers with fail(n).
+ countingConnect := func(hold func(n int64), fail func(n int64) error) (func(context.Context) (net.Conn, error), *atomic.Int64, *atomic.Int64) {
var dials, inFlight, most atomic.Int64
- release := make(chan struct{})
- connect := func(context.Context) (net.Conn, error) {
+ return func(context.Context) (net.Conn, error) {
n := inFlight.Add(1)
defer inFlight.Add(-1)
for m := most.Load(); n > m && !most.CompareAndSwap(m, n); m = most.Load() {
}
- if dials.Add(1) == 1 {
- <-release
+ d := dials.Add(1)
+ hold(d)
+ if err := fail(d); err != nil {
+ return nil, err
}
a, b := net.Pipe()
b.Close()
return a, nil
+ }, &dials, &most
+ }
+ t.Run("after a success the next waiter takes the gate", func(t *testing.T) {
+ g := &forwardDialGate{}
+ release := []chan struct{}{make(chan struct{}), make(chan struct{})}
+ gone := errors.New("connection refused")
+ connect, dials, most := countingConnect(
+ func(n int64) {
+ if n <= 2 {
+ <-release[n-1]
+ }
+ },
+ func(n int64) error {
+ if n == 2 {
+ return gone
+ }
+ return nil
+ })
+ type result struct {
+ conn net.Conn
+ err error
}
- errs := make(chan error, 5)
+ results := make(chan result, 5)
for range 5 {
go func() {
conn, err := g.dial(ctx, connect)
if conn != nil {
conn.Close()
}
- errs <- err
+ results <- result{conn, err}
}()
}
laWaitFor(t, "the first dial", func() bool { return dials.Load() == 1 })
time.Sleep(50 * time.Millisecond)
- if most.Load() != 1 {
- t.Fatalf("%d dials in flight before the first connected, want 1", most.Load())
+ if most.Load() != 1 || dials.Load() != 1 {
+ t.Fatalf("before the first connected: %d dials, %d at once; want 1 and 1", dials.Load(), most.Load())
}
- close(release)
- for range 5 {
+ close(release[0]) // the first connects
+ if r := <-results; r.err != nil {
+ t.Fatalf("the first dial: %v", r.err)
+ }
+ // The next waiter takes the gate; the rest keep waiting.
+ laWaitFor(t, "the second dial", func() bool { return dials.Load() == 2 })
+ time.Sleep(50 * time.Millisecond)
+ if most.Load() != 1 || dials.Load() != 2 {
+ t.Fatalf("after the first connected: %d dials, %d at once; want 2 and 1", dials.Load(), most.Load())
+ }
+ select {
+ case r := <-results:
+ t.Fatalf("a waiter returned %v while the second dial was held", r.err)
+ default:
+ }
+ close(release[1]) // the second fails: the holder is gone
+ for i := range 4 {
+ if r := <-results; !errors.Is(r.err, gone) {
+ t.Fatalf("waiter %d after the second dial failed: %v, want its error", i, r.err)
+ }
+ }
+ if dials.Load() != 2 || most.Load() != 1 {
+ t.Fatalf("%d dials, %d at once; want 2 and 1", dials.Load(), most.Load())
+ }
+ })
+ t.Run("a healthy burst dials one at a time", func(t *testing.T) {
+ g := &forwardDialGate{}
+ connect, dials, most := countingConnect(func(int64) { time.Sleep(5 * time.Millisecond) }, func(int64) error { return nil })
+ errs := make(chan error, 10)
+ for range 10 {
+ go func() {
+ conn, err := g.dial(ctx, connect)
+ if conn != nil {
+ conn.Close()
+ }
+ errs <- err
+ }()
+ }
+ for range 10 {
if err := <-errs; err != nil {
- t.Fatalf("a dial after a success: %v", err)
+ t.Fatalf("a dial to a healthy holder: %v", err)
}
}
- if n := dials.Load(); n != 5 {
- t.Fatalf("%d dials, want one each", n)
+ if dials.Load() != 10 || most.Load() != 1 {
+ t.Fatalf("%d dials, %d at once; want one each, one at a time", dials.Load(), most.Load())
+ }
+ })
+ t.Run("a waiter's deadline bounds its queueing", func(t *testing.T) {
+ g := &forwardDialGate{}
+ release := make(chan struct{})
+ defer close(release)
+ go g.dial(ctx, func(context.Context) (net.Conn, error) {
+ <-release
+ a, b := net.Pipe()
+ b.Close()
+ return a, nil
+ })
+ laWaitFor(t, "the first dial", func() bool { g.mu.Lock(); defer g.mu.Unlock(); return g.last != nil })
+ wctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
+ defer cancel()
+ start := time.Now()
+ _, err := g.dial(wctx, func(dctx context.Context) (net.Conn, error) { <-dctx.Done(); return nil, dctx.Err() })
+ if !errors.Is(err, context.DeadlineExceeded) || time.Since(start) > time.Second {
+ t.Fatalf("a queued waiter past its deadline: %v after %v", err, time.Since(start))
}
})
t.Run("a waiter's own deadline", func(t *testing.T) {
diff --git a/internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv b/internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv
index 818a16697..2bf91c9a0 100644
--- a/internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv
+++ b/internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv
@@ -5,4 +5,4 @@
internal/edge/forwardha.go Edge.frontForwardHA legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's edge decides at the front door whether to forward a visitor request: reads the route's HA flag and host from the box's own registry row and asks the activator; forwards only to a box in view over the existing signed hop; root never calls it;rev3=census-ruling-region-ha-dead-holder-backoff;basis=ForwardTarget returns a ForwardDecision (Ref, Refuse, Gen); a Refuse is answered at the front door with the existing unreachable answer (503, Retry-After: 10, same body, via answerHolderUnreachable), with no forward, dial, note or per-request step. A forward passes the decision to forwardHA and NoteForward carries Ref and Gen back. The round-1 connection callback (NoteConnected) is gone: the dial bound now lives in forwardTransport's forwardDialGate, which coalesces TCP dials per holder address in memory. The same 5 read sites; no new read; root never calls it. registry:HA@225,registry:Host@563,whole:Route@228,whole:Route@405,whole:Route@566 c5b342e1a71cff53bdc6666821708f962c5b2fc73bd6cba2e983cff54c839626
internal/activator/haforward.go Service.ForwardTarget legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's activator answers the edge with a holder box ref for an HA route not awake here, from its in-memory holder cache or one bounded (2 s) lock.json read through readHolderView; reads the route's HA flag and host from its own registry row; root never calls it;rev3=census-ruling-region-ha-dead-holder-backoff;basis=Returns edge.ForwardDecision (Ref, Refuse, Gen) from holderCache.resolve, in memory only: a suspect holder refuses; a positive entry forwards under its generation; a failed read that revalidates an expired suspect keeps it suspect and refuses. The round-1 cache connection gate is removed. The same single bounded (2 s) lock.json read through readHolderView on a miss; no new read and no new S3 request; root never calls it. registry:HA@100,registry:Host@1000,registry:Host@262,registry:Host@383,whole:Route@176,whole:Route@741 da8dcb16995990c9fb3c7107cd0d1ef8d5424bddea08a70ad8133801b186cc53
@coverage internal/activator d57a02d4526ac697a1885db1ffd483e922cb5529a0e90ba4c5a325c0f684f1a7
-@coverage internal/edge 725524c26589b9b81aeede93e89e42f1c7002bd122d8e112405a06cf0baef1a7
+@coverage internal/edge 2e63066e08c06ee36ad0ec6c7391b26d73fac07ce0fb341a431e06ae919a55c9