Skip to content

net/http: Setting r.Close = true in a handler does not cause the server to close the connection #70833

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

Closed
johnmaguire opened this issue Dec 13, 2024 · 4 comments

Comments

@johnmaguire
Copy link

Go version

go version go1.23.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/jmaguire/Library/Caches/go-build'
GOENV='/Users/jmaguire/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/jmaguire/go/pkg/mod'
GONOPROXY='github.com/DefinedNet'
GONOSUMDB='github.com/DefinedNet'
GOOS='darwin'
GOPATH='/Users/jmaguire/go'
GOPRIVATE='github.com/DefinedNet'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.23.4/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.23.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/jmaguire/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/jmaguire/src/junkcode/read-deadline-empty-body/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/2t/rbxbv8612sq9_rdrxhtw8rzc0000gn/T/go-build893276654=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Issue two requests against a handler that sets r.Close = true: https://go.dev/play/p/1OVTsaO82br

What did you see happen?

Both requests used the same connection (see "Remote address" and "Close" flag on the client.)

What did you expect to see?

The server close the connection to the client per the Request docs:

// Close indicates whether to close the connection after
// replying to this request (for servers) or after sending this
// request and reading its response (for clients).

The docs do state the following:

// For server requests, the HTTP server handles this automatically
// and this field is not needed by Handlers.

The wording here ("not needed") implies that handlers can access it, but do not need to. In our case, we wanted to explicitly close the HTTP connection. Setting this flag did not achieve that. Setting r.Header().Set("Connection", "close") did.

@chad-bekmezian-snap
Copy link

I believe this is expected behavior, but poorly worded documentation

@seankhliao
Copy link
Member

The docs for http.Handler explicitly state that handlers should not modify the request. I don't think we need to reducument that for every field.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2024
@johnmaguire
Copy link
Author

Thanks @seankhliao - the phrasing "this field is not needed by Handlers" is what threw me off here. Something to the effect of "Changing this field from Handlers has no effect" might be more helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants