fix(context): fix data race on c.Keys in Copy

This commit is contained in:
Samiul Sk 2026-07-25 22:03:28 +05:30
parent 34dac209ff
commit 13bca933d9
2 changed files with 24 additions and 2 deletions

View File

@ -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

View File

@ -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}