Files
magpie/internal/gateway/upstream_h2_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

313 lines
9.2 KiB
Go

package gateway
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"io"
"math/big"
"net"
"net/http"
"strings"
"sync"
"testing"
"time"
"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"
"github.com/yetone/magpie/internal/netproxy"
)
// pingPolicyVendor is an HTTP/2 vendor that answers each request after a
// silence, and holds its clients to gRPC's ping policy, as Google's front
// end does (grpc keepalive.md, chttp2's ping abuse policy): a ping that
// comes within minGap of the last one, with no HEADERS or DATA sent
// between, is a strike, and past two strikes the connection ends with
// GOAWAY ENHANCE_YOUR_CALM "too_many_pings".
type pingPolicyVendor struct {
ln net.Listener
cert *x509.Certificate
silence time.Duration
minGap time.Duration
mu sync.Mutex
conns int
pings []time.Time
goaways int
wg sync.WaitGroup
}
func newPingPolicyVendor(t *testing.T, silence, minGap time.Duration) *pingPolicyVendor {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
tmpl := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "127.0.0.1"},
IPAddresses: []net.IP{net.ParseIP("127.0.0.1").To4()},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
IsCA: true,
BasicConstraintsValid: true,
}
der, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
cert, err := x509.ParseCertificate(der)
if err != nil {
t.Fatal(err)
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
v := &pingPolicyVendor{ln: ln, cert: cert, silence: silence, minGap: minGap}
conf := &tls.Config{
Certificates: []tls.Certificate{{Certificate: [][]byte{der}, PrivateKey: key}},
NextProtos: []string{"h2"},
}
var open []net.Conn
v.wg.Add(1)
go func() {
defer v.wg.Done()
for {
c, err := ln.Accept()
if err != nil {
return
}
v.mu.Lock()
v.conns++
open = append(open, c)
v.mu.Unlock()
v.wg.Add(1)
go func() {
defer v.wg.Done()
defer c.Close()
v.serve(tls.Server(c, conf))
}()
}
}()
t.Cleanup(func() {
ln.Close()
v.mu.Lock()
for _, c := range open {
c.Close()
}
v.mu.Unlock()
v.wg.Wait()
})
return v
}
func (v *pingPolicyVendor) Addr() string { return v.ln.Addr().String() }
func (v *pingPolicyVendor) serve(c *tls.Conn) {
preface := make([]byte, len(http2.ClientPreface))
if _, err := io.ReadFull(c, preface); err != nil || string(preface) != http2.ClientPreface {
return
}
fr := http2.NewFramer(c, c)
var wmu sync.Mutex
write := func(f func() error) error {
wmu.Lock()
defer wmu.Unlock()
return f()
}
if write(func() error { return fr.WriteSettings() }) != nil {
return
}
// the policy's state: chttp2 resets both when it sends HEADERS or DATA
var pmu sync.Mutex
var lastPing time.Time
strikes := 0
var lastStream uint32
for {
f, err := fr.ReadFrame()
if err != nil {
return
}
switch f := f.(type) {
case *http2.SettingsFrame:
if !f.IsAck() {
write(fr.WriteSettingsAck)
}
case *http2.PingFrame:
if f.IsAck() {
continue
}
now := time.Now()
v.mu.Lock()
v.pings = append(v.pings, now)
v.mu.Unlock()
pmu.Lock()
if !lastPing.IsZero() && now.Sub(lastPing) < v.minGap {
strikes++
}
lastPing = now
calm := strikes > 2
last := lastStream
pmu.Unlock()
if calm {
v.mu.Lock()
v.goaways++
v.mu.Unlock()
write(func() error {
return fr.WriteGoAway(last, http2.ErrCodeEnhanceYourCalm, []byte("too_many_pings"))
})
// the in-flight stream is not answered: GFE closes it
time.Sleep(50 * time.Millisecond)
return
}
data := f.Data
write(func() error { return fr.WritePing(true, data) })
case *http2.HeadersFrame:
id := f.StreamID
pmu.Lock()
lastStream = id
pmu.Unlock()
v.mu.Lock()
silence := v.silence
v.mu.Unlock()
go func() {
time.Sleep(silence)
var hb bytes.Buffer
enc := hpack.NewEncoder(&hb)
enc.WriteField(hpack.HeaderField{Name: ":status", Value: "200"})
enc.WriteField(hpack.HeaderField{Name: "content-type", Value: "text/plain"})
pmu.Lock()
strikes, lastPing = 0, time.Time{}
pmu.Unlock()
write(func() error {
if err := fr.WriteHeaders(http2.HeadersFrameParam{StreamID: id, BlockFragment: hb.Bytes(), EndHeaders: true}); err != nil {
return err
}
return fr.WriteData(id, true, []byte("thought it through"))
})
}()
}
}
}
// policyPings has the gateway's transports, made after it, trust vendor,
// and ping as the real ones do with the times scaled down: a silence's
// first ping after 100ms (PingAfter, 10s), the next 1s on (PingAgain,
// 5 minutes), against a vendor whose minimum gap is 600ms (gRPC's 5
// minutes): room on both sides of it.
func policyPings(t *testing.T, vendor *pingPolicyVendor) {
pool := x509.NewCertPool()
pool.AddCert(vendor.cert)
wasTLS := upstreamTLS
wasAfter, wasWait, wasAgain := netproxy.PingAfter, netproxy.PingWait, netproxy.PingAgain
upstreamTLS = &tls.Config{RootCAs: pool, ServerName: "127.0.0.1"}
netproxy.PingAfter, netproxy.PingWait, netproxy.PingAgain = 100*time.Millisecond, time.Second, time.Second
t.Cleanup(func() {
upstreamTLS = wasTLS
netproxy.PingAfter, netproxy.PingWait, netproxy.PingAgain = wasAfter, wasWait, wasAgain
})
}
// zdevoz (Discord): gemini-4-argon through Antigravity thought for about
// four minutes before its first byte, and Google ended the connection:
// "http2: server sent GOAWAY and closed the connection; LastStreamID=3,
// ErrCode=ENHANCE_YOUR_CALM, debug=\"too_many_pings\"". The turn was lost
// at ~235s and sent again from the start. Go's HTTP/2 health check pinged
// the silent connection every PingAfter (its ping's own answer restarts
// the clock), 24 pings in four minutes; Google's front end takes pings
// that close together with nothing sent between as abuse. Now a silence
// gets its first ping at PingAfter, and the next PingAgain later, so the
// vendor thinking for 2.5 (scaled) minutes answers on the first try.
func TestLongSilenceIsNotPingedIntoGoaway(t *testing.T) {
fresh(t)
vendor := newPingPolicyVendor(t, 2500*time.Millisecond, 600*time.Millisecond)
policyPings(t, vendor)
s := New()
t.Cleanup(s.client.CloseIdleConnections)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodPost,
"https://"+vendor.Addr()+"/v1internal:streamGenerateContent?alt=sse", strings.NewReader(`{"request":{}}`))
began := time.Now()
res, err := s.client.Do(req)
took := time.Since(began)
vendor.mu.Lock()
pings := append([]time.Time(nil), vendor.pings...)
goaways, conns := vendor.goaways, vendor.conns
vendor.mu.Unlock()
if err != nil {
t.Fatalf("the turn failed after %v, %d pings, %d GOAWAYs: %v", took, len(pings), goaways, err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil || string(b) != "thought it through" {
t.Fatalf("the turn's answer: %q, %v", b, err)
}
if goaways != 0 || conns != 1 {
t.Fatalf("%d GOAWAYs over %d connections; want the turn answered on its one connection", goaways, conns)
}
// a silence is still pinged, from PingAfter on, so a dead path is
// found as soon as before (#4936)
if len(pings) == 0 {
t.Fatal("the silent connection was never pinged")
}
if len(pings) > 4 {
t.Fatalf("%d pings in a %v silence; want one at PingAfter and one each PingAgain", len(pings), vendor.silence)
}
if d := pings[0].Sub(began); d > time.Second {
t.Fatalf("the first ping came %v into the silence; want it about PingAfter (100ms) in", d)
}
}
// A GOAWAY ends the connection it came on, and the try sent again goes out
// on a new one, never the connection that refused it.
func TestTryAfterGoawayDialsAnew(t *testing.T) {
fresh(t)
// a vendor whose minimum gap is longer than PingAgain: the third ping
// of a silence ends the connection
vendor := newPingPolicyVendor(t, 2500*time.Millisecond, 5*time.Second)
policyPings(t, vendor)
netproxy.PingAgain = 300 * time.Millisecond
tr := vendorTransport(time.Minute)
t.Cleanup(tr.CloseIdleConnections)
cl := &http.Client{Transport: tr}
ask := func() error {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "https://"+vendor.Addr()+"/turn", strings.NewReader("{}"))
res, err := cl.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
_, err = io.ReadAll(res.Body)
return err
}
err := ask()
if err == nil || !strings.Contains(err.Error(), "too_many_pings") {
t.Fatalf("the turn on a vendor that refuses the pings: %v; want its GOAWAY", err)
}
vendor.mu.Lock()
vendor.silence = 0
vendor.mu.Unlock()
if err := ask(); err != nil {
t.Fatalf("the try after the GOAWAY: %v", err)
}
vendor.mu.Lock()
conns := vendor.conns
vendor.mu.Unlock()
if conns != 2 {
t.Fatalf("%d connections dialled; want the try after the GOAWAY on a new one", conns)
}
}