fix(sentinel): close non-winning sentinel clients in MasterAddr concurrent probe (#3827)

In MasterAddr, multiple goroutines probe sentinels concurrently. The first
to succeed enters sync.Once and saves its SentinelClient via setSentinel.
Other goroutines that also succeeded but lost the once.Do race doesn't
closed its SentinelClient — a resource leak.

After once.Do returns, close the sentinelCli if it's not the winner.
sync.Once guarantees all goroutines see c.sentinel written by the winner.
This commit is contained in:
cxljs
2026-05-27 12:37:02 +03:00
committed by GitHub
parent 00bf6d3edc
commit 68a8bc1e84
+7 -2
View File
@@ -954,6 +954,7 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
errCh <- err
return
}
once.Do(func() {
masterAddr = net.JoinHostPort(addrVal[0], addrVal[1])
// Push working sentinel to the top
@@ -962,6 +963,10 @@ func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
internal.Logger.Printf(ctx, "sentinel: selected addr=%s masterAddr=%s", addr, masterAddr)
cancel()
})
if sentinelCli != c.sentinel {
_ = sentinelCli.Close()
}
}(i, sentinelAddr)
}
@@ -1045,9 +1050,9 @@ func (c *sentinelFailover) replicaAddrs(ctx context.Context, useDisconnected boo
}
if sentinelReachable {
return []string{}, nil
return nil, nil
}
return []string{}, errors.New("redis: all sentinels specified in configuration are unreachable")
return nil, errors.New("redis: all sentinels specified in configuration are unreachable")
}
func (c *sentinelFailover) getMasterAddr(ctx context.Context, sentinel *SentinelClient) (string, error) {