From e7c8e50895bbd86ab77a8cece343c4c926bcafec Mon Sep 17 00:00:00 2001 From: barry3406 Date: Thu, 9 Apr 2026 06:10:21 -0700 Subject: [PATCH] fix: move Keys read inside RLock in Context.Copy() to prevent data race Copy() reads c.Keys before acquiring the read lock, then clones the stale reference under the lock. If another goroutine calls Set() between the read and the lock acquisition, the cKeys variable may reference a map being concurrently modified. This is particularly dangerous on the first Set() call where c.Keys transitions from nil to a new map allocation. --- context.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/context.go b/context.go index 5174033e..34a6e7ca 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