Go version

go version go1.22.0 windows/amd64

Output of go env in your module/workspace:

not relevant

What did you do?

Setting *http.Request.Close to true to disable persistent connections in proxyprotocol reverse proxy scenarios. This comes from this issue.

It can be reproduced,

server:

    server := httptest.NewUnstartedServer(h2c.NewHandler(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
        _, _ = io.WriteString(writer, request.Proto)
    }), new(http2.Server)))
    fmt.Println(server.Listener.Addr().String())
    server.Start()
    select {}

client:

func TestSingle(t *testing.T) {
    h2t := &http2.Transport{
        DialTLSContext: func(ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
            return (&net.Dialer{}).DialContext(ctx, network, addr)
        },
        AllowHTTP: true,
    }
    req, _ := http.NewRequest("GET", "http://127.0.0.1:51905", nil)
    req.Close = true
    for range 100 {
        resp, err := h2t.RoundTrip(req)
        if err != nil {
            t.Error(err)
        }
        _ = resp.Write(io.Discard)
    }
}

The error is http2: client conn not usable.

What did you see happen?

It works for https request but not for h2c requests.

What did you expect to see?

It should work in both cases.

Comment From: gopherbot

Change https://go.dev/cl/588635 mentions this issue: http2: check if transport allows h2c before determining if a request has been sent

Comment From: mknyszek

CC @neild

Comment From: gopherbot

Change https://go.dev/cl/591275 mentions this issue: http2: avoid Transport hang with Connection: close and AllowHTTP