From 87dfe005f28085620aa202bbc0486b3f6362afba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveirinha?= Date: Fri, 30 Oct 2026 15:54:58 +0000 Subject: [PATCH] TUN-11058: Fix regression on max concurrent streams over http2 introduced by go1.27 The bump to go1.27 introduced a regression where using MaxUint32 resulted in a fallback to the default 250 concurrent streams value. This fix uses MaxInt32 to resolve that regression. https://github.com/golang/go/issues/82497 --- AGENTS.md | 2 +- connection/connection.go | 3 +- connection/http2.go | 19 ++++++----- connection/http2_settings_test.go | 53 +++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 connection/http2_settings_test.go diff --git a/AGENTS.md b/AGENTS.md index 5242f82e..fae12b11 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -155,7 +155,7 @@ func TestMetricsListenerCreation(t *testing.T) { ```go const ( MaxGracePeriod = time.Minute * 3 - MaxConcurrentStreams = math.MaxUint32 + MaxConcurrentStreams = math.MaxInt32 LogFieldConnIndex = "connIndex" ) diff --git a/connection/connection.go b/connection/connection.go index 24ae9b54..3162aa47 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -24,7 +24,8 @@ const ( lbProbeUserAgentPrefix = "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/;" LogFieldConnIndex = "connIndex" MaxGracePeriod = time.Minute * 3 - MaxConcurrentStreams = math.MaxUint32 + // Go 1.27 defaults limits above MaxInt32 to 250 concurrent streams. + MaxConcurrentStreams = math.MaxInt32 contentTypeHeader = "content-type" contentLengthHeader = "content-length" diff --git a/connection/http2.go b/connection/http2.go index c7e14c67..3a068c1d 100644 --- a/connection/http2.go +++ b/connection/http2.go @@ -37,7 +37,7 @@ var errEdgeConnectionClosed = fmt.Errorf("connection with edge closed") // origin. type HTTP2Connection struct { conn net.Conn - server *http2.Server + server *http2.Server //nolint:staticcheck orchestrator Orchestrator connOptions *client.ConnectionOptionsSnapshot observer *Observer @@ -62,8 +62,11 @@ func NewHTTP2Connection( ) *HTTP2Connection { return &HTTP2Connection{ conn: conn, - server: &http2.Server{ - MaxConcurrentStreams: MaxConcurrentStreams, + server: &http2.Server{ //nolint:staticcheck + // In case more values are set it is imperative to update + // the test TestHTTP2MaxConcurrentStreams expecting the + // new values + MaxConcurrentStreams: MaxConcurrentStreams, //nolint:staticcheck }, orchestrator: orchestrator, connOptions: connOptions, @@ -80,7 +83,7 @@ func (c *HTTP2Connection) Serve(ctx context.Context) error { <-ctx.Done() c.close() }() - c.server.ServeConn(c.conn, &http2.ServeConnOpts{ + c.server.ServeConn(c.conn, &http2.ServeConnOpts{ //nolint: staticcheck Context: ctx, Handler: c, }) @@ -131,7 +134,7 @@ func (c *HTTP2Connection) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Check for tracing on request tr := tracing.NewTracedHTTPRequest(r, c.connIndex, c.log) if err := originProxy.ProxyHTTP(respWriter, tr, connType == TypeWebsocket); err != nil { - requestErr = fmt.Errorf("Failed to proxy HTTP: %w", err) + requestErr = fmt.Errorf("failed to proxy HTTP: %w", err) } case TypeTCP: @@ -151,7 +154,7 @@ func (c *HTTP2Connection) ServeHTTP(w http.ResponseWriter, r *http.Request) { }) default: - requestErr = fmt.Errorf("Received unknown connection type: %s", connType) + requestErr = fmt.Errorf("received unknown connection type: %s", connType) } if requestErr != nil { @@ -188,7 +191,7 @@ func (c *HTTP2Connection) handleConfigurationUpdate(respWriter *http2RespWriter, func (c *HTTP2Connection) close() { // Wait for all serve HTTP handlers to return c.activeRequestsWG.Wait() - c.conn.Close() + _ = c.conn.Close() } type http2RespWriter struct { @@ -232,7 +235,7 @@ func (rp *http2RespWriter) AddTrailer(trailerName, trailerValue string) { return } - rp.w.Header().Add(http2.TrailerPrefix+trailerName, trailerValue) + rp.w.Header().Add(http.TrailerPrefix+trailerName, trailerValue) } func (rp *http2RespWriter) WriteRespHeaders(status int, header http.Header) error { diff --git a/connection/http2_settings_test.go b/connection/http2_settings_test.go new file mode 100644 index 00000000..da8e48a1 --- /dev/null +++ b/connection/http2_settings_test.go @@ -0,0 +1,53 @@ +package connection + +import ( + "context" + "io" + "math" + "testing" + "time" + + "github.com/stretchr/testify/require" + "golang.org/x/net/http2" +) + +func TestHTTP2MaxConcurrentStreams(t *testing.T) { + // Currently, the only value set for the http2.Server is MaxConcurrentStreams + // this test should be updated in case there is an addition to the values that + // are set. + + t.Parallel() + conn, edgeConn := newTestHTTP2Connection() + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + served := make(chan struct{}) + go func() { + defer close(served) + _ = conn.Serve(ctx) + }() + t.Cleanup(func() { + cancel() + _ = edgeConn.Close() + select { + case <-served: + case <-time.After(5 * time.Second): + t.Fatal("HTTP/2 connection did not shut down") + } + }) + deadline, _ := ctx.Deadline() + require.NoError(t, edgeConn.SetDeadline(deadline)) + + // net.Pipe is unbuffered, so read SETTINGS while the server reads the preface. + prefaceWritten := make(chan error, 1) + go func() { + _, err := io.WriteString(edgeConn, http2.ClientPreface) + prefaceWritten <- err + }() + frame, err := http2.NewFramer(edgeConn, edgeConn).ReadFrame() + require.NoError(t, err) + settings, ok := frame.(*http2.SettingsFrame) + require.True(t, ok, "first server frame must be SETTINGS") + limit, ok := settings.Value(http2.SettingMaxConcurrentStreams) + require.True(t, ok, "SETTINGS_MAX_CONCURRENT_STREAMS must be advertised") + require.Equal(t, uint32(math.MaxInt32), limit) + require.NoError(t, <-prefaceWritten) +}