Fix context.Params race condition on Copy()

Using context.Param(key) on a context.Copy inside a goroutine
may lead to incorrect value on a high load, where another request
overwrite a Param
This commit is contained in:
Samuel Abreu 2019-04-03 11:47:47 -03:00
parent 2e915f4e50
commit 8709d894ef
2 changed files with 21 additions and 0 deletions

View File

@ -87,6 +87,9 @@ func (c *Context) Copy() *Context {
for k, v := range c.Keys { for k, v := range c.Keys {
cp.Keys[k] = v cp.Keys[k] = v
} }
paramCopy := make([]Param, len(cp.Params))
copy(paramCopy, cp.Params)
cp.Params = paramCopy
return &cp return &cp
} }

View File

@ -13,6 +13,7 @@ import (
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -1812,3 +1813,20 @@ func TestContextResetInHandler(t *testing.T) {
c.Next() c.Next()
}) })
} }
func TestRaceParamsContextCopy(t *testing.T) {
DefaultWriter = os.Stdout
router := Default()
nameGroup := router.Group("/:name")
{
nameGroup.GET("/api", func(c *Context) {
go func(c *Context, param string) {
time.Sleep(100 * time.Millisecond)
assert.Equal(t, c.Param("name"), param)
}(c.Copy(), c.Param("name"))
})
}
performRequest(router, "GET", "/name1/api")
performRequest(router, "GET", "/name2/api")
time.Sleep(200 * time.Millisecond)
}