activator: log withdrawal causes and retry pending proofs sooner
Keep withdrawal proof gates and pending codes unchanged. Retry pending hosts after 30 seconds, doubling to five minutes and resetting on success. Cover restart admission and retry timing through real reconcile.
Implemented-by: Astra (codex)
internal/activator/poolwithdrawal.go+11 −11internal/activator/poolwithdrawal_retry_test.go+119 −0internal/provision/poolwithdrawal.go+26 −5internal/provision/poolwithdrawal_retry_test.go+58 −0internal/provision/poolwithdrawal_test.go+2 −2
Intent
<task-notification> <task-id>bi0340sze</task-id> <tool-use-id>toolu_01RVf8a28xUwZeRLMEX7sqPT</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/87a21814-5655-4016-9e7e-41935ee8e96e/tasks/bi0340sze.output</output-file> <status>completed</status> <summary>Background command "W=/home/paulca/amadan.net/carlos/platform/.claude/worktrees/g2r-fixes/.superpowers/sdd/2026-11-06-region-ha-g2r-fixes; echo "Task 4: fix round 1 committed a5ecbe76; re-review dispatched" >> $W/progress.md; for i in $(seq 1 340); do [ -f $W/t2f1.done ] && break; sleep 10; done; cat $W/t2f1.done 2>/dev/null; tail -1 $W/t2f1.log" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>a1c5d098c858fb6b5</task-id> <tool-use-id>toolu_01GySrrRBc3UiMFyZyW5WP4F</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/87a21814-5655-4016-9e7e-41935ee8e96e/tasks/a1c5d098c858fb6b5.output</output-file> <status>completed</status> <summary>Agent "Re-review Task 2 fix round 1" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>I re-reviewed Task 2 fix round 1 (445e881d..5ee6d6ae) in `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/g2r-fixes`. All three findings are addressed and I found no new Critical or Important breakage. I ran the three targeted tests once and they passed. **Finding 1 (Critical), HTTP/2 body check: ADDRESSED.** - `internal/edge/backhaul_holder.go:157` now treats a request as bodiless when `r.ContentLength == 0 && len(r.TransferEncoding) == 0`. - The HTTP/2 client test is in `backhaul_holder_export_test.go:196` and asserts `ProtoMajor == 2` at `:82`. - The new `http2_GET` and `http2_HEAD` cases in `TestBackhaulFollowsHolderOnce` failed before the fix, according to the report's red run. - How each request shape behaves now: - HTTP/1.1 GET with `Content-Length: 0` has length 0, so it follows. That is correct, since there is nothing to replay. - Chunked requests have length -1 and `TransferEncoding=[chunked]`, so they don't follow. - HTTP/2 requests with unknown length (-1) don't follow. - HTTP/2 requests whose headers end the stream have length 0, so they follow. - A WebSocket upgrade is a GET with length 0, so it follows. **Finding 2 (Minor), hint dropped on any proxy error: ADDRESSED.** - `backhaul_holder.go:123-136` watches connection events per request, and `:168` drops the hint only when a connection failed and the client is still connected. - TCP dial failures, TLS handshake failures and TLS handshake timeouts set the flag. All three mean "could not connect to that holder", which is what the finding asked for. - Response-header timeouts and resets after connecting don't drop, because a successful connection (`:125`) clears the flag first. - A client hang-up keeps the hint, because the context check at `:168` sees the cancellation. `TestBackhaulHolderKeepsMemoryOnClientHangup` covers this, and the new `tls_failure` subtest of `TestBackhaulHolderDropsMemory` covers handshake failure. - `backhaulTransport` (`edge.go:4349-4356`) dials through `net.Dialer` with the request context, so the dial-failure callback does fire. It always dials one fixed address, so there is no dual-stack race. **Finding 3 (Minor), logs: ADDRESSED.** - Learn logs at `:153-155`, follow at `:178`, and drop at `:78-82` with three reasons. - `logThrottled` (`edge.go:1503`) prints the first occurrence and then every 300th per key. Keys are box refs already checked against the region view, so the number of keys stays bounded and log volume is fine. - Learn and drop log only when the cache actually changed. **Binding rules: all still hold.** - One follow only: the second call passes `follow=false` (`:179`), so a third hop is impossible. - A refusal ends in `errBackhaulHolder`, which `onError` swallows (`:165`) without writing anything, so the first 503 never reaches the client. - The holder is checked against the region view before it is remembered or followed (`:149-152`). - The follow target comes only from the remote edge's 503 header, read in the transport's response check (`:137-141`), never from the app. **New breakage** - Minor, with no fix required (`backhaul_holder.go:125-129`). Go may hand request A an idle connection while A's own dial is still running. If that dial then fails, the flag flips back to true after the connection was taken. If A later errors after connecting, the hint is dropped. The failed dial was to the same holder address, so this is real evidence of a connectivity problem and the damage is one dropped hint. - Note only, not a bug (`:179`). The follow request carries the first attempt's tracking hooks as well as its own, because Go chains them. The first attempt's flag is never read again, so this is harmless. All addressed: yes</result> <usage><subagent_tokens>48945</subagent_tokens><tool_uses>4</tool_uses><duration_ms>72387</duration_ms></usage> </task-notification>
Attributed to this commit: 12.0 g CO₂e (1,402,774 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on region-ha-g2r-fixes, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/activator/poolwithdrawal.go b/internal/activator/poolwithdrawal.go| index 84ccd6790..2ec9c6fcd 100644 |
| --- a/internal/activator/poolwithdrawal.go |
| +++ b/internal/activator/poolwithdrawal.go |
| @@ -41,7 +41,7 @@ func (a *Service) PreparePoolWithdrawal(ctx context.Context, rt registry.Route) |
| a.mu.Lock() |
| defer a.mu.Unlock() |
| if err != nil || current == nil || !samePoolWithdrawalRoute(*current, withdrawalRow(rt)) || eligible { |
| - return pending("changed") |
| + return pending("changed", err) |
| } |
| if a.shuttingDown { |
| return pending("shutdown") |
| @@ -92,7 +92,7 @@ func (a *Service) provePoolWithdrawal(b *ExecBackend, job *poolWithdrawal) { |
| job.err, job.done = err, true |
| a.mu.Unlock() |
| if err != nil { |
| - log.Printf("ha_pool_withdrawal_proof_finish host=%s result=error error=%v", job.route.Host, err) |
| + log.Printf("ha_pool_withdrawal_proof_finish host=%s result=error error=%v cause=%v", job.route.Host, err, errors.Unwrap(err)) |
| } else { |
| log.Printf("ha_pool_withdrawal_proof_finish host=%s result=success", job.route.Host) |
| } |
| @@ -105,7 +105,7 @@ func (a *Service) poolWithdrawalProof(ctx context.Context, b *ExecBackend, job * |
| evidence, err := a.FenceAndStopAs(stopCtx, rt.Host, 1, job.owner) |
| stopCancel() |
| if err != nil && !errors.Is(err, ErrStopUnproven) { |
| - return pending("stop") |
| + return pending("stop", err) |
| } |
| a.mu.Lock() |
| busy := a.entries[rt.Host] != nil || a.unprovenWakes[rt.Host] != nil || len(a.straySidecars[rt.Host]) != 0 || a.releaseOps[rt.Host] != nil |
| @@ -123,7 +123,7 @@ func (a *Service) poolWithdrawalProof(ctx context.Context, b *ExecBackend, job * |
| } |
| } |
| if err := b.poolWithdrawalEmpty(rt); err != nil { |
| - return pending("cgroup") |
| + return pending("cgroup", err) |
| } |
| if rt.Kind == "static" && (history != nil || rt.Hibernate || rt.Socket != "" || rt.Addr != "" || rt.Addrs != "" || rt.DBPath != "" || rt.Runtime != "" || rt.Args != "") { |
| return pending("ownership") |
| @@ -137,7 +137,7 @@ func (a *Service) poolWithdrawalProof(ctx context.Context, b *ExecBackend, job * |
| if rt.Kind != "static" { |
| facts, err := b.Cfg.Record(ctx, rt) |
| if err != nil { |
| - return pending("record") |
| + return pending("record", err) |
| } |
| ha = ha || facts.HA |
| if b.Lifetimes != nil { |
| @@ -147,7 +147,7 @@ func (a *Service) poolWithdrawalProof(ctx context.Context, b *ExecBackend, job * |
| } |
| view, err = b.readHolderView(ctx, rt) |
| if err != nil { |
| - return pending("release") |
| + return pending("release", err) |
| } |
| ha = ha || view.HAOn() |
| if !ha && evidence == nil && !view.Absent { |
| @@ -168,7 +168,7 @@ func (a *Service) poolWithdrawalProof(ctx context.Context, b *ExecBackend, job * |
| } |
| if ha { |
| if err := b.haContained(rt); err != nil { |
| - return pending("cgroup") |
| + return pending("cgroup", err) |
| } |
| if life == "" { |
| return pending("lifetime") |
| @@ -180,12 +180,12 @@ func (a *Service) poolWithdrawalProof(ctx context.Context, b *ExecBackend, job * |
| } |
| if errors.Is(rerr, fs.ErrNotExist) && b.StartupSwept != nil && b.StartupSwept() && !b.sameBox.acquiredHere(rt.Host) { |
| if _, err := statFileID(b.pathsFor(rt).dbPath); !errors.Is(err, fs.ErrNotExist) { |
| - return pending("release") |
| + return pending("release", err) |
| } |
| } else { |
| m, self := rec.Marker, b.self() |
| if rerr != nil || view.Absent || m.FilesGen == "" || !m.Clean || m.V != 1 || m.Box != self.Ref || m.IID != self.IID || m.Lifetime != life { |
| - return pending("release") |
| + return pending("release", rerr) |
| } |
| if view.Lock.Release != nil && *view.Lock.Release == m { |
| if view.Lock.Owner != hafresh.MarkerOwner(m) { |
| @@ -208,11 +208,11 @@ func (a *Service) poolWithdrawalProof(ctx context.Context, b *ExecBackend, job * |
| } |
| timers, err := b.Registry.TimersFor(rt.Host) |
| if err != nil { |
| - return pending("duties") |
| + return pending("duties", err) |
| } |
| current, err := b.Registry.Get(rt.Host) |
| if err != nil || current == nil || !samePoolWithdrawalRoute(*current, withdrawalRow(rt)) || b.poolEligible(rt) { |
| - return pending("changed") |
| + return pending("changed", err) |
| } |
| if current.NextDue != 0 || len(timers) != 0 { |
| return pending("duties") |
diff --git a/internal/activator/poolwithdrawal_retry_test.go b/internal/activator/poolwithdrawal_retry_test.go| new file mode 100644 |
| index 000000000..a584b3007 |
| --- /dev/null |
| +++ b/internal/activator/poolwithdrawal_retry_test.go |
| @@ -0,0 +1,119 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "log" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +func TestPoolWithdrawalReconcileRetriesAdmissionAfterRestart(t *testing.T) { |
| + s := haService(t, "shop.example.com") |
| + s = s.restart(t) |
| + store := release.NewDirStore(t.TempDir()) |
| + rec := haRecord(s.rt.Host, true) |
| + rec.Region = "ie" |
| + if err := provision.CreateRecord(t.Context(), store, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + s.b.Cfg.Record = func(ctx context.Context, rt registry.Route) (RecordFacts, error) { |
| + rec, err := provision.GetRecord(ctx, store, rt.Account, rt.App, rt.Host) |
| + if err != nil { |
| + return RecordFacts{}, err |
| + } |
| + return RecordFactsOf(*rec) |
| + } |
| + row := s.rt |
| + row.Region, row.HA = rec.Region, true |
| + if err := s.b.Registry.Upsert(row); err != nil { |
| + t.Fatal(err) |
| + } |
| + withdrawalFixture(t, s) |
| + var allowed atomic.Bool |
| + s.b.TenantStartAllowed = func(registry.Route) bool { return allowed.Load() } |
| + view := provision.NewRecordView(store, release.NewChangeWatch(store)) |
| + if err := view.Rebuild(t.Context()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !view.Snapshot().Complete { |
| + t.Fatal("incomplete record view") |
| + } |
| + var logs syncBuffer |
| + old := log.Writer() |
| + log.SetOutput(&logs) |
| + t.Cleanup(func() { log.SetOutput(old) }) |
| + now := time.Now() |
| + withdrawn := 0 |
| + cfg := provision.ReconcileConfig{ |
| + Region: rec.Region, Pools: []string{"other"}, |
| + Now: func() time.Time { return now }, |
| + WithdrawalBackoff: &provision.PoolWithdrawalBackoff{}, |
| + WithdrawPool: s.a.PreparePoolWithdrawal, |
| + PoolWithdrawn: func() { withdrawn++; s.a.ReapPoolWithdrawals() }, |
| + } |
| + reconcile := func() { |
| + t.Helper() |
| + if err := provision.Reconcile(t.Context(), view.Snapshot().Store(store), s.b.Registry, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + awaitFinish := func(count int) { |
| + t.Helper() |
| + deadline := time.Now().Add(4 * time.Second) |
| + for time.Now().Before(deadline) { |
| + if strings.Count(logs.String(), "ha_pool_withdrawal_proof_finish host="+row.Host) == count { |
| + return |
| + } |
| + time.Sleep(time.Millisecond) |
| + } |
| + t.Fatalf("proof did not finish: %s", logs.String()) |
| + } |
| + for cycle := 0; cycle < 2; cycle++ { |
| + start := now |
| + allowed.Store(false) |
| + if err := s.b.Registry.Upsert(row); err != nil { |
| + t.Fatal(err) |
| + } |
| + reconcile() |
| + awaitFinish(2*cycle + 1) |
| + want := "result=error error=ha_pool_withdrawal_pending code=cgroup cause=" + ErrTenantAdmission.Error() |
| + if !strings.Contains(logs.String(), want) { |
| + t.Errorf("missing admission cause %q: %s", want, logs.String()) |
| + } |
| + s.a.mu.Lock() |
| + err := s.a.poolWithdrawals[row.Host].err |
| + s.a.mu.Unlock() |
| + if !errors.Is(err, ErrTenantAdmission) { |
| + t.Errorf("proof did not wrap admission cause: %v", err) |
| + } |
| + now = start.Add(2 * time.Second) |
| + reconcile() |
| + allowed.Store(true) |
| + now = start.Add(31 * time.Second) |
| + reconcile() |
| + if strings.Count(logs.String(), "ha_pool_withdrawal_proof_start host="+row.Host) != 2*cycle+1 { |
| + t.Fatal("retried before 30-second backoff expired") |
| + } |
| + if got, err := s.b.Registry.Get(row.Host); err != nil || got == nil || withdrawn != cycle || !s.a.Fenced(row.Host) { |
| + t.Fatalf("pending proof lost its row or fence: row=%+v err=%v withdrawn=%d", got, err, withdrawn) |
| + } |
| + now = start.Add(32 * time.Second) |
| + reconcile() |
| + awaitFinish(2*cycle + 2) |
| + now = start.Add(34 * time.Second) |
| + reconcile() |
| + if got, err := s.b.Registry.Get(row.Host); err != nil || got != nil || withdrawn != cycle+1 || s.a.Fenced(row.Host) { |
| + t.Fatalf("withdrawal did not finish within a minute: row=%+v err=%v withdrawn=%d logs=%s", got, err, withdrawn, logs.String()) |
| + } |
| + if lockWrites(t, s.b, s.rt) != 0 { |
| + t.Fatal("withdrawal wrote a lease") |
| + } |
| + } |
| +} |
diff --git a/internal/provision/poolwithdrawal.go b/internal/provision/poolwithdrawal.go| index 939fc1b17..fb4762bfd 100644 |
| --- a/internal/provision/poolwithdrawal.go |
| +++ b/internal/provision/poolwithdrawal.go |
| @@ -145,6 +145,7 @@ func (cfg ReconcileConfig) withdrawPools(ctx context.Context, s release.Store, r |
| code = "changed" |
| break |
| } |
| + cfg.WithdrawalBackoff.succeeded(rt.Host) |
| if cfg.PoolWithdrawn != nil { |
| cfg.PoolWithdrawn() |
| } |
| @@ -165,16 +166,21 @@ func (cfg ReconcileConfig) withdrawPools(ctx context.Context, s release.Store, r |
| var errPoolWithdrawalPending = errors.New("ha_pool_withdrawal_pending") |
| -type poolWithdrawalPendingError struct{ code string } |
| +type poolWithdrawalPendingError struct { |
| + code string |
| + cause error |
| +} |
| func (e *poolWithdrawalPendingError) Error() string { |
| return fmt.Sprintf("%s code=%s", errPoolWithdrawalPending, e.code) |
| } |
| -func (e *poolWithdrawalPendingError) Unwrap() error { return errPoolWithdrawalPending } |
| +func (e *poolWithdrawalPendingError) Unwrap() error { return e.cause } |
| + |
| +func (e *poolWithdrawalPendingError) Is(target error) bool { return target == errPoolWithdrawalPending } |
| -func PoolWithdrawalPending(code string) error { |
| - return &poolWithdrawalPendingError{code: code} |
| +func PoolWithdrawalPending(code string, cause ...error) error { |
| + return &poolWithdrawalPendingError{code: code, cause: errors.Join(cause...)} |
| } |
| type PoolWithdrawalBackoff struct { |
| @@ -186,6 +192,7 @@ type poolWithdrawalRetry struct { |
| row registry.Route |
| pools string |
| after time.Time |
| + delay time.Duration |
| } |
| func poolWithdrawalRetryRow(rt registry.Route) registry.Route { |
| @@ -235,5 +242,19 @@ func (b *PoolWithdrawalBackoff) pending(rt registry.Route, pools string, now tim |
| if b.hosts == nil { |
| b.hosts = make(map[string]poolWithdrawalRetry) |
| } |
| - b.hosts[rt.Host] = poolWithdrawalRetry{row: poolWithdrawalRetryRow(rt), pools: pools, after: now.Add(5 * time.Minute)} |
| + row := poolWithdrawalRetryRow(rt) |
| + delay := 30 * time.Second |
| + if previous, ok := b.hosts[rt.Host]; ok && previous.row == row && previous.pools == pools { |
| + delay = min(previous.delay*2, 5*time.Minute) |
| + } |
| + b.hosts[rt.Host] = poolWithdrawalRetry{row: row, pools: pools, after: now.Add(delay), delay: delay} |
| +} |
| + |
| +func (b *PoolWithdrawalBackoff) succeeded(host string) { |
| + if b == nil { |
| + return |
| + } |
| + b.mu.Lock() |
| + defer b.mu.Unlock() |
| + delete(b.hosts, host) |
| } |
diff --git a/internal/provision/poolwithdrawal_retry_test.go b/internal/provision/poolwithdrawal_retry_test.go| new file mode 100644 |
| index 000000000..cfef582a7 |
| --- /dev/null |
| +++ b/internal/provision/poolwithdrawal_retry_test.go |
| @@ -0,0 +1,58 @@ |
| +package provision |
| + |
| +import ( |
| + "errors" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +func TestPoolWithdrawalPendingWrapsCause(t *testing.T) { |
| + cause := errors.New("containment unavailable") |
| + for _, err := range []error{PoolWithdrawalPending("cgroup"), PoolWithdrawalPending("cgroup", nil), PoolWithdrawalPending("cgroup", cause)} { |
| + var pending *poolWithdrawalPendingError |
| + if !errors.Is(err, errPoolWithdrawalPending) || !errors.As(err, &pending) || pending.code != "cgroup" || err.Error() != "ha_pool_withdrawal_pending code=cgroup" { |
| + t.Fatalf("pending code or identity changed: %v", err) |
| + } |
| + } |
| + if err := PoolWithdrawalPending("cgroup", cause); !errors.Is(err, cause) { |
| + t.Fatalf("underlying cause lost: %v", err) |
| + } |
| +} |
| + |
| +func TestPoolWithdrawalBackoffDoublesPerHost(t *testing.T) { |
| + var b PoolWithdrawalBackoff |
| + a := registry.Route{Host: "a.example.com"} |
| + z := registry.Route{Host: "z.example.com"} |
| + now := time.Now() |
| + for _, delay := range []time.Duration{30 * time.Second, time.Minute, 2 * time.Minute, 4 * time.Minute, 5 * time.Minute, 5 * time.Minute} { |
| + b.pending(a, "other", now) |
| + if !b.deferred(a, "other", now.Add(delay-time.Nanosecond)) || b.deferred(a, "other", now.Add(delay)) { |
| + t.Fatalf("retry did not wait exactly %s", delay) |
| + } |
| + now = now.Add(delay) |
| + } |
| + b.pending(z, "other", now) |
| + if !b.deferred(z, "other", now.Add(29*time.Second)) || b.deferred(z, "other", now.Add(30*time.Second)) { |
| + t.Fatal("another host inherited the capped backoff") |
| + } |
| + for _, change := range []string{"row", "pools", "removed"} { |
| + t.Run(change, func(t *testing.T) { |
| + row, pools := a, "other" |
| + switch change { |
| + case "row": |
| + row.NextDue = 1 |
| + case "pools": |
| + pools = "new" |
| + case "removed": |
| + b.prune([]registry.Route{z}, pools) |
| + } |
| + b.prune([]registry.Route{row, z}, pools) |
| + b.pending(row, pools, now) |
| + if !b.deferred(row, pools, now.Add(29*time.Second)) || b.deferred(row, pools, now.Add(30*time.Second)) { |
| + t.Fatal("changed evidence did not reset backoff") |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/provision/poolwithdrawal_test.go b/internal/provision/poolwithdrawal_test.go| index 11b02299a..20b6f821b 100644 |
| --- a/internal/provision/poolwithdrawal_test.go |
| +++ b/internal/provision/poolwithdrawal_test.go |
| @@ -206,7 +206,7 @@ func TestPoolWithdrawalPendingBackoffBeforeReads(t *testing.T) { |
| t.Fatalf("reads=%d proofs=%d", before, proofs) |
| } |
| for i := 0; i < 4; i++ { |
| - now = now.Add(time.Minute) |
| + now = now.Add(7 * time.Second) |
| rt.BuildState = fmt.Sprintf(`{"read_at":"%d","version":"v1"}`, i) |
| if err := reg.Upsert(rt); err != nil { |
| t.Fatal(err) |
| @@ -216,7 +216,7 @@ func TestPoolWithdrawalPendingBackoffBeforeReads(t *testing.T) { |
| if s.reads != before || proofs != 1 { |
| t.Fatalf("backoff read S3: reads=%d -> %d proofs=%d", before, s.reads, proofs) |
| } |
| - now = now.Add(time.Minute) |
| + now = now.Add(2 * time.Second) |
| cfg.withdrawPools(ctx, s, reg, false) |
| if proofs != 2 { |
| t.Fatalf("expiry proofs=%d", proofs) |