Skip to content

Commit

Permalink
Fix context.Copy() race condition (#1020)
Browse files Browse the repository at this point in the history
* Fix context.Copy race condition

* Update githubapi_test.go

* fix code format
  • Loading branch information
raphaelgavache authored and thinkerou committed Feb 26, 2019
1 parent 62749f0 commit e207a3c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (c *Context) Copy() *Context {
cp.Writer = &cp.writermem
cp.index = abortIndex
cp.handlers = nil
cp.Keys = map[string]interface{}{}
for k, v := range c.Keys {
cp.Keys[k] = v
}
return &cp
}

Expand Down
2 changes: 2 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ func TestContextCopy(t *testing.T) {
assert.Equal(t, cp.Keys, c.Keys)
assert.Equal(t, cp.engine, c.engine)
assert.Equal(t, cp.Params, c.Params)
cp.Set("foo", "notBar")
assert.False(t, cp.Keys["foo"] == c.Keys["foo"])
}

func TestContextHandlerName(t *testing.T) {
Expand Down
23 changes: 23 additions & 0 deletions githubapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,29 @@ func TestBindUriError(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w1.Code)
}

func TestRaceContextCopy(t *testing.T) {
DefaultWriter = os.Stdout
router := Default()
router.GET("/test/copy/race", func(c *Context) {
c.Set("1", 0)
c.Set("2", 0)

// Sending a copy of the Context to two separate routines
go readWriteKeys(c.Copy())
go readWriteKeys(c.Copy())
c.String(http.StatusOK, "run OK, no panics")
})
w := performRequest(router, "GET", "/test/copy/race")
assert.Equal(t, "run OK, no panics", w.Body.String())
}

func readWriteKeys(c *Context) {
for {
c.Set("1", rand.Int())
c.Set("2", c.Value("1"))
}
}

func githubConfigRouter(router *Engine) {
for _, route := range githubAPI {
router.Handle(route.method, route.path, func(c *Context) {
Expand Down

0 comments on commit e207a3c

Please sign in to comment.