diff --git a/context.go b/context.go index 5dc7f8a0..5d7c83c3 100644 --- a/context.go +++ b/context.go @@ -87,6 +87,9 @@ func (c *Context) Copy() *Context { for k, v := range c.Keys { cp.Keys[k] = v } + paramCopy := make([]Param, len(cp.Params)) + copy(paramCopy, cp.Params) + cp.Params = paramCopy return &cp } diff --git a/context_test.go b/context_test.go index 0da5fbe6..1044265c 100644 --- a/context_test.go +++ b/context_test.go @@ -13,6 +13,7 @@ import ( "mime/multipart" "net/http" "net/http/httptest" + "os" "reflect" "strings" "testing" @@ -1812,3 +1813,20 @@ func TestContextResetInHandler(t *testing.T) { 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) +}