redis / rueidis

A fast Golang Redis client that supports Client Side Caching, Auto Pipelining, Generics OM, RedisJSON, RedisBloom, RediSearch, etc.
Apache License 2.0
2.46k stars 158 forks source link

v1.0.20 introduced occasional panic when connecting to non-available Redis Cluster #543

Closed FZambia closed 6 months ago

FZambia commented 6 months ago

Hello @rueian !

Found a regression in rueidis while testing centrifuge. It triggers a panic when initializing rueidis.Client while Redis Cluster is not available. Before v1.0.20 there was no panic.

To reproduce, add test like this:

func TestConnectToNonAvailableCluster(t *testing.T) {
    _, err := NewClient(ClientOption{
        InitAddress: []string{"127.0.0.1:3000", "127.0.0.1:3001", "127.0.0.1:3002"},
    })
    if err == nil {
        t.Fatal("expected connect error")
    }
}

Do not run Redis Cluster at all (!)

Then with the help of stress tool:

go test -c
stress ./rueidis.test -test.run TestConnectToNonAvailableCluster

You should quickly see panic like this:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1009ec070]

goroutine 52 [running]:
github.com/redis/rueidis.(*clusterClient).lazyRefresh(...)
    /Users/alexander.emelin/centrifugal/rueidis/cluster.go:174
github.com/redis/rueidis.newClusterClient.func1.1({0x0?, 0x0?})
    /Users/alexander.emelin/centrifugal/rueidis/cluster.go:118 +0x60
github.com/redis/rueidis.(*mux)._pipe(0x140000ce790, 0x0)
    /Users/alexander.emelin/centrifugal/rueidis/mux.go:173 +0x47c
github.com/redis/rueidis.(*mux).pipe(...)
    /Users/alexander.emelin/centrifugal/rueidis/mux.go:191
github.com/redis/rueidis.(*mux).Version(0x0?)
    /Users/alexander.emelin/centrifugal/rueidis/mux.go:205 +0x20
github.com/redis/rueidis.getClusterSlots({0x100c9a878, 0x140000ce790})
    /Users/alexander.emelin/centrifugal/rueidis/cluster.go:191 +0x4c
github.com/redis/rueidis.(*clusterClient)._refresh.func1({0x100c9a878?, 0x140000ce790?})
    /Users/alexander.emelin/centrifugal/rueidis/cluster.go:216 +0x34
created by github.com/redis/rueidis.(*clusterClient)._refresh in goroutine 34
    /Users/alexander.emelin/centrifugal/rueidis/cluster.go:215 +0x12c8

Could you please take a look?

rueian commented 6 months ago

Hi @FZambia,

Wow, that's critical. Thank you so much for finding this. The fix will be merged soon.

FZambia commented 6 months ago

Many thanks!