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)
257 lines
7.9 KiB
Go
257 lines
7.9 KiB
Go
package netproxy
|
|
|
|
import (
|
|
"bufio"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"io"
|
|
"math/big"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A request whose connection's path dies after it was written — a proxy's
|
|
// tunnel whose far side a NAT or a relay dropped — is discovered only by
|
|
// the connection's health check, which goes out PingAfter after the last
|
|
// frame the connection received (#4936: a first try read EOF ~20.5s after
|
|
// it was written, then magpie's own retry succeeded at once on a new
|
|
// connection). The test watches the bound with the timings scaled down: a
|
|
// holed connection is pinged at PingAfter, dropped at PingAfter+PingWait,
|
|
// and the try on it fails with the connection, while the next one dials
|
|
// anew and succeeds. The same holds for Go's own health check, which
|
|
// DefaultTransport has (HTTP2Health), and for the gateway's (CalmHTTP2).
|
|
func TestRequestOnAHoledConnectionFailsAtThePing(t *testing.T) {
|
|
pingAfter, pingWait := time.Second, 500*time.Millisecond
|
|
t.Run("HTTP2Health", func(t *testing.T) {
|
|
holedTry(t, pingAfter, pingWait, &http.Transport{
|
|
ForceAttemptHTTP2: true,
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
HTTP2: &http.HTTP2Config{SendPingTimeout: pingAfter, PingTimeout: pingWait},
|
|
})
|
|
})
|
|
t.Run("CalmHTTP2", func(t *testing.T) {
|
|
scalePings(t, pingAfter, pingWait, time.Minute)
|
|
tr := &http.Transport{
|
|
ForceAttemptHTTP2: true,
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}
|
|
if err := CalmHTTP2(tr); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// the gateway's lostPing knows the ping's failure by Go's words
|
|
if said := holedTry(t, pingAfter, pingWait, tr); !strings.Contains(said, "http2: client connection lost") {
|
|
t.Fatalf("the turn failed with %q; want Go's words for a lost ping", said)
|
|
}
|
|
})
|
|
}
|
|
|
|
func scalePings(t *testing.T, after, wait, again time.Duration) {
|
|
wasAfter, wasWait, wasAgain := PingAfter, PingWait, PingAgain
|
|
PingAfter, PingWait, PingAgain = after, wait, again
|
|
t.Cleanup(func() { PingAfter, PingWait, PingAgain = wasAfter, wasWait, wasAgain })
|
|
}
|
|
|
|
// holedTry runs a request on a connection whose path died through tr,
|
|
// which pings after pingAfter and waits pingWait for the answer, and one
|
|
// after it.
|
|
func holedTry(t *testing.T, pingAfter, pingWait time.Duration, tr *http.Transport) string {
|
|
t.Cleanup(tr.CloseIdleConnections)
|
|
|
|
// The vendor, speaking HTTP/2 over TLS on a listener the shim pumps
|
|
// raw bytes into, as a CONNECT tunnel would.
|
|
srv := newH2Vendor(t)
|
|
// The tunnel: a byte pump with a hole per connection.
|
|
shim, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { shim.Close() })
|
|
var mu sync.Mutex
|
|
holed := map[net.Conn]bool{}
|
|
serverHits := 0
|
|
go func() {
|
|
for {
|
|
c, err := shim.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
go func(c net.Conn) {
|
|
defer c.Close()
|
|
s, err := net.Dial("tcp", srv.Addr())
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer s.Close()
|
|
mu.Lock()
|
|
serverHits++
|
|
holed[c] = false
|
|
mu.Unlock()
|
|
defer func() { mu.Lock(); delete(holed, c); mu.Unlock() }()
|
|
var once sync.Once
|
|
go func() { // vendor -> client
|
|
io.Copy(c, s)
|
|
once.Do(func() { c.Close() })
|
|
}()
|
|
br := bufio.NewReader(c)
|
|
buf := make([]byte, 16<<10)
|
|
for {
|
|
n, err := br.Read(buf)
|
|
if n > 0 {
|
|
mu.Lock()
|
|
hole := holed[c]
|
|
mu.Unlock()
|
|
if !hole {
|
|
if _, werr := s.Write(buf[:n]); werr != nil {
|
|
once.Do(func() { c.Close() })
|
|
return
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
once.Do(func() { s.Close() })
|
|
return
|
|
}
|
|
}
|
|
}(c)
|
|
}
|
|
}()
|
|
punchHole := func() {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
for c := range holed {
|
|
holed[c] = true
|
|
}
|
|
}
|
|
|
|
client := &http.Client{Transport: tr, Timeout: 20 * time.Second}
|
|
post := func() (time.Duration, int, error) {
|
|
start := time.Now()
|
|
req, err := http.NewRequest("POST", "https://"+shim.Addr().String()+"/turn", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res, err := client.Do(req)
|
|
took := time.Since(start)
|
|
if err != nil {
|
|
return took, 0, err
|
|
}
|
|
io.Copy(io.Discard, res.Body)
|
|
res.Body.Close()
|
|
return took, res.StatusCode, nil
|
|
}
|
|
|
|
if took, code, err := post(); err != nil || code != 200 {
|
|
t.Fatalf("the first turn: %d in %v, %v", code, took, err)
|
|
}
|
|
punchHole() // the tunnel's far side dies, the way a NAT session does
|
|
|
|
took, code, err := post()
|
|
mu.Lock()
|
|
dials := serverHits
|
|
mu.Unlock()
|
|
if err == nil {
|
|
t.Fatalf("the turn on the holed connection answered %d in %v; want the dead path found at the ping", code, took)
|
|
}
|
|
if took < pingAfter-pingAfter/4 {
|
|
t.Fatalf("the turn failed after %v, before the health check was due (%v): %v", took, pingAfter, err)
|
|
}
|
|
if took > pingAfter+pingWait+3*time.Second {
|
|
t.Fatalf("the turn failed after %v; the health check should have found the dead path by %v: %v", took, pingAfter+pingWait, err)
|
|
}
|
|
said := err.Error()
|
|
if !containsAny(said, "connection lost", "EOF", "unexpected EOF", "closed") {
|
|
t.Fatalf("the turn failed with %q; want the connection's own end", said)
|
|
}
|
|
// magpie's own retry: a new connection, the turn served.
|
|
took, code, err = post()
|
|
if err != nil || code != 200 {
|
|
t.Fatalf("the retry: %d in %v, %v", code, took, err)
|
|
}
|
|
mu.Lock()
|
|
dials = serverHits
|
|
mu.Unlock()
|
|
if dials != 2 {
|
|
t.Fatalf("the vendor was dialed %d times; want the retry on a connection of its own", dials)
|
|
}
|
|
return said
|
|
}
|
|
|
|
func containsAny(s string, subs ...string) bool {
|
|
for _, sub := range subs {
|
|
if strings.Contains(s, sub) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// The health check is the only probe of a connection whose path has died
|
|
// silently: a turn in flight on it waits for the next one, so PingAfter is
|
|
// how long a vendor's silence before a turn's first byte can cost (#4936:
|
|
// a first try read EOF 20.5s in, then a retry that served at once — the
|
|
// 20s was the ping). Half of it keeps the budget PingAfter+PingWait at
|
|
// 20s, the margin a body under uploadBytes needs on an uplink of about
|
|
// 3KB/s before a ping queued behind it is mistaken for a lost one (#3909).
|
|
func TestPingAfterSizesTheDeadPathDiscovery(t *testing.T) {
|
|
if PingAfter != 10*time.Second {
|
|
t.Fatalf("PingAfter %v; want 10s: the dead path of a turn in flight is found by the next health check, and #3909's slow bodies keep the %v budget PingAfter+PingWait", PingAfter, PingAfter+PingWait)
|
|
}
|
|
if PingWait != 10*time.Second {
|
|
t.Fatalf("PingWait %v; want 10s", PingWait)
|
|
}
|
|
}
|
|
|
|
// h2Vendor is an HTTP/2 server on a plain listener a test's shim can pump
|
|
// raw bytes to.
|
|
type h2Vendor struct {
|
|
ln net.Listener
|
|
}
|
|
|
|
func (v *h2Vendor) Addr() string { return v.ln.Addr().String() }
|
|
|
|
func newH2Vendor(t *testing.T) *h2Vendor {
|
|
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.KeyUsageKeyEncipherment | 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)
|
|
}
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
w.WriteHeader(200)
|
|
io.WriteString(w, "data: ok\n\n")
|
|
}), TLSConfig: &tls.Config{
|
|
Certificates: []tls.Certificate{{Certificate: [][]byte{der}, PrivateKey: key}},
|
|
NextProtos: []string{"h2"},
|
|
}}
|
|
go srv.ServeTLS(ln, "", "")
|
|
t.Cleanup(func() { ln.Close() })
|
|
return &h2Vendor{ln: ln}
|
|
}
|