region HA: bound the client cleanup after an early closing answer
Task 3 fix round 3 (Astra r3: P2, early-answer cleanup unbounded). net/http, once a handler returns, aborts a pending body read by resetting the read deadline to none, then drains the body with no bound. So after an early closing answer, a client withholding the rest of a fixed-length body kept the first box's connection open indefinitely. - closeAfterEarlyAnswer now also sets the client connection's read deadline 1 s ahead. - finishEarlyAnswer, after the relay and before the handler returns, flushes the answer and reads and discards the client's body itself until it ends or that deadline passes. The transport's pending read is interrupted, net/http's drain then ends at once, and the connection closes within about 1 s of the answer. On the in-region forward and the backhaul. - Witness TestEarlyAnswerClosesAStalledClient: 10 bytes of a Content-Length: 100000 body, then nothing; the full 503 with Retry-After: 10 arrives and EOF follows within about 1 s, in region and across regions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+76
−1
internal/edge/backhaul_holder.go+2 −1internal/edge/forwardha.go+1 −0internal/edge/replay.go+30 −0internal/edge/replay_test.go+43 −0
diff --git a/internal/edge/backhaul_holder.go b/internal/edge/backhaul_holder.go| index cb43518ae..1b4d63ca5 100644 |
| --- a/internal/edge/backhaul_holder.go |
| +++ b/internal/edge/backhaul_holder.go |
| @@ -287,14 +287,15 @@ func (e *Edge) backhaulAttempt(w http.ResponseWriter, r *http.Request, host, dia |
| // and closes before it has all gone (a draining box's at-once |
| // refusal) also closes the client connection (closeAfterEarlyAnswer). |
| var onResponse func(*http.Response) |
| + var streamed *streamedBody |
| if whole == nil { |
| - var streamed *streamedBody |
| r, streamed = watchBody(r) |
| if streamed != nil { |
| onResponse = func(resp *http.Response) { closeAfterEarlyAnswer(w, r, streamed, resp) } |
| } |
| } |
| e.hopTo(w, r, host, dial, box, key, "backhaul", tr, onError, onResponse) |
| + finishEarlyAnswer(w, streamed) |
| // The refused hop releases its capacity slot before the holder starts a |
| // response that may stream or remain upgraded indefinitely. |
| if nextRef != "" { |
diff --git a/internal/edge/forwardha.go b/internal/edge/forwardha.go| index 4b84a7473..201d84bfe 100644 |
| --- a/internal/edge/forwardha.go |
| +++ b/internal/edge/forwardha.go |
| @@ -483,6 +483,7 @@ func (e *Edge) forwardHAObserved(w http.ResponseWriter, r *http.Request, host st |
| return errHandoverFollow |
| }} |
| e.hopTo(w, r, host, dial, ref, key, "forward", tr, onError, onResponse) |
| + finishEarlyAnswer(w, streamed) |
| return res |
| } |
diff --git a/internal/edge/replay.go b/internal/edge/replay.go| index 4dee7cab0..13f36a6a7 100644 |
| --- a/internal/edge/replay.go |
| +++ b/internal/edge/replay.go |
| @@ -23,6 +23,7 @@ import ( |
| "io" |
| "net/http" |
| "sync/atomic" |
| + "time" |
| ) |
| // replayBodyLimit is the largest body a first box copies whole (§25.5's |
| @@ -115,6 +116,8 @@ type streamedBody struct { |
| want int64 // Content-Length, -1 when unknown |
| n int64 // read so far; only the reading goroutine touches it |
| done atomic.Bool |
| + // closing: closeAfterEarlyAnswer asked the connection to close. |
| + closing atomic.Bool |
| } |
| func (b *streamedBody) Read(p []byte) (int, error) { |
| @@ -146,9 +149,36 @@ func watchBody(r *http.Request) (*http.Request, *streamedBody) { |
| // under the transport still sending it upstream (which cut the relayed |
| // answer short). A body that did all go upstream leaves the connection |
| // as it is. |
| +// |
| +// The cleanup is bounded too: the client connection's read deadline goes |
| +// exitLingerBound ahead. The answer is relayed first; then a client still |
| +// withholding its body interrupts, at that deadline, the transport's |
| +// pending read of it (which net/http's own close of the body would wait |
| +// behind), and the connection closes. A client that keeps sending only |
| +// lingers until the deadline. finishEarlyAnswer, after the relay, makes |
| +// that bound hold. |
| func closeAfterEarlyAnswer(w http.ResponseWriter, r *http.Request, body *streamedBody, resp *http.Response) { |
| if body == nil || body.done.Load() || !resp.Close || r.ProtoMajor != 1 || resp.StatusCode == http.StatusSwitchingProtocols { |
| return |
| } |
| w.Header().Set("Connection", "close") |
| + body.closing.Store(true) |
| + _ = http.NewResponseController(w).SetReadDeadline(time.Now().Add(exitLingerBound)) |
| +} |
| + |
| +// finishEarlyAnswer runs after the relay when closeAfterEarlyAnswer |
| +// applied. It flushes the answer, then reads and discards the client's |
| +// body itself until the body ends or closeAfterEarlyAnswer's deadline |
| +// passes. That read waits behind the transport's pending read, which the |
| +// deadline interrupts; it must happen before the handler returns, because |
| +// net/http, once the handler is done, aborts a pending read by resetting |
| +// the read deadline to none and then drains the body with no bound. With |
| +// the body ended or the deadline spent, that drain ends at once, and the |
| +// connection closes within about exitLingerBound of the answer. |
| +func finishEarlyAnswer(w http.ResponseWriter, body *streamedBody) { |
| + if body == nil || !body.closing.Load() { |
| + return |
| + } |
| + _ = http.NewResponseController(w).Flush() |
| + _, _ = io.Copy(io.Discard, body.ReadCloser) |
| } |
diff --git a/internal/edge/replay_test.go b/internal/edge/replay_test.go| index b1acc2c0e..3a1cc2187 100644 |
| --- a/internal/edge/replay_test.go |
| +++ b/internal/edge/replay_test.go |
| @@ -20,6 +20,7 @@ import ( |
| "crypto/rand" |
| "crypto/sha256" |
| "crypto/tls" |
| + "crypto/x509" |
| "fmt" |
| "io" |
| "net" |
| @@ -968,3 +969,45 @@ func TestBackhaulNonHAStillStreams(t *testing.T) { |
| t.Fatalf("%d %q %v", r.status, r.body, r.err) |
| } |
| } |
| + |
| +// Fix round 3: after relaying an early closing answer, the forwarding box |
| +// closes the client connection within about a second, even while the |
| +// client withholds the rest of its body. In region and across regions. |
| +func TestEarlyAnswerClosesAStalledClient(t *testing.T) { |
| + stalled := func(t *testing.T, addr, host string, pool *x509.CertPool) { |
| + t.Helper() |
| + raw, err := net.DialTimeout("tcp", addr, 5*time.Second) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + c := tls.Client(raw, &tls.Config{ServerName: host, RootCAs: pool, MinVersion: tls.VersionTLS12}) |
| + defer c.Close() |
| + c.SetDeadline(time.Now().Add(10 * time.Second)) |
| + fmt.Fprintf(c, "POST /echo-body HTTP/1.1\r\nHost: %s\r\nContent-Length: 100000\r\n\r\n0123456789", host) |
| + br := bufio.NewReader(c) |
| + resp, err := http.ReadResponse(br, nil) |
| + if err != nil { |
| + t.Fatalf("no answer: %v", err) |
| + } |
| + b, err := io.ReadAll(resp.Body) |
| + if err != nil || resp.StatusCode != 503 || resp.Header.Get("Retry-After") != "10" || !resp.Close { |
| + t.Fatalf("%d %v %q %v, want today's 503, connection closing", resp.StatusCode, resp.Header, b, err) |
| + } |
| + answered := time.Now() |
| + c.SetReadDeadline(time.Now().Add(2 * time.Second)) |
| + if _, err := br.ReadByte(); err != io.EOF { |
| + t.Fatalf("the connection stayed open while the client withheld its body: %v", err) |
| + } |
| + if waited := time.Since(answered); waited > exitLingerBound+500*time.Millisecond { |
| + t.Fatalf("closed %v after the answer, want about %v", waited, exitLingerBound) |
| + } |
| + } |
| + t.Run("in region", func(t *testing.T) { |
| + x := newRpScene(t) |
| + stalled(t, x.a.tlsAddr, fwHost, x.ca.pool) |
| + }) |
| + t.Run("cross region", func(t *testing.T) { |
| + s, _ := newRpBackhaul(t) |
| + stalled(t, s.sender.tlsAddr, HAForwardHost, backhaulRootCAs) |
| + }) |
| +} |