region HA: held requests fix round 1 (tickets, exits, move lifetime)
Astra task review r1 of held-requests Task 2: - P1 ticket gap: the admission ticket no longer ends before a forward is chosen. forwardHAObserved ends it only once the forward has committed to the remote path, after every ForwardNotInView return, so a Holder or Recovery peer out of view falls back to local service with the ticket still held until begin counts the request. - P1 bounded exits: the whole taken-client release (the relayed answer, the hop's admission cap, an unreachable holder, a holder out of view) runs inside handoverExit. An upgrade keeps its stream: no Connection: close and no deadline after the hijack. - P2 lifetime: a move records the accepted route at BeginMove, and MoveState, the hold re-check and ConfirmMoveHolder apply it only to that host lifetime (host, account, app, incarnation, serving seq). - P2 Done: the activator's ticket callback is once-only itself.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/activator/handover.go+33 −14internal/activator/handover_test.go+82 −1internal/edge/forwardha.go+5 −2internal/edge/handover.go+30 −14internal/edge/handover_test.go+123 −10
Intent
<task-notification> <task-id>a8b92a57abc36621e</task-id> <tool-use-id>toolu_01Ey1jnGnMJfjLmWp91cjfap</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/283be278-5c6a-4a71-a8d6-b3d32c668985/tasks/a8b92a57abc36621e.output</output-file> <status>completed</status> <summary>Agent "Implement Task 2: MoveState holds" 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>Status: **DONE_WITH_CONCERNS**. Option A is built as ruled. The decision is one function, called just before `frontForwardHAIngress` and just before `frontForwardHA`. The new test shows every request kind passes through it exactly once. The rest of the brief is done too: holds, admission tickets, bounds, the bounded exit, both headers and the Task 5 API. Commits (not pushed): `f115ff8f` (code), `52989af9` (census overlay). Tests: 10 new activator and 8 new edge tests pass, along with the neighbouring tests in both packages, the census test, gofmt, vet and the darwin/windows builds. The code came before the tests, so RED evidence is 17 mutations: 16 fail a test, and one survives (the full-duplex call, concern 1). Going green also found and fixed a real coalescing bug. Concerns: 1. Full duplex can't be observed on its own on HTTP/1, because `Connection: close` already stops net/http draining the body. It stays because the spec requires it. 2. No test yet drives the real activator through the real edge. The edge tests use a fake activator, and Task 8's integration test should close this. 3. A slow request that never reaches `begin` (a cache or asset fill) holds its ticket until it finishes, up to the driver's 10 s bound. 4. One decision of mine: door calls, tunnel deliveries and regional names for a moving host are refused at once rather than held, because none can be forwarded. Census: proposal re-run with the whole branch's claims and committed. The cross-family verdict on #280 is still owed. Report: `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/region-ha-held-build/.superpowers/sdd/2026-11-08-region-ha-held-requests/task-2-report.md`</result> <usage><subagent_tokens>298862</subagent_tokens><tool_uses>74</tool_uses><duration_ms>2168915</duration_ms></usage> </task-notification>
diff --git a/internal/activator/handover.go b/internal/activator/handover.go| index c1c1c0cc8..f3e091e77 100644 |
| --- a/internal/activator/handover.go |
| +++ b/internal/activator/handover.go |
| @@ -44,6 +44,7 @@ import ( |
| "context" |
| "errors" |
| "log" |
| + "sync" |
| "time" |
| "github.com/carlosframework/platform/internal/boxchoice" |
| @@ -75,8 +76,11 @@ const ( |
| moveTaken |
| ) |
| -// hostMove is one host's move on this box. |
| +// hostMove is one host's move on this box. It belongs to one accepted |
| +// host lifetime (route, captured at BeginMove): a request for another |
| +// lifetime of the same hostname never sees it (sameMoveLifetime). |
| type hostMove struct { |
| + route registry.Route |
| phase movePhase |
| holder string |
| gen int64 |
| @@ -133,7 +137,7 @@ func (a *Service) MoveState(rt registry.Route, ask edge.MoveAsk) edge.MoveDecisi |
| a.mu.Lock() |
| defer a.mu.Unlock() |
| m := a.moves.hosts[rt.Host] |
| - if m == nil { |
| + if m == nil || !sameMoveLifetime(m.route, rt) { |
| return a.moveNoneLocked(rt) |
| } |
| if d, ok := a.moveDecideLocked(rt, m, ask); ok { |
| @@ -166,22 +170,37 @@ func (a *Service) moveNoneLocked(rt registry.Route) edge.MoveDecision { |
| } |
| a.moves.tickets[rt.Host]++ |
| host := rt.Host |
| + var once sync.Once |
| + // Each ticket ends exactly once, however often Done is called: a |
| + // repeated call must never end another request's ticket. |
| return edge.MoveDecision{Done: func() { |
| - a.mu.Lock() |
| - defer a.mu.Unlock() |
| - if a.moves.tickets[host]--; a.moves.tickets[host] <= 0 { |
| - delete(a.moves.tickets, host) |
| - } |
| - if a.moves.ticketsDone != nil { |
| - close(a.moves.ticketsDone) |
| - a.moves.ticketsDone = nil |
| - } |
| + once.Do(func() { |
| + a.mu.Lock() |
| + defer a.mu.Unlock() |
| + if a.moves.tickets[host]--; a.moves.tickets[host] <= 0 { |
| + delete(a.moves.tickets, host) |
| + } |
| + if a.moves.ticketsDone != nil { |
| + close(a.moves.ticketsDone) |
| + a.moves.ticketsDone = nil |
| + } |
| + }) |
| }} |
| } |
| +// sameMoveLifetime is whether rt is the accepted host lifetime a move was |
| +// begun for: the same host, account, app, incarnation and serving |
| +// sequence. A replacement incarnation or a reassigned route of the same |
| +// hostname is another lifetime, and the move's holds, refusals, holder and |
| +// cached check are not its. |
| +func sameMoveLifetime(moved, rt registry.Route) bool { |
| + return moved.Host == rt.Host && registry.EffectiveAccount(moved.Account) == registry.EffectiveAccount(rt.Account) && |
| + moved.App == rt.App && moved.Incarnation == rt.Incarnation && moved.ServingSeq == rt.ServingSeq |
| +} |
| + |
| // moveDecideLocked answers every phase but a hold: ok false means hold. |
| func (a *Service) moveDecideLocked(rt registry.Route, m *hostMove, ask edge.MoveAsk) (edge.MoveDecision, bool) { |
| - if m.gone { |
| + if m.gone || !sameMoveLifetime(m.route, rt) { |
| return edge.MoveDecision{Phase: edge.MoveRefused}, true |
| } |
| if a.haLeaseRefusalLocked(rt) != nil { |
| @@ -259,7 +278,7 @@ func (a *Service) BeginMove(host string) error { |
| if a.moves.hosts == nil { |
| a.moves.hosts = make(map[string]*hostMove) |
| } |
| - a.moves.hosts[host] = &hostMove{phase: moveHolding, changed: make(chan struct{})} |
| + a.moves.hosts[host] = &hostMove{route: e.route, phase: moveHolding, changed: make(chan struct{})} |
| return nil |
| } |
| @@ -371,7 +390,7 @@ func (a *Service) ConfirmMoveHolder(ctx context.Context, rt registry.Route, snap |
| a.mu.Lock() |
| for { |
| m := a.moves.hosts[rt.Host] |
| - if m == nil || m.phase != moveTaken || r == nil { |
| + if m == nil || m.phase != moveTaken || r == nil || !sameMoveLifetime(m.route, rt) { |
| a.mu.Unlock() |
| return refused |
| } |
diff --git a/internal/activator/handover_test.go b/internal/activator/handover_test.go| index dd8e2e3ca..86d9ef3e4 100644 |
| --- a/internal/activator/handover_test.go |
| +++ b/internal/activator/handover_test.go |
| @@ -312,7 +312,7 @@ func TestMoveHoldCaps(t *testing.T) { |
| s.a.moves.hosts["other"+string(rune('a'+i))+".example.com"] = h |
| } |
| third := registry.Route{Host: "third.example.com", HA: true} |
| - s.a.moves.hosts[third.Host] = &hostMove{phase: moveHolding, changed: make(chan struct{})} |
| + s.a.moves.hosts[third.Host] = &hostMove{route: third, phase: moveHolding, changed: make(chan struct{})} |
| s.a.mu.Unlock() |
| if d := s.a.MoveState(third, askClient); d.Phase != edge.MoveRefused { |
| t.Fatalf("over the box cap: %+v, want today's answer", d) |
| @@ -533,3 +533,84 @@ func waitUntil(t *testing.T, what string, cond func() bool) { |
| time.Sleep(5 * time.Millisecond) |
| } |
| } |
| + |
| +// Fix round 1 (P2): two tickets; a repeated Done of one ends only it. |
| +func TestMoveTicketDoneEndsOnlyItsOwn(t *testing.T) { |
| + s, _ := haServiceEntry(t, "shop.example.com") |
| + first, second := s.a.MoveState(s.rt, askClient), s.a.MoveState(s.rt, askClient) |
| + if first.Done == nil || second.Done == nil || s.a.MoveTickets(s.rt.Host) != 2 { |
| + t.Fatalf("two tickets: %d", s.a.MoveTickets(s.rt.Host)) |
| + } |
| + if err := s.a.BeginMove(s.rt.Host); err != nil { |
| + t.Fatal(err) |
| + } |
| + first.Done() |
| + first.Done() |
| + first.Done() |
| + if n := s.a.MoveTickets(s.rt.Host); n != 1 { |
| + t.Fatalf("after the first ticket ended three times: %d outstanding, want 1", n) |
| + } |
| + ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond) |
| + defer cancel() |
| + if err := s.a.WaitMoveTickets(ctx, s.rt.Host); !errors.Is(err, context.DeadlineExceeded) { |
| + t.Fatalf("the move went on with the second request uncounted: %v", err) |
| + } |
| + second.Done() |
| + if err := s.a.WaitMoveTickets(t.Context(), s.rt.Host); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// Fix round 1 (P2): a move belongs to the accepted host lifetime it was |
| +// begun for. Another incarnation, or the hostname reassigned to another |
| +// app, never inherits its holds, refusals, holder or cached check. |
| +func TestMoveBelongsToItsLifetime(t *testing.T) { |
| + s, _ := haServiceEntry(t, "shop.example.com") |
| + if err := s.a.BeginMove(s.rt.Host); err != nil { |
| + t.Fatal(err) |
| + } |
| + reincarnated := s.rt |
| + reincarnated.Incarnation++ |
| + reassigned := s.rt |
| + reassigned.App = "other" |
| + others := map[string]registry.Route{"another incarnation": reincarnated, "another app": reassigned} |
| + held := waitAsync(t.Context(), s.a.MoveState(s.rt, askClient)) |
| + for name, rt := range others { |
| + for ask, a := range map[string]edge.MoveAsk{"client": askClient, "hop": askHop, "replayable hop": askReplayable} { |
| + if d := s.a.MoveState(rt, a); d.Phase != edge.MoveNone || d.Done != nil { |
| + t.Fatalf("holding, %s, %s: %+v, want none (and no ticket: this box holds the other lifetime)", name, ask, d) |
| + } |
| + } |
| + } |
| + if err := s.a.SetMoveTaken(s.rt.Host, "b", 9); err != nil { |
| + t.Fatal(err) |
| + } |
| + if d := wantDecision(t, "the moved lifetime's hold", held, edge.MoveTaken); d.Holder != "b" { |
| + t.Fatalf("%+v", d) |
| + } |
| + putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(time.Hour))) |
| + reads := len(s.fx.Requests()) |
| + for name, rt := range others { |
| + if d := s.a.MoveState(rt, askClient); d.Phase != edge.MoveNone { |
| + t.Fatalf("taken, %s: %+v, want none", name, d) |
| + } |
| + if d := s.a.ConfirmMoveHolder(t.Context(), rt, nil); d.Phase != edge.MoveRefused { |
| + t.Fatalf("taken, %s: the check answered %+v, want refused", name, d) |
| + } |
| + } |
| + if got := len(s.fx.Requests()) - reads; got != 0 { |
| + t.Fatalf("another lifetime's check read the store %d times", got) |
| + } |
| + if d := s.a.MoveState(s.rt, askClient); d.Phase != edge.MoveTaken || d.Holder != "b" { |
| + t.Fatalf("the moved lifetime: %+v", d) |
| + } |
| + if d := s.a.ConfirmMoveHolder(t.Context(), s.rt, nil); d.Phase != edge.MoveTaken || d.Holder != "b" { |
| + t.Fatalf("the moved lifetime's check: %+v", d) |
| + } |
| + // The cached check is the moved lifetime's only. |
| + for name, rt := range others { |
| + if d := s.a.ConfirmMoveHolder(t.Context(), rt, nil); d.Phase != edge.MoveRefused { |
| + t.Fatalf("%s reused the cached check: %+v", name, d) |
| + } |
| + } |
| +} |
diff --git a/internal/edge/forwardha.go b/internal/edge/forwardha.go| index f0171cbb2..f0441228e 100644 |
| --- a/internal/edge/forwardha.go |
| +++ b/internal/edge/forwardha.go |
| @@ -231,8 +231,6 @@ func (e *Edge) frontForwardHA(w http.ResponseWriter, r *http.Request, host strin |
| if d.Ref == "" { |
| return false |
| } |
| - // Forwarded: the holder counts it, not this box (handover.go). |
| - ReleaseMoveTicket(r) |
| res := e.forwardHAAndNote(w, r, host, up.route, fw, d) |
| if res.Outcome == ForwardNotInView && !e.localPoolEligible(up.route) { |
| HAUnavailableResponse(w, r) |
| @@ -295,6 +293,11 @@ func (e *Edge) forwardHAObserved(w http.ResponseWriter, r *http.Request, host st |
| return res |
| } |
| res.Outcome = ForwardDone |
| + // Committed to the remote path: every return from here on answers the |
| + // request there or refuses it, and none serves it here. Only now does |
| + // its admission ticket end (handover.go); a ForwardNotInView above falls |
| + // back to local service with the ticket still held. |
| + ReleaseMoveTicket(r) |
| stopObservation := func() {} |
| if d.Kind == Holder && note != nil { |
| silent := res |
diff --git a/internal/edge/handover.go b/internal/edge/handover.go| index a69f5599d..0f09c9eed 100644 |
| --- a/internal/edge/handover.go |
| +++ b/internal/edge/handover.go |
| @@ -111,8 +111,9 @@ type MoveDecision struct { |
| // (ForwardDecision.Gen). |
| ForwardGen uint64 |
| // Done ends an admission ticket (MoveNone, for a host this box holds): |
| - // the request finished, was forwarded or was counted by begin. nil when |
| - // there is none. Safe to call more than once. |
| + // the request finished, was committed to a forward or was counted by |
| + // begin. nil when there is none. An implementation must make it |
| + // idempotent: a second call ends nothing. |
| Done func() |
| // Wait (MoveHolding only) waits until the host leaves holding or |
| // releasing, the hold's bound passes, or ctx ends, and answers again; |
| @@ -239,16 +240,21 @@ func (e *Edge) moveGate(w http.ResponseWriter, r *http.Request, host string, rt |
| } |
| // A client's request: the ordinary front forward to the confirmed |
| // holder. Its first attempt; the holder runs the gate, accounting |
| - // and recording once, and the unread body streams as today. |
| + // and recording once, and the unread body streams as today. The |
| + // whole forward runs inside the bounded exit, so its relayed answer |
| + // and every failure (the hop's admission cap, an unreachable |
| + // holder, a holder out of view) end the same way. |
| fw, ok := e.act.(HAForwarder) |
| if !ok || d.Holder == "" { |
| handoverExit(w, r, answerHolderUnreachable) |
| return r, true |
| } |
| - res := e.forwardHAAndNote(w, r, host, rt, fw, ForwardDecision{Ref: d.Holder, Kind: Holder, Gen: d.ForwardGen}) |
| - if res.Outcome == ForwardNotInView { |
| - handoverExit(w, r, answerHolderUnreachable) |
| - } |
| + handoverExit(w, r, func(w http.ResponseWriter, r *http.Request) { |
| + res := e.forwardHAAndNote(w, r, host, rt, fw, ForwardDecision{Ref: d.Holder, Kind: Holder, Gen: d.ForwardGen}) |
| + if res.Outcome == ForwardNotInView { |
| + answerHolderUnreachable(w, r) |
| + } |
| + }) |
| default: |
| // MoveRefused, and anything unexpected: today's answer. |
| handoverExit(w, r, answerHolderUnreachable) |
| @@ -257,19 +263,29 @@ func (e *Edge) moveGate(w http.ResponseWriter, r *http.Request, host string, rt |
| } |
| // handoverExit is the one response path of every handover exit (R3.3): a |
| -// handover answer, a refusal, a cap, a timeout, a lost lease and a failed |
| -// move. It enables full duplex before writing, so net/http never drains an |
| -// unread body before the answer; asks for the connection to close after |
| -// it (HTTP/1); flushes; and then puts the read deadline in the past, so |
| -// net/http's drain of the unread body after the handler fails at once |
| -// instead of waiting on a slow client. No exit waits for a body. |
| +// release to the confirmed holder, a handover answer, a refusal, a cap, a |
| +// timeout, a lost lease and a failed move. It enables full duplex before |
| +// answer writes anything, so net/http never drains an unread body before |
| +// the answer and a streaming upload can still be read while it is |
| +// relayed; asks for the connection to close after it (HTTP/1); then |
| +// flushes and puts the read deadline in the past, so net/http's drain of |
| +// the unread body after the handler fails at once instead of waiting on a |
| +// slow client. No exit waits for a body. |
| +// |
| +// An upgrade request is the exception to the close and the deadline: its |
| +// connection becomes the upgraded stream (the proxy hijacks it), and it |
| +// has no body to drain. |
| func handoverExit(w http.ResponseWriter, r *http.Request, answer func(http.ResponseWriter, *http.Request)) { |
| rc := http.NewResponseController(w) |
| _ = rc.EnableFullDuplex() |
| - if r.ProtoMajor == 1 { |
| + upgrade := isUpgradeRequest(r) |
| + if r.ProtoMajor == 1 && !upgrade { |
| w.Header().Set("Connection", "close") |
| } |
| answer(w, r) |
| + if upgrade { |
| + return |
| + } |
| _ = rc.Flush() |
| _ = rc.SetReadDeadline(time.Now()) |
| } |
diff --git a/internal/edge/handover_test.go b/internal/edge/handover_test.go| index d08669c6a..37d2a1b09 100644 |
| --- a/internal/edge/handover_test.go |
| +++ b/internal/edge/handover_test.go |
| @@ -469,26 +469,56 @@ func (x *hoScene) CloseIdle() { |
| // R3.3: the bounded exit on a real HTTP/1.1 connection whose body never |
| // finishes. The answer arrives, the connection closes and the handler |
| -// returns, though not a byte more of the body is sent. |
| +// returns, though not a byte more of the body is sent. Fix round 1: the |
| +// whole taken-client release is inside the exit too, so its failures (the |
| +// hop's admission cap, an unreachable holder) and its relayed answer end |
| +// the same way. |
| func TestHandoverExitOnUnfinishedBodies(t *testing.T) { |
| for _, c := range []struct { |
| name, framing, sent string |
| - hop bool |
| + hop, taken bool |
| + setup func(t *testing.T, x *hoScene) |
| + status int |
| + retry string |
| + appSeen bool |
| }{ |
| - {"chunked, unfinished", "Transfer-Encoding: chunked", "5\r\nhello\r\n", false}, |
| - {"fixed length, stalled", "Content-Length: 100000", "0123456789", false}, |
| - {"fixed length, small, stalled", "Content-Length: 1000", "0123456789", false}, |
| - {"replayable hop, chunked, unfinished", "Transfer-Encoding: chunked", "5\r\nhello\r\n", true}, |
| + {name: "chunked, unfinished", framing: "Transfer-Encoding: chunked", sent: "5\r\nhello\r\n", status: 503, retry: "10"}, |
| + {name: "fixed length, stalled", framing: "Content-Length: 100000", sent: "0123456789", status: 503, retry: "10"}, |
| + {name: "fixed length, small, stalled", framing: "Content-Length: 1000", sent: "0123456789", status: 503, retry: "10"}, |
| + {name: "replayable hop, chunked, unfinished", framing: "Transfer-Encoding: chunked", sent: "5\r\nhello\r\n", hop: true, taken: true, status: 503, retry: "10"}, |
| + {name: "taken client, hop at its cap, chunked", framing: "Transfer-Encoding: chunked", sent: "5\r\nhello\r\n", taken: true, status: 503, retry: "2", |
| + setup: func(t *testing.T, _ *hoScene) { |
| + old := backhaulMaxInflight.Load() |
| + backhaulMaxInflight.Store(0) |
| + t.Cleanup(func() { backhaulMaxInflight.Store(old) }) |
| + }}, |
| + {name: "taken client, hop at its cap, small fixed length", framing: "Content-Length: 1000", sent: "0123456789", taken: true, status: 503, retry: "2", |
| + setup: func(t *testing.T, _ *hoScene) { |
| + old := backhaulMaxInflight.Load() |
| + backhaulMaxInflight.Store(0) |
| + t.Cleanup(func() { backhaulMaxInflight.Store(old) }) |
| + }}, |
| + {name: "taken client, holder unreachable, chunked", framing: "Transfer-Encoding: chunked", sent: "5\r\nhello\r\n", taken: true, status: 503, retry: "10", |
| + setup: func(t *testing.T, x *hoScene) { hoUnreachable(t, x) }}, |
| + {name: "taken client, holder unreachable, small fixed length", framing: "Content-Length: 1000", sent: "0123456789", taken: true, status: 503, retry: "10", |
| + setup: func(t *testing.T, x *hoScene) { hoUnreachable(t, x) }}, |
| + // A relayed answer: the holder needs the body it was sent, so these |
| + // send it whole; the exit still closes the connection after it. |
| + {name: "taken client, relayed by the holder, chunked", framing: "Transfer-Encoding: chunked", sent: "5\r\nhello\r\n0\r\n\r\n", taken: true, status: 200, appSeen: true}, |
| + {name: "taken client, relayed by the holder, fixed length", framing: "Content-Length: 10", sent: "0123456789", taken: true, status: 200, appSeen: true}, |
| } { |
| t.Run(c.name, func(t *testing.T) { |
| x := newHoScene(t) |
| x.mv.mu.Lock() |
| x.mv.phase = MoveRefused |
| - if c.hop { |
| + if c.taken { |
| x.mv.phase, x.mv.holder, x.mv.gen = MoveTaken, "betty", 9 |
| x.mv.confirm = MoveDecision{Phase: MoveTaken, Holder: "betty", Gen: 9} |
| } |
| x.mv.mu.Unlock() |
| + if c.setup != nil { |
| + c.setup(t, x) |
| + } |
| conn, err := tls.Dial("tcp", x.a.tlsAddr, &tls.Config{ServerName: fwHost, RootCAs: x.ca.pool, MinVersion: tls.VersionTLS12}) |
| if err != nil { |
| t.Fatal(err) |
| @@ -510,23 +540,106 @@ func TestHandoverExitOnUnfinishedBodies(t *testing.T) { |
| if err != nil { |
| t.Fatalf("answer body: %v", err) |
| } |
| - if resp.StatusCode != 503 || !resp.Close || resp.Header.Get("Retry-After") != "10" { |
| - t.Fatalf("%d %v %q", resp.StatusCode, resp.Header, b) |
| + if resp.StatusCode != c.status || !resp.Close || resp.Header.Get("Retry-After") != c.retry { |
| + t.Fatalf("%d %v %q, want %d, Retry-After %q, connection closed", resp.StatusCode, resp.Header, b, c.status, c.retry) |
| } |
| if c.hop && resp.Header.Get(HandoverHeader) != "holder=betty; gen=9" { |
| t.Fatalf("hop: %v", resp.Header) |
| } |
| + if c.appSeen && string(b) != "betty|"+fwHost+"|/upload" { |
| + t.Fatalf("relayed %q, want betty's answer", b) |
| + } |
| if _, err := br.ReadByte(); err != io.EOF { |
| t.Fatalf("the connection stayed open after the answer: %v", err) |
| } |
| laWaitFor(t, "annie's handler to return", func() bool { return x.a.served.Load() == x.a.calls.Load() }) |
| - if x.aApp.n.Load() != 0 || x.b.calls.Load() != 0 { |
| + if x.aApp.n.Load() != 0 || (!c.appSeen && x.bApp.n.Load() != 0) { |
| t.Fatal("the refused upload went on") |
| } |
| }) |
| } |
| } |
| +// hoUnreachable makes every forward dial fail at once, as a refused |
| +// connection to a box that is gone does. |
| +func hoUnreachable(t *testing.T, x *hoScene) { |
| + t.Helper() |
| + x.CloseIdle() |
| + old := forwardDial |
| + forwardDial = func(context.Context, string, string) (net.Conn, error) { |
| + return nil, fmt.Errorf("connection refused") |
| + } |
| + t.Cleanup(func() { forwardDial = old }) |
| +} |
| + |
| +// An upgrade released from taken still switches protocols through the |
| +// exit: no Connection: close, no read deadline on the upgraded stream. |
| +func TestMoveTakenUpgradeStillSwitches(t *testing.T) { |
| + x := newHoScene(t) |
| + x.mv.mu.Lock() |
| + x.mv.phase, x.mv.holder, x.mv.gen = MoveTaken, "betty", 9 |
| + x.mv.mu.Unlock() |
| + raw, err := net.DialTimeout("tcp", x.a.tlsAddr, 10*time.Second) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + c := tls.Client(raw, &tls.Config{ServerName: fwHost, RootCAs: x.ca.pool, MinVersion: tls.VersionTLS12}) |
| + defer c.Close() |
| + c.SetDeadline(time.Now().Add(10 * time.Second)) |
| + fmt.Fprintf(c, "GET /socket HTTP/1.1\r\nHost: %s\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n", fwHost) |
| + br := bufio.NewReader(c) |
| + resp, err := http.ReadResponse(br, nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if resp.StatusCode != http.StatusSwitchingProtocols { |
| + t.Fatalf("handshake = %d %v, want 101", resp.StatusCode, resp.Header) |
| + } |
| + for _, v := range resp.Header.Values("Connection") { |
| + if strings.Contains(strings.ToLower(v), "close") { |
| + t.Fatalf("the 101 carries Connection: %q; an upgrade must not be told to close", resp.Header.Values("Connection")) |
| + } |
| + } |
| + // Past the exit's return: nothing closed the upgraded stream. |
| + time.Sleep(50 * time.Millisecond) |
| + fmt.Fprint(c, "ping\n") |
| + if line, err := br.ReadString('\n'); err != nil || line != "betty:ping\n" { |
| + t.Fatalf("echo = %q %v, want betty's", line, err) |
| + } |
| +} |
| + |
| +// Fix round 1 (P1): a Holder or Recovery peer that is not in view falls |
| +// back to local service, and the request keeps its ticket until it is |
| +// served here: it is never uncounted across the fallback. |
| +func TestMoveTicketKeptOnLocalFallback(t *testing.T) { |
| + for _, kind := range []ForwardKind{Holder, Recovery} { |
| + t.Run(fmt.Sprint(kind), func(t *testing.T) { |
| + x := newHoScene(t) |
| + x.mv.target, x.mv.kind = "nobody", kind |
| + var atLocal atomic.Int64 |
| + atLocal.Store(-1) |
| + old := movePauseHook |
| + movePauseHook = func(point, host string) { |
| + if point == "local" { |
| + atLocal.Store(x.mv.ended.Load()) |
| + } |
| + } |
| + t.Cleanup(func() { movePauseHook = old }) |
| + resp, body := x.fetch(t, fwHost, "/fallback") |
| + if resp.StatusCode != 200 || !strings.HasPrefix(body, "annie|") { |
| + t.Fatalf("%d %q, want annie's local answer", resp.StatusCode, body) |
| + } |
| + laWaitFor(t, "annie's handler", func() bool { return x.a.served.Load() == x.a.calls.Load() }) |
| + if atLocal.Load() != 0 { |
| + t.Fatalf("the ticket ended before the local fallback (%d ended)", atLocal.Load()) |
| + } |
| + if x.mv.issued.Load() != 1 || x.mv.ended.Load() != 1 { |
| + t.Fatalf("issued %d ended %d, want 1 and 1", x.mv.issued.Load(), x.mv.ended.Load()) |
| + } |
| + }) |
| + } |
| +} |
| + |
| // With no move, every pre-existing 503 path answers byte for byte as it |
| // does without the handover decision at all. |
| func TestMoveNoneLeavesEvery503Unchanged(t *testing.T) { |