From d0fb810b13dba5c653a9fab136c497c6349a1159 Mon Sep 17 00:00:00 2001 From: Shawn Wang <62313353+shawnwgit@users.noreply.github.com> Date: Mon, 3 Feb 2025 06:15:00 -0800 Subject: [PATCH] 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 --- osscluster.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/osscluster.go b/osscluster.go index 72e922a8..188f5035 100644 --- a/osscluster.go +++ b/osscluster.go @@ -487,9 +487,11 @@ func (c *clusterNodes) Addrs() ([]string, error) { closed := c.closed //nolint:ifshort if !closed { if len(c.activeAddrs) > 0 { - addrs = c.activeAddrs + addrs = make([]string, len(c.activeAddrs)) + copy(addrs, c.activeAddrs) } else { - addrs = c.addrs + addrs = make([]string, len(c.addrs)) + copy(addrs, c.addrs) } } c.mu.RUnlock()