edge: fall back from a lost bound box only when never reached (#511)
CI failed TestTwoBoxFleetEndToEnd at 3654fc7d with 502 "unexpected EOF". Cause: round 1's rewrite of its step (b) asserted the fall-over to the survivor right after box-b's agent stopped. M4's fall-over applies once the hub has read box-b's wire end; before that, fleetChannelFor still saw box-b serving, the request's HEADERS were written onto the dying connection, and the box may have seen them, so R22 rightly answers 502 and sends it nowhere else. A race in the test, not a lost fallback. The test now waits for the hub to read box-b's wire end (SawWireEnd) before asserting the fall-over. fleetProxy also closes the narrower window where the bound channel is lost between the pick and the send: for an unlabelled row, a request the attempt proves never reached the lost box (reachNotWritten, body unread) goes to another connected box of the fleet. Anything that may have reached the box never falls back.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/edge/edge.go+32 −6internal/edge/fleet_e2e_test.go+18 −0internal/edge/tunnel_export_test.go+9 −0
Intent
<task-notification> <task-id>a41c5fc02dcfea0c5</task-id> <tool-use-id>toolu_01Dg4UKAKcXKj2gzZc51go4Y</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/a41c5fc02dcfea0c5.output</output-file> <status>completed</status> <summary>Agent "Fix drill-3 hub routing gaps" 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.** R25, R26, R27, the CI census failure, and Fable M4 and M5 are all done, each with a witness through the real hub and box wiring. Tip 3654fc7d, not pushed. - **Commits:** - 08da3af2: the box refuses a stream with REFUSED_STREAM. - 7428bf80: the hub retries a box's refusal on a survivor whatever the method; live copies and the handoff come from one snapshot (R25); draining channels are skipped (M4); comment (M5). - d5198460: R27, the box refuses requests no app has read while it stops. - 3654fc7d: census re-proposal, and Reload re-pinned for `TestGroupNameClassCensus`. - **Tests:** targeted edge, tunnelclient, host, activator and region HA tests pass, as do grouproll, the three cross-compiles and the census tests (`make census-card` clean, seal 52cb3166). The new witnesses passed 20 runs at `-cpu=1,2,8` and a race run. Each fix is shown by a mutation that breaks its witness. - **Concerns:** - The REFUSED_STREAM path rests on x/net v0.62 internals: the box reads the stream id by reflection and rewrites one reset frame's code, and the hub matches the GOAWAY abort by its text. Tests pin all three. If the reflection breaks, the box just answers as before. - Request bodies of known length up to 64 KiB are now read whole before the first attempt. A refused request with a larger, partly sent body gets 502 rather than a retry. - R27's app-proxy path can stall up to 13 s before refusing, while the activator hasn't yet noticed an outside stop. - The R27 gap witness uses an activator stand-in that calls the same rule as the real activator. A separate test drives the real activator to that rule. - A refusal ejects the copy for about 2 s. - I fixed a pre-existing flaky test box from round 0 that wrote frames out of order. - Astra's census review is still owed. - **Report:** /home/paulca/amadan.net/carlos/platform/.claude/worktrees/stateless-replicas-4/.superpowers/sdd/2026-11-01-stateless-replicas-4-deploy-and-drill/task-drill3-report.md</result> <usage><subagent_tokens>677264</subagent_tokens><tool_uses>83</tool_uses><duration_ms>3226826</duration_ms></usage> </task-notification>
<task-notification> <task-id>b3a314uge</task-id> <tool-use-id>toolu_01MCvtQZMGo9VhvmWAZJ2dRV</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/b3a314uge.output</output-file> <status>completed</status> <summary>Background command "Astra census review of drill-3 round 1" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>buagyqbf6</task-id> <tool-use-id>toolu_011p7XW1Sj53sAXnrJcrcpYL</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/buagyqbf6.output</output-file> <status>completed</status> <summary>Background command "Astra re-review of drill-3 round 1" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>bjyan9dcv</task-id> <tool-use-id>toolu_01X4Jd9vaJ1kR4wkgpvtu7X7</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bjyan9dcv.output</output-file> <status>completed</status> <summary>Background command "Fable re-review of drill-3 round 1" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>bwo4ei2yn</task-id> <tool-use-id>toolu_01NaSXQJcBPE1aYvJ5uTrZCB</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bwo4ei2yn.output</output-file> <status>completed</status> <summary>Background command "Wait for CI on 3654fc7d" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index 9ca5ebcb7..fdfa6367d 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -4368,15 +4368,41 @@ func fleetProxy(fleet, boxLabel string, authoritative bool, hub *TunnelHub) *htt |
| if name == "" { |
| return nil, errFleetDark |
| } |
| + // replicaRoundTrip sends exactly as transportFor would. The body |
| + // goes through an attempt view, so a request the connection |
| + // refused before writing it keeps its body whole. |
| + var view *replicaAttemptBody |
| + attempt := req |
| + if req.Body != nil && req.Body != http.NoBody { |
| + view = &replicaAttemptBody{src: req.Body} |
| + attempt = req.Clone(req.Context()) |
| + attempt.Body = view |
| + } |
| + resp, reach, err := hub.replicaRoundTrip(name, attempt) |
| + // A request that never reached a bound channel that has ended |
| + // or is draining (#511 R22, R24; review round 2) goes to |
| + // another connected box of the fleet when the row allows a |
| + // fallback (no label): the same rule 2 a dark bound box gets, |
| + // applied the moment the attempt proves the box never saw it, |
| + // before the ping reaps the channel. A request that may have |
| + // reached the box never falls back. |
| + if err != nil && reach == reachNotWritten && !authoritative && (view == nil || !view.read.Load()) && hub.replicaChannelLost(name) { |
| + if next := fleetChannelFor(fleet, boxLabel, authoritative, hub); next != "" && next != name { |
| + name = next |
| + if view != nil { |
| + view = &replicaAttemptBody{src: req.Body} |
| + attempt = req.Clone(req.Context()) |
| + attempt.Body = view |
| + } |
| + resp, reach, err = hub.replicaRoundTrip(name, attempt) |
| + } |
| + } |
| // A request that never reached a channel that has ended or is |
| // draining (a GOAWAY, #511 R24), or that its box refused |
| // unprocessed (R26, R27: the box is shutting down or its app |
| - // is down), answers as a dark fleet does, 503 with Retry-After, |
| - // never the transport's bare 502: the box used to answer such |
| - // requests with its own 503. This row has one box, so nothing |
| - // is retried. replicaRoundTrip sends exactly as transportFor |
| - // would. |
| - resp, reach, err := hub.replicaRoundTrip(name, req) |
| + // is down), with no box left to try, answers as a dark fleet |
| + // does, 503 with Retry-After, never the transport's bare 502: |
| + // the box used to answer such requests with its own 503. |
| if err != nil && (reach == reachRefused || (reach == reachNotWritten && hub.replicaChannelLost(name))) { |
| return nil, errFleetDark |
| } |
diff --git a/internal/edge/fleet_e2e_test.go b/internal/edge/fleet_e2e_test.go| index 44fb8604b..a947e5f2d 100644 |
| --- a/internal/edge/fleet_e2e_test.go |
| +++ b/internal/edge/fleet_e2e_test.go |
| @@ -423,6 +423,11 @@ func TestTwoBoxFleetEndToEnd(t *testing.T) { |
| if got := hub.State(e2eFleet + "/box-b"); got != "connected" { |
| t.Fatalf("(b) hub state for the dead box = %q, want %q (still registered until the ping reaps it)", got, "connected") |
| } |
| + // The fall-over needs the hub to have read box-b's wire end (its closed |
| + // notice). Before that, a request may be written onto the dying |
| + // connection; it may then have reached the box, so it is answered 502 |
| + // and never sent elsewhere (R22). That race made CI fail at 3654fc7d. |
| + waitHubSawEnd(t, hub, e2eFleet+"/box-b") |
| if code, body := e2eGet(t, srv.URL, e2eAppHost); code != http.StatusOK || body != "box-a" { |
| t.Fatalf("(b) a dead-but-registered bound channel: got %d %q, want the survivor's 200 %q", code, body, "box-a") |
| } |
| @@ -599,3 +604,16 @@ func TestTwoBoxFleetEndToEnd(t *testing.T) { |
| } |
| t.Log("(e) dark fleet answered 503 with Retry-After: 2") |
| } |
| + |
| +// waitHubSawEnd waits until the hub has read the end of name's wire, while |
| +// the channel is still registered. |
| +func waitHubSawEnd(t *testing.T, hub *edge.TunnelHub, name string) { |
| + t.Helper() |
| + deadline := time.Now().Add(5 * time.Second) |
| + for !hub.SawWireEnd(name) { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("the hub never read the end of %s's wire", name) |
| + } |
| + time.Sleep(5 * time.Millisecond) |
| + } |
| +} |
diff --git a/internal/edge/tunnel_export_test.go b/internal/edge/tunnel_export_test.go| new file mode 100644 |
| index 000000000..bc845eef4 |
| --- /dev/null |
| +++ b/internal/edge/tunnel_export_test.go |
| @@ -0,0 +1,9 @@ |
| +package edge |
| + |
| +// SawWireEnd reports whether name is still registered and the hub has read |
| +// the end of its wire (or its connection takes no new streams): for the |
| +// external fleet tests. |
| +func (h *TunnelHub) SawWireEnd(name string) bool { |
| + c := h.channel(name) |
| + return c != nil && tunnelConnLost(c) |
| +} |