Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: EnableFullDuplex without closing Request.Body panics with invalid concurrent Body.Read call #68560

Open
DavidArchibald opened this issue Jul 23, 2024 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@DavidArchibald
Copy link

Go version

go version go1.22.5 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/david/.cache/go-build'
GOENV='/home/david/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/david/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/david/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/david/Projects/My Price Health/mono/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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build17757478=/tmp/go-build -gno-record-gcc-switches

What did you do?

While I encountered this with a full fledged server I simplified it down to this script:

package main

import "net/http"

func main() {
	s := &http.Server{
		Addr: "localhost:8080",
		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			err := http.NewResponseController(w).EnableFullDuplex()
			if err != nil {
				panic(err)
			}
		}),
	}

	err := s.ListenAndServe()
	if err != nil {
		panic(err)
	}
}

I then narrowed down the issue to when I make a request like so:

import requests

requests.request("GET", "http://localhost:8080", json={})

I don't have this problem when I try to reproduce the issue in Postman or craft the request myself, even when I make it obstensibly identical. Likely because of differences in client behaviours.

What did you see happen?

The program panicked with this result:

2024/07/23 15:29:42 http: panic serving 127.0.0.1:36822: invalid concurrent Body.Read call
goroutine 20 [running]:
net/http.(*conn).serve.func1()
        /usr/local/go/src/net/http/server.go:1903 +0xbe
panic({0x63b1c0?, 0x6ff850?})
        /usr/local/go/src/runtime/panic.go:770 +0x132
net/http.(*connReader).Read(0xc00011b0b0, {0xc000244000, 0x1000, 0x1000})
        /usr/local/go/src/net/http/server.go:768 +0x22a
bufio.(*Reader).fill(0xc00012a360)
        /usr/local/go/src/bufio/bufio.go:110 +0x103
bufio.(*Reader).Peek(0xc00012a360, 0x4)
        /usr/local/go/src/bufio/bufio.go:148 +0x53
net/http.(*conn).serve(0xc00023e000, {0x702b90, 0xc00011afc0})
        /usr/local/go/src/net/http/server.go:2079 +0x749
created by net/http.(*Server).Serve in goroutine 1
        /usr/local/go/src/net/http/server.go:3290 +0x4b4

What did you expect to see?

I expected no panic. Perhaps there's a reason that enabling EnableFullDuplex is bad in a handler like this but I couldn't find any documentation that suggested this would be harmful.

I could manage to get this panic to go away by moving this handler a bit later into the actual router I have but this seems to indicate to me that it's actually a race condition or something.

@seankhliao seankhliao changed the title invalid concurrent Body.Read call panic when EnableFullDuplex is enabled in http.Server.Handler net/http: EnableFullDuplex without closing Request.Body panics with invalid concurrent Body.Read call Jul 24, 2024
@seankhliao
Copy link
Member

Any request with a body will do, e.g. curl http://localhost:8080 -d '{}'. Python isn't necessary to make the request.

Looks like the concurrent read happens when the server tries to drain the body in preparation for reusing the connection. Adding either of the below avoids the panic:

  • defer r.Body.Close() to the handler
  • marking the connection non-reusable with -H 'connection: close' from the client side

@seankhliao
Copy link
Member

cc @neild

@DavidArchibald
Copy link
Author

marking the connection non-reusable with -H 'connection: close' from the client side

That makes sense. I can confirm that the other clients I tried were adding connection: close to their headers. This is why I ended up including Python in my attempt at minimum reproduction because I couldn't figure out how to distill it down.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 25, 2024
@dmitshur dmitshur added this to the Backlog milestone Jul 25, 2024
@Tachone
Copy link

Tachone commented Aug 21, 2024

Any request with a body will do, e.g. curl http://localhost:8080 -d '{}'. Python isn't necessary to make the request.

Looks like the concurrent read happens when the server tries to drain the body in preparation for reusing the connection. Adding either of the below avoids the panic:

  • defer r.Body.Close() to the handler
  • marking the connection non-reusable with -H 'connection: close' from the client side

@seankhliao When the server has EnableFullDuplex enabled, should it be specified in the documentation that req.Body.Close() needs to be called explicitly, since the transport no longer handles closing it?
@neild Could you share your thoughts on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants