mirror of
https://github.com/gin-gonic/gin.git
synced 2026-09-04 22:53:34 +08:00
fix(context): fix data race on c.Keys in Copy
This commit is contained in:
parent
34dac209ff
commit
13bca933d9
@ -132,9 +132,8 @@ func (c *Context) Copy() *Context {
|
||||
cp.handlers = nil
|
||||
cp.fullPath = c.fullPath
|
||||
|
||||
cKeys := c.Keys
|
||||
c.mu.RLock()
|
||||
cp.Keys = maps.Clone(cKeys)
|
||||
cp.Keys = maps.Clone(c.Keys)
|
||||
c.mu.RUnlock()
|
||||
|
||||
cParams := c.Params
|
||||
|
||||
@ -776,6 +776,29 @@ func TestContextCopyNilErrorsAndAccepted(t *testing.T) {
|
||||
assert.Nil(t, cp.Accepted)
|
||||
}
|
||||
|
||||
func TestContextCopyRace(t *testing.T) {
|
||||
c := &Context{}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for range 1000 {
|
||||
c.Set("foo", "bar")
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for range 1000 {
|
||||
c.Copy()
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestContextHandlerName(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user