-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Closed
Labels
Description
Expected Behavior
no panic due to nil pointer dereference after retrieving a nil shard from the shards map
Current Behavior
There is no check if a shard is not nil before returning it.
It might cause panic due to nil pointer dereference here
Possible Solution
Check if shard is not nil before returning it.
Steps to Reproduce
We have a proprietary code which wraps go-redis/go
and sometimes recreates the Ring structure (and hence repopulating the shards map), all while holding the mutex.
Despite it, sometimes after the ring is recreated, we get panic in the place linked above because the shard retrieved by the name is nil.
Possible Implementation
func (c *ringShards) GetByName(shardName string) (*ringShard, error) {
if shardName == "" {
return c.Random()
}
c.mu.RLock()
shard := c.shards[shardName]
c.mu.RUnlock()
if shard == nil {
return nil, fmt.Errorf("a shard named %q is nil", shardName)
}
return shard, nil
}