Skip to content

net/http/httputil: ReverseProxy stuck forever when handling request with expect: 100-continue header and upstream connection creation fails #75933

@SecuriFresh

Description

@SecuriFresh

Go version

go version go1.24.0 windows/amd64

Output of go env in your module/workspace:

set AR=ar
set CC=gcc
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=0
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set CXX=g++
set GCCGO=gccgo
set GO111MODULE=on
set GOAMD64=v1
set GOARCH=amd64
set GOAUTH=netrc
set GOBIN=
set GOCACHE=C:\Users\xxxx\AppData\Local\go-build
set GOCACHEPROG=
set GODEBUG=
set GOENV=C:\Users\xxxx\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFIPS140=off
set GOFLAGS=
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\xxxx\AppData\Local\Temp\go-build1085482316=/tmp/go-build -gno-record-gcc-switches
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMOD=C:\Users\xxxx\Code\yyyyy\zzzzzz\go.mod
set GOMODCACHE=C:\Users\xxxx\go\pkg\mod
set GONOPROXY=yyyyy.com
set GONOSUMDB=yyyyy.com
set GOOS=windows
set GOPATH=C:\Users\xxxx\go
set GOPRIVATE=yyyyy.com
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:/Users/xxxx/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.0.windows-amd64
set GOSUMDB=sum.golang.org
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\xxxx\AppData\Roaming\go\telemetry
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Users\xxxx\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.24.0.windows-amd64\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.24.0
set GOWORK=
set PKG_CONFIG=pkg-config

What did you do?

When httputil.ReverseProxy handles a request with the 'Expect: 100-continue' header and the upstream connection could not be created, the ServeHTTP function never returns.

This is because the proxy tries to consume and close the request's body before returning the response, however the request body was still not sent by the client (client can wait for the intermediate HTTP 100 response before sending the body)

Here is a small unit test to simulate this issue:

func Test100ContinueNonExist(t *testing.T) {

	proxy := httputil.ReverseProxy{
		Transport: &http.Transport{DisableKeepAlives: true, ExpectContinueTimeout: time.Second * 60},
		Director: func(request *http.Request) {
			request.URL.Scheme = "http"
			request.URL.Host = "localhost:12345" // non-existent upstream
		},
	}
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		proxy.ServeHTTP(w, r)
	})

	upstreamServer := httptest.NewServer(handler)
	defer upstreamServer.Close()

	conn, _ := net.Dial("tcp", upstreamServer.Listener.Addr().String())

	requestBody := `{"test": "data"}`
	initialRequest := fmt.Sprintf("POST %s/test-expect HTTP/1.1\r\n"+
		"Host: %s\r\n"+
		"Content-Type: application/json\r\n"+
		"Content-Length: %d\r\n"+
		"Expect: 100-continue\r\n"+
		"\r\n", upstreamServer.URL, upstreamServer.Listener.Addr().String(), len(requestBody))

	_, _ = conn.Write([]byte(initialRequest))

	buff := make([]byte, 1024)
	_, _ = conn.Read(buff)

}

What did you see happen?

the ServeHTTP function of httputil.ReverseProxy is stuck and never returns

What did you expect to see?

I would expect the client to receive HTTP 502 response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions