From 13bca933d9304e18b0491a631cad33e25d42d529 Mon Sep 17 00:00:00 2001 From: Samiul Sk <9149283+kernelshard@users.noreply.github.com> Date: Sat, 25 Jul 2026 22:03:28 +0530 Subject: [PATCH] fix(context): fix data race on c.Keys in Copy --- context.go | 3 +-- context_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 1dc730e3..62281f3d 100644 --- a/context.go +++ b/context.go @@ -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 diff --git a/context_test.go b/context_test.go index e8d305e4..68c474cc 100644 --- a/context_test.go +++ b/context_test.go @@ -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}