host: link sim allows a GET the visitor's client sent twice
CI seed 155 at 9e514dda failed "request 155-7 reached the apps 2 times". The hub sends a request down the link once (ClientConn never retries); a replaced connection cut the answer off after its head, the hub's reverse proxy aborted with nothing written, and Go's client sent the GET again on a new connection, as it may for an idempotent method. Under four-core load, 13 GETs took two attempts, each with the hub's first run aborted after WriteHeader. The oracle now counts the client's attempts (httptrace GotConn): a GET or HEAD may reach the apps once per attempt; any other method and every WebSocket open still fail on a second arrival. TestLinkSimHomeClientRetries holds that rule.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+117
−7
internal/host/link_sim_test.go+117 −7
CI passed — run details
diff --git a/internal/host/link_sim_test.go b/internal/host/link_sim_test.go| index f6a75599f..99ba672de 100644 |
| --- a/internal/host/link_sim_test.go |
| +++ b/internal/host/link_sim_test.go |
| @@ -24,6 +24,17 @@ package host |
| // was published to at some moment since it was sent (the home's read |
| // happens inside that window). A late arrival after any other answer |
| // fails; |
| +// - a request reaches the apps at most as many times as the visitor's |
| +// client sent it. The hub never sends a request down a link twice |
| +// (ClientConn.RoundTrip does not retry), but when a replaced |
| +// connection cuts an answer off after its head and before its body, |
| +// the hub's reverse proxy can only abort, closing the visitor's |
| +// connection with nothing written; Go's client then sends a GET or |
| +// HEAD again on a new connection if the old one was reused, as RFC |
| +// 9110 §9.2.2 lets any client do with an idempotent method. So a GET |
| +// sent twice may reach its app twice (CI seed 155 at 9e514dda). Any |
| +// other method, and every WebSocket open (one raw connection here), |
| +// counts as sent once: a second arrival fails (linkSimSends); |
| // - every stream opened for a (name, account, app) that is no longer |
| // published ends within homeSimBound of the change that took it away |
| // (the link loop replaces the connection on conn_gen; Paul's choice 3), |
| @@ -51,10 +62,12 @@ import ( |
| "math/rand" |
| "net" |
| "net/http" |
| + "net/http/httptrace" |
| "net/url" |
| "os" |
| "strconv" |
| "strings" |
| + "sync/atomic" |
| "testing" |
| "time" |
| @@ -156,6 +169,8 @@ type homeSimStats struct { |
| served, refused, refusedPublished, streams, revokedEnded, ticks, retargets int |
| // late: requests answered by no app that reached one afterwards. |
| late int |
| + // resent: GETs the visitor's client sent more than once. |
| + resent int |
| // unreloaded: ticks that left the home edge unreloaded; revokedUnreloaded: |
| // revoked streams those ticks checked. |
| unreloaded, revokedUnreloaded int |
| @@ -178,6 +193,9 @@ type homeSim struct { |
| nextReq int |
| // pending: requests answered by no app, which may still reach one. |
| pending []*homeSimReq |
| + // sends: how many times a request may reach the apps (linkSimSends), |
| + // by tag; a tag not here was sent once. |
| + sends map[string]int |
| } |
| // linkSimReqHeader tags every request the home-side simulation sends, so |
| @@ -185,6 +203,46 @@ type homeSim struct { |
| // pass it through like any other request header. |
| const linkSimReqHeader = "X-Linksim-Req" |
| +// linkSimSends is how many times one request may reach the apps, given |
| +// the method and the visitor client's attempts at it (one GotConn each). |
| +// A GET or HEAD may be sent again by the client: when a replaced link |
| +// connection cuts an answer off between its head and its body, the hub's |
| +// reverse proxy aborts (http.ErrAbortHandler) with the head still |
| +// unwritten, and Go's Transport, which got nothing on a reused |
| +// connection, retries an idempotent request on a new one |
| +// (persistConn.shouldRetryRequest). Every other method counts as sent |
| +// once, whatever the attempts: the client does not retry it, and the hub |
| +// sends each request down once, so a second arrival is a duplicate the |
| +// platform made. |
| +func linkSimSends(method string, attempts int) int { |
| + if (method == http.MethodGet || method == http.MethodHead) && attempts > 1 { |
| + return attempts |
| + } |
| + return 1 |
| +} |
| + |
| +// countAttempts traces req so that attempts counts the visitor client's |
| +// attempts at it. |
| +func countAttempts(req *http.Request, attempts *atomic.Int32) *http.Request { |
| + return req.WithContext(httptrace.WithClientTrace(req.Context(), &httptrace.ClientTrace{ |
| + GotConn: func(httptrace.GotConnInfo) { attempts.Add(1) }, |
| + })) |
| +} |
| + |
| +// sent records that the request tagged id, with method, took attempts. |
| +func (s *homeSim) sent(id, method string, attempts int) { |
| + n := linkSimSends(method, attempts) |
| + if n == 1 { |
| + return |
| + } |
| + if s.sends == nil { |
| + s.sends = map[string]int{} |
| + } |
| + s.sends[id] = n |
| + s.stats.resent++ |
| + s.logf("resent: %s %s took %d attempts (the hub closed a reused connection with no answer)", method, id, attempts) |
| +} |
| + |
| // homeSimReq is a request (or open) whose answer came from no app. allowed |
| // is every target its name was published to from the moment it was sent: |
| // the home's per-request read, if the request gets that far, falls in that |
| @@ -222,7 +280,8 @@ func (s *homeSim) tag() string { |
| } |
| // reached is the one target whose app saw the request tagged id, "" for |
| -// none. Seen more than once, or by more than one app, is a failure. |
| +// none. Seen more times than it was sent (linkSimSends), or by more than |
| +// one app, is a failure. |
| func (s *homeSim) reached(id, what string) string { |
| got, n := "", 0 |
| for _, tg := range s.targets { |
| @@ -237,8 +296,9 @@ func (s *homeSim) reached(id, what string) string { |
| got = tg.key |
| } |
| } |
| - if n > 1 { |
| - s.failf("%s %s reached the apps %d times", what, id, n) |
| + sends := max(s.sends[id], 1) |
| + if n > sends { |
| + s.failf("%s %s reached the apps %d times, sent %d", what, id, n, sends) |
| } |
| return got |
| } |
| @@ -354,7 +414,9 @@ func (s *homeSim) request(name string) { |
| } |
| req.Host = linkPublicHost(name) |
| req.Header.Set(linkSimReqHeader, id) |
| - resp, err := linkNoRedirect.Do(req) |
| + var attempts atomic.Int32 |
| + resp, err := linkNoRedirect.Do(countAttempts(req, &attempts)) |
| + s.sent(id, req.Method, int(attempts.Load())) |
| status := 0 |
| if err == nil { |
| io.Copy(io.Discard, resp.Body) |
| @@ -380,7 +442,9 @@ func (s *homeSim) openSSE(name string) { |
| // hang to the package timeout. Once open, cancel is the stream's close. |
| ctx, cancel := context.WithCancel(context.Background()) |
| timer := time.AfterFunc(homeSimOpenBound, cancel) |
| - resp, err := http.DefaultClient.Do(req.WithContext(ctx)) |
| + var attempts atomic.Int32 |
| + resp, err := http.DefaultClient.Do(countAttempts(req.WithContext(ctx), &attempts)) |
| + s.sent(id, req.Method, int(attempts.Load())) |
| if err != nil { |
| stopped := timer.Stop() |
| cancel() |
| @@ -691,9 +755,9 @@ func TestLinkSimHomeSide(t *testing.T) { |
| } |
| s.cleanup() |
| } |
| - t.Logf("%d seeds, up to %d steps each, in %s: %d requests and opens served, %d refused (%d of them published; %d reached an app after their answer), %d streams opened, %d revoked streams ended at %d ticks (%d transient; %d at %d ticks with no reload), %d retargets", |
| + t.Logf("%d seeds, up to %d steps each, in %s: %d requests and opens served, %d refused (%d of them published; %d reached an app after their answer), %d GETs sent again by the client, %d streams opened, %d revoked streams ended at %d ticks (%d transient; %d at %d ticks with no reload), %d retargets", |
| len(seeds), maxSteps, time.Since(start).Round(time.Millisecond), |
| - stats.served, stats.refused, stats.refusedPublished, stats.late, stats.streams, stats.revokedEnded, stats.ticks, stats.transient, |
| + stats.served, stats.refused, stats.refusedPublished, stats.late, stats.resent, stats.streams, stats.revokedEnded, stats.ticks, stats.transient, |
| stats.revokedUnreloaded, stats.unreloaded, stats.retargets) |
| if !one && len(seeds) >= linkSimDefaultSeeds { |
| // A full run that reached none of these proves nothing. |
| @@ -765,6 +829,52 @@ func TestLinkSimHomeLateArrivals(t *testing.T) { |
| } |
| } |
| +// TestLinkSimHomeClientRetries holds the duplicate check to what it allows. |
| +// CI seed 155 at 9e514dda failed "request 155-7 reached the apps 2 times": |
| +// a replaced connection cut the answer off after its head, the hub's |
| +// reverse proxy aborted with nothing written, and the visitor's client |
| +// sent the GET again on a new connection. Under four-core contention |
| +// locally, 13 GETs took two attempts (the first on a reused connection; |
| +// the hub's handler ran twice and aborted the first after WriteHeader with |
| +// no body byte written). A GET or HEAD may reach its app once per attempt; |
| +// any other method, and a WebSocket open, once in all. |
| +func TestLinkSimHomeClientRetries(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + method string |
| + attempts int |
| + arrivals int |
| + wantFails bool |
| + }{ |
| + {"GET sent twice, arrived twice", http.MethodGet, 2, 2, false}, |
| + {"HEAD sent twice, arrived twice", http.MethodHead, 2, 2, false}, |
| + {"GET sent twice, arrived once", http.MethodGet, 2, 1, false}, |
| + {"GET sent once, arrived twice", http.MethodGet, 1, 2, true}, |
| + {"GET sent twice, arrived three times", http.MethodGet, 2, 3, true}, |
| + {"POST in two attempts, arrived twice", http.MethodPost, 2, 2, true}, |
| + {"PUT in two attempts, arrived twice", http.MethodPut, 2, 2, true}, |
| + {"POST sent once, arrived once", http.MethodPost, 1, 1, false}, |
| + {"WebSocket open, arrived twice", "", 1, 2, true}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + shop := &linkTestApp{name: "shop"} |
| + s := &homeSim{t: t, stats: &homeSimStats{}, pub: map[string]string{"one": "shop"}, |
| + targets: []homeSimTarget{{key: "shop", test: shop}}, seed: time.Now().UnixNano()} |
| + id := s.tag() |
| + if tc.method != "" { |
| + s.sent(id, tc.method, tc.attempts) |
| + } |
| + for range tc.arrivals { |
| + shop.seen = append(shop.seen, linkSeen{SimReq: id}) |
| + } |
| + s.admitted(id, "one", s.reached(id, "a request"), "a request", http.StatusOK) |
| + if got := len(s.fails) != 0; got != tc.wantFails { |
| + t.Fatalf("fails = %q, want failing %v", s.fails, tc.wantFails) |
| + } |
| + }) |
| + } |
| +} |
| + |
| // TestLinkRepublishSameTargetBetweenTicks is the transient revocation the |
| // simulation's oracle records: a stream open on shop under demo, demo |
| // unpublished and published to shop again before the tick. The same |