1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-01 15:41:40 +03:00

Fix race condition in clusterNodes.Addrs() (#3219)

Resolve a race condition in the clusterNodes.Addrs() method.
Previously, the method returned a reference to a string slice, creating
the potential for concurrent reads by the caller while the slice was
being modified by the garbage collection process.

Co-authored-by: Nedyalko Dyakov <nedyalko.dyakov@gmail.com>
This commit is contained in:
Shawn Wang
2025-02-03 06:15:00 -08:00
committed by GitHub
parent 1139bc3aa9
commit d0fb810b13

View File

@ -487,9 +487,11 @@ func (c *clusterNodes) Addrs() ([]string, error) {
closed := c.closed //nolint:ifshort closed := c.closed //nolint:ifshort
if !closed { if !closed {
if len(c.activeAddrs) > 0 { if len(c.activeAddrs) > 0 {
addrs = c.activeAddrs addrs = make([]string, len(c.activeAddrs))
copy(addrs, c.activeAddrs)
} else { } else {
addrs = c.addrs addrs = make([]string, len(c.addrs))
copy(addrs, c.addrs)
} }
} }
c.mu.RUnlock() c.mu.RUnlock()