Skip to content

x/net/http2: when StrictMaxConcurrentStreams enabled ClientConn.ReserveNewRequest() causes stalls in request processing #70809

@mmatczuk

Description

@mmatczuk

Go version

go1.23.3

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/michal/Library/Caches/go-build'
GOENV='/Users/michal/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/michal/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/michal/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/michal/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.3.darwin-arm64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/michal/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.3.darwin-arm64/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.3'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/michal/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/7q/nyynjpwj5p19npby48ykjpx00000gn/T/go-build1425036568=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Please find a minimal reproducer below:

package reprod

import (
	"flag"
	"fmt"
	"net"
	"net/http"
	"sync"
	"testing"

	"golang.org/x/net/http2"
)

var reserve = flag.Bool("reserve", false, "Reserve new request")

type singleConnPool struct {
	cc *http2.ClientConn
}

func (s *singleConnPool) GetClientConn(req *http.Request, addr string) (*http2.ClientConn, error) {
	if (*reserve && s.cc.ReserveNewRequest()) || s.cc.CanTakeNewRequest() {
		return s.cc, nil
	}
	return nil, fmt.Errorf("no available connection")
}

func (s *singleConnPool) MarkDead(conn *http2.ClientConn) {
	panic("not implemented")
}

func TestReserveNewRequest(t *testing.T) {
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	})

	tr := &http2.Transport{
		AllowHTTP:                  true,
		StrictMaxConcurrentStreams: true,
		DisableCompression:         true,
	}

	c0, c1 := net.Pipe()

	s := http2.Server{
		MaxConcurrentStreams: 1,
	}
	go s.ServeConn(c0, &http2.ServeConnOpts{Handler: h})

	cc, err := tr.NewClientConn(c1)
	if err != nil {
		t.Fatal(err)
	}
	tr.ConnPool = &singleConnPool{cc: cc}
	client := http.Client{Transport: tr}

	var wg sync.WaitGroup
	for range 2 {
		wg.Add(1)
		go func() {
			defer wg.Done()
			t.Log("sending request")
			res, err := client.Get("http://foobar")
			if err != nil {
				t.Error(err)
			}
			t.Log("got response", res.StatusCode)
		}()
	}
	wg.Wait()
}

What did you see happen?

Running it without reserve flag works as expected

$ go test -race -v .
=== RUN   TestReserveNewRequest
    rep_test.go:61: sending request
    rep_test.go:61: sending request
    rep_test.go:66: got response 200
    rep_test.go:66: got response 200
--- PASS: TestReserveNewRequest (0.00s)
PASS

Running it with reserve flag set hangs

$ go test -race -v . -reserve
=== RUN   TestReserveNewRequest
    rep_test.go:61: sending request
    rep_test.go:61: sending request

I nailed down this bug to change: https://go-review.googlesource.com/c/net/+/617655

What did you expect to see?

I expect it to work as in x/net version 0.30.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions