gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.76k stars 8.01k forks source link

create a goroutine within router func will report wrote more than declared Content-Length #2276

Open little-guy-lxr opened 4 years ago

little-guy-lxr commented 4 years ago

Description

How to reproduce

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

func Response(c gin.Context){ c.String(200, "Hello %s", c.Param("name")) } func main() { g := gin.Default() g.GET("/hello/:name", func(c gin.Context) { go Response(c) }) g.Run(":9000") }


## Expectations

$ curl http://localhost:8201/hello/world
Hello world

## Actual result

<!-- Actual result showing the problem -->

$ curl -i http://localhost:8201/hello/world

server core panic : wrote more than declared Content-Length ## Environment - go version: 1.22.5 - gin version (or commit ref): 094b3fdb393ff8eab16ec293bf49513213955523 - operating system:
linvis commented 4 years ago

Gin doesn't allow use original context in goroutine.

first, original context is not allowed second, context copy is only read-only

see more https://github.com/gin-gonic/gin#goroutines-inside-a-middleware