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

Request body closed in copied context #2575

Open
kenticny opened this issue Dec 6, 2020 · 1 comment
Open

Request body closed in copied context #2575

kenticny opened this issue Dec 6, 2020 · 1 comment

Comments

@kenticny
Copy link

kenticny commented Dec 6, 2020

Description

I try to do some works with goroutine in handler as the demo, and when I read the request body from copied context in goroutine, the reader always closed. the error detail is http: invalid Read on closed Body

How to reproduce

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	g := gin.Default()
	g.POST("/dosomething", func(c *gin.Context) {
		cCp := c.Copy()
                go func() {
                    var err error
		    var buf []byte
		    if buf, err = ioutil.ReadAll(cCp.Request.Body); err != nil {
                        log.Println(err)
			return
		    }
                    fmt.Println(string(buf))
                }()
	})
	g.Run(":9000")
}

Expectations

$ curl -XPOST -d '...some json data...' http://localhost:9000/dosomething

...json data...

Actual result

Show in stdout:

http: invalid Read on closed Body

Environment

  • go version: go1.14.4 darwin/amd64
  • gin version (or commit ref): v1.6.3
  • operating system: MacOS 10.14.5
@yeqown
Copy link

yeqown commented Jan 22, 2021

your handler runs a new goroutine, so the handler finished then it called finishedRequest which would close the request body.

https://golang.org/src/net/http/server.go?s=89579:89620#L1930
https://golang.org/src/net/http/server.go?s=89579:89620#L1613

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

2 participants