Skip to content

x/net/http2: h2c requests can't be sent when *http.Request.Close is true #67671

Closed
@WeidiDeng

Description

@WeidiDeng

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.

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