Skip to content

go-redis/v8 nil shard in the shards map #2126

@alemrtv

Description

@alemrtv

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions