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.
This commit is contained in:
barry3406 2026-04-09 06:10:21 -07:00
parent d3ffc99852
commit e7c8e50895

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