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: request.Body.Close() can be called after RoundTrip() returns #58521

Closed
soslanco opened this issue Feb 14, 2023 · 5 comments
Closed

Comments

@soslanco
Copy link

soslanco commented Feb 14, 2023

What version of Go are you using (go version)?

$ go version
go version go1.20 linux/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/usr/local/godev/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/usr/local/godev"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/usr/local/godev/src/bench/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 -fdebug-prefix-map=/tmp/go-build159046709=/tmp/go-build -gno-record-gcc-switches"
GOROOT/bin/go version: go version go1.20 linux/amd64
GOROOT/bin/go tool compile -V: compile version go1.20
uname -sr: Linux 5.14.0-162.12.1.el9_1.x86_64
/lib64/libc.so.6: GNU C Library (GNU libc) stable release version 2.34.
gdb --version: GNU gdb (GDB) Red Hat Enterprise Linux 10.2-10.el9

What did you do?

rd := &bytes.Reader{}
body := make([]byte, 1048576)
for i := 0; i < 1000; i++ {
	rd.Reset(body)
	_, err := http.Post("http://127.0.0.1:8080", "application/octet-stream", rd)
	if err != nil {
		log.Fatalf("[%d] %v", i, err)
	}
}

What did you expect to see?

reusing without errors

What did you see instead?

2023/02/14 14:52:48 [7] Post "http://127.0.0.1:12345": net/http: HTTP/1.x transport connection broken: http: ContentLength=1048576 with Body length 1015808

@soslanco soslanco changed the title reusing bytes.Reader and byte slice for the http requests problem net/http: reusing bytes.Reader and byte slice problem Feb 14, 2023
@seankhliao
Copy link
Member

what's your full test code?
transport connection broken doesn't point to an error with the post contents

@soslanco
Copy link
Author

soslanco commented Feb 14, 2023

It's full code from main function. In another hand just web server handling post requests on 8080 port.

package main

import (
	"bytes"
	"log"
	"net/http"
)

func main() {
	rd := &bytes.Reader{}
	body := make([]byte, 1048576)
	for i := 0; i < 1000; i++ {
		rd.Reset(body)
		_, err := http.Post("http://127.0.0.1:8080", "application/octet-stream", rd)
		if err != nil {
			log.Fatalf("[%d] %v", i, err)
		}
	}
}

@seankhliao
Copy link
Member

That's not enough to reproduce the issue, it may be that your server is breaking the connection

@soslanco
Copy link
Author

soslanco commented Feb 14, 2023

You may run for example nginx and test previous code:
docker run --rm -p 8080:80 nginx

or use something like that

package main

import (
	"io"
	"log"
	"net/http"
)

func main() {
	helloHandler := func(w http.ResponseWriter, req *http.Request) {
		io.WriteString(w, "Hello, world!\n")
	}
	http.HandleFunc("/", helloHandler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

But if we allocate variables for every request then code works fine:

for i := 0; i < 1000; i++ {
	body := make([]byte, 1048576)
	rd := bytes.NewReader(body)
	_, err := http.Post("http://127.0.0.1:8080", "application/octet-stream", rd)
	if err != nil {
		log.Fatalf("[%d] %v", i, err)
	}
}

@seankhliao seankhliao changed the title net/http: reusing bytes.Reader and byte slice problem net/http: request.Body.Close() can be called after RoundTrip() returns Feb 14, 2023
@seankhliao
Copy link
Member

Duplicate of #51907

@seankhliao seankhliao marked this as a duplicate of #51907 Feb 14, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2023
@golang golang locked and limited conversation to collaborators Feb 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants