Files
magpie/internal/netproxy/calm_test.go
T
yetone 970a86ff30 gateway: a vendor thinking for minutes is not pinged into GOAWAY too_many_pings
zdevoz (Discord, Windows, 0.1.3249): gemini-4-argon at effort high
through Antigravity waited ~235s for its first byte and then failed with
"http2: server sent GOAWAY and closed the connection; LastStreamID=3,
ErrCode=ENHANCE_YOUR_CALM, debug=\"too_many_pings\"". magpie logged a 502
and sent the whole turn again, so one turn took 421s.

Cause: the gateway's transports used Go's HTTP/2 health check
(netproxy.HTTP2Health, SendPingTimeout 10s). Go's client resets its
read-idle timer on every frame it reads, the ping's own ACK included
(h2_bundle.go readLoop / healthCheck), so a silent connection is pinged
every 10s for as long as the silence lasts, about 24 pings in four
minutes. Google's front end holds clients to gRPC's ping policy
(keepalive.md: a ping within MIN_RECV_PING_INTERVAL_WITHOUT_DATA, 5
minutes, of the last with no HEADERS or DATA sent between is a strike;
past MAX_PING_STRIKES, 2, the server sends GOAWAY ENHANCE_YOUR_CALM
"too_many_pings").

Fix: netproxy.CalmHTTP2 gives the gateway's vendor transports
(vendorTransport, so upstreamTransport and each Dispatch clone) an
HTTP/2 health check of magpie's own, on x/net/http2 with a ConnPool that
never dials (net/http dials through the proxy and hands the connection
over in TLSNextProto). The first ping of a silence still goes at
PingAfter (10s) and a lost one still drops the connection after
PingWait, in Go's words ("http2: client connection lost", which lostPing
reads), so #2843 and #4936 are found as fast as before. A silence starts
at the vendor's last HEADERS/DATA/CONTINUATION frame (read from the
frame headers as they come) or at the request handed to the connection.
After an answered ping the next waits PingAgain, 5 minutes, so a 4-minute
silence has one ping. A Dispatch clone gets its own pool (Clone copies
TLSNextProto, not RegisterProtocol), and t.CloseIdleConnections reaches
the calm pool through net/http's altProto lookup, so the proxy-move and
region drops (#4523, #5182) still close the connections.
uploadTransport stays on Go's HTTP/2 with no ping (#3909), and
http.DefaultTransport keeps HTTP2Health: its requests are short, and it
is cloned at 8 sites that would share one calm pool across proxies.

Retry after a GOAWAY: the retry the reporter saw is still made. With the
pings gone it should not be needed. When a GOAWAY does come, the
connection can't take a new request and the pool drops it, so the next
try dials anew (TestTryAfterGoawayDialsAnew).

Tests (no network):
- TestLongSilenceIsNotPingedIntoGoaway: a raw x/net Framer HTTP/2 vendor
  that enforces gRPC's ping policy (minimum gap scaled to 600ms, GOAWAY
  ENHANCE_YOUR_CALM too_many_pings past 2 strikes) and answers after a
  2.5s silence, reached through upstreamTransport with pings scaled to
  100ms/1s/1s.
- TestTryAfterGoawayDialsAnew.
- TestRequestOnAHoledConnectionFailsAtThePing now runs for HTTP2Health
  and CalmHTTP2, and the calm run must fail with "client connection lost".
- TestCalmFindsAPathDeadLaterInTheSilence: a path that dies after the
  first ping was answered is found at the PingAgain ping.
- TestCalmIdleConnectionsCloseAndClonesKeepTheirOwn.

Run (mote, Go 1.26.8):
- go vet -tags nogui ./internal/netproxy ./internal/gateway, plus
  GOOS=windows and GOOS=linux vet; gofmt -l clean.
- go test -race -count=1 ./internal/netproxy: ok.
- go test -count=1 -run
  "H2|HTTP2|Silent|Upload|Uplink|Region|Proxy|Goaway|LongSilence|Unsent|Replay|Fresh|LargeBody|Codex|Antigravity|CodeAssist"
  ./internal/gateway: ok (131s).
- win-run (xiaomi-gpu, Windows 11): go test ./internal/netproxy ok, and
  the gateway's Goaway/LongSilence/SilentHTTP2/Unsent/Upload/LargeBody
  tests ok.
- Again at the pushed head, rebased on 784113472: vet, gofmt,
  ./internal/netproxy and the gateway subset above (with TokenWall and
  Fallback, which the rebase brought in): ok.
- The full go test -timeout 45m ./... runs after the push under the
  suite lock; any red is filed as an issue with its output.

Broken by hand:
- vendorTransport back on HTTP2Health: TestLongSilenceIsNotPingedIntoGoaway
  red in 0.46s with the reporter's exact error: "http2: server sent GOAWAY
  and closed the connection; LastStreamID=1, ErrCode=ENHANCE_YOUR_CALM,
  debug=\"too_many_pings\"" (4 pings, 1 GOAWAY).
- watch with PingAgain = 1h: TestCalmFindsAPathDeadLaterInTheSilence red
  (context deadline at 20s).
- watch with PingAfter = 1h: TestRequestOnAHoledConnectionFailsAtThePing/
  CalmHTTP2 red (20s, the dead path not found).
- Dispatch not re-calming its clones: base's request went on the clone's
  connection, red.
- calmPool.CloseIdleConnections a no-op: the idle connection never
  closed, red.

Not tried: a real Antigravity or Google request (no real account was
used, and Google's exact tolerance is unknown: the report suggests about
22 pings at 10s). Not tried with a real proxy between. Go 1.27 moves
x/net's Transport onto net/http's (transport_wrap.go); ConnPool is still
honoured there, but this has not been run on it.

Reported-by: zdevoz (Discord)
2026-11-07 22:18:28 +08:00

230 lines
6.3 KiB
Go

package netproxy
import (
"context"
"crypto/tls"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
// hushPipe passes bytes between a client and to until hushed: then it
// goes on taking what either side writes and passes none of it, as a
// tunnel whose far side died does.
type hushPipe struct {
ln net.Listener
hushed atomic.Bool
mu sync.Mutex
conns []net.Conn
dials int
}
func newHushPipe(t *testing.T, to string) *hushPipe {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
p := &hushPipe{ln: ln}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
a, err := ln.Accept()
if err != nil {
return
}
b, err := net.Dial("tcp", to)
if err != nil {
a.Close()
continue
}
p.mu.Lock()
p.conns = append(p.conns, a, b)
p.dials++
p.mu.Unlock()
pump := func(dst, src net.Conn) {
defer wg.Done()
buf := make([]byte, 32<<10)
for {
n, err := src.Read(buf)
if n > 0 && !p.hushed.Load() {
if _, err := dst.Write(buf[:n]); err != nil {
return
}
}
if err != nil {
return
}
}
}
wg.Add(2)
go pump(a, b)
go pump(b, a)
}
}()
t.Cleanup(func() {
ln.Close()
p.mu.Lock()
for _, c := range p.conns {
c.Close()
}
p.mu.Unlock()
wg.Wait()
})
return p
}
func (p *hushPipe) Addr() string { return p.ln.Addr().String() }
// slowH2 is an HTTP/2 vendor that answers after wait, and counts the
// connections it has open.
func slowH2(t *testing.T, wait time.Duration) (*httptest.Server, *atomic.Int32) {
t.Helper()
var open atomic.Int32
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
select {
case <-time.After(wait):
case <-r.Context().Done():
return
}
w.Header().Set("X-Conn", r.RemoteAddr)
io.WriteString(w, r.Proto)
}))
srv.EnableHTTP2 = true
srv.Config.ConnState = func(_ net.Conn, s http.ConnState) {
switch s {
case http.StateNew:
open.Add(1)
case http.StateClosed, http.StateHijacked:
open.Add(-1)
}
}
srv.StartTLS()
t.Cleanup(srv.Close)
return srv, &open
}
func calmTransport(t *testing.T) *http.Transport {
t.Helper()
tr := &http.Transport{ForceAttemptHTTP2: true, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
if err := CalmHTTP2(tr); err != nil {
t.Fatal(err)
}
t.Cleanup(tr.CloseIdleConnections)
return tr
}
func calmAsk(rt http.RoundTripper, ctx context.Context, url string) (string, error) {
got, _, err := calmAskOn(rt, ctx, url)
return got, err
}
// calmAskOn is calmAsk, and the connection the answer came on, as the
// vendor saw its far end.
func calmAskOn(rt http.RoundTripper, ctx context.Context, url string) (string, string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader("{}"))
if err != nil {
return "", "", err
}
res, err := rt.RoundTrip(req)
if err != nil {
return "", "", err
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
return string(b), res.Header.Get("X-Conn"), err
}
// A path that dies after the first ping of a long silence was answered is
// found at the next one, PingAgain on, and the request on it fails in
// Go's words for a lost ping, which the gateway's lostPing reads.
func TestCalmFindsAPathDeadLaterInTheSilence(t *testing.T) {
scalePings(t, 100*time.Millisecond, 300*time.Millisecond, 800*time.Millisecond)
srv, _ := slowH2(t, 30*time.Second)
pipe := newHushPipe(t, srv.Listener.Addr().String())
tr := calmTransport(t)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
go func() {
// after the first ping (100ms) and its answer
time.Sleep(450 * time.Millisecond)
pipe.hushed.Store(true)
}()
began := time.Now()
_, err := calmAsk(tr, ctx, "https://"+pipe.Addr()+"/turn")
took := time.Since(began)
if err == nil {
t.Fatal("the turn on a dead path was answered")
}
if !strings.Contains(err.Error(), "http2: client connection lost") {
t.Fatalf("the turn failed with %v after %v; want the lost ping", err, took)
}
// the next ping goes PingAgain after the first (~900ms), and is lost
// PingWait later (~1.2s)
if took < 800*time.Millisecond || took > 10*time.Second {
t.Fatalf("the dead path was found after %v; want at the ping PingAgain after the first", took)
}
}
// CloseIdleConnections closes a calmed transport's HTTP/2 connections, as
// it closes Go's own: the gateway drops them when the proxy or region
// moves (#4523, #5182). A Dispatch clone, for a provider's own proxy,
// keeps connections of its own, dialled through that proxy, never base's.
func TestCalmIdleConnectionsCloseAndClonesKeepTheirOwn(t *testing.T) {
srv, open := slowH2(t, 0)
u := "https://" + srv.Listener.Addr().String() + "/turn"
tr := calmTransport(t)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
for range 2 {
if got, err := calmAsk(tr, ctx, u); err != nil || got != "HTTP/2.0" {
t.Fatalf("a request: %q, %v; want HTTP/2.0", got, err)
}
}
if n := open.Load(); n != 1 {
t.Fatalf("%d connections open after two requests; want one, kept", n)
}
tr.CloseIdleConnections()
waitFor(t, func() bool { return open.Load() == 0 }, "the idle HTTP/2 connection to close")
d := Dispatch(tr)
// the clone first: a connection it dialled and left in base's pool
// would carry base's next request
got, own, err := calmAskOn(d, With(ctx, "direct"), u)
if err != nil || got != "HTTP/2.0" {
t.Fatalf("a request through a provider's own proxy: %q, %v", got, err)
}
got, base, err := calmAskOn(d, ctx, u)
if err != nil || got != "HTTP/2.0" {
t.Fatalf("a request through base: %q, %v", got, err)
}
if base == own {
t.Fatalf("base's request went on the clone's connection (%s)", own)
}
if n := open.Load(); n != 2 {
t.Fatalf("%d connections open; want base's and the clone's own", n)
}
d.(interface{ CloseIdleConnections() }).CloseIdleConnections()
waitFor(t, func() bool { return open.Load() == 0 }, "base's and the clone's idle connections to close")
}
func waitFor(t *testing.T, ok func() bool, what string) {
t.Helper()
deadline := time.Now().Add(10 * time.Second)
for !ok() {
if time.Now().After(deadline) {
t.Fatalf("waited 10s for %s", what)
}
time.Sleep(10 * time.Millisecond)
}
}