Redis: use v8 with context=nil panic

Created on 24 May 2020  路  3Comments  路  Source: go-redis/redis

ENV

  • golang: go version go1.14.2 darwin/amd64
  • OS: macOS Catalina Version 10.15.4
  • redis-server: Redis server v=5.0.5 sha=00000000:0 malloc=libc bits=64 build=1560ae4862f87ae
  • go-redis: v8

code:

package main

import (
    "fmt"
    "github.com/go-redis/redis/v8"
)

func main() {
    rdb := redis.NewClient(&redis.Options{
        Addr:     "localhost:6380", // use default Addr
        Password: "turing",         // no password set
        DB:       0,                // use default DB
    })
    pong, err := rdb.Ping(nil).Result()
    fmt.Println(pong, err)
}

output:

Some sensitive information has been replaced

GOROOT=${GOROOT} #gosetup
GOPATH=/Users/Turing/Desktop/project/github/go #gosetup
${GOROOT}/bin/go build -o /private/var/folders/yf/fl99q50s69748cpgv23kqc6r0000gn/T/___go_build_main_go_darwin ${PROJECT_DIR}/main.go #gosetup
/private/var/folders/yf/fl99q50s69748cpgv23kqc6r0000gn/T/___go_build_main_go_darwin #gosetup
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x12063cc]

goroutine 1 [running]:
go.opentelemetry.io/otel/api/trace.SpanFromContext(...)
    /${GOPATH}pkg/mod/go.opentelemetry.io/[email protected]/api/trace/context.go:36
github.com/go-redis/redis/v8/internal.WithSpan(0x0, 0x0, 0x132cf65, 0x7, 0xc0000e1c88, 0x0, 0x0)
    /${GOPATH}pkg/mod/github.com/go-redis/redis/[email protected]/internal/util.go:63 +0x5c
github.com/go-redis/redis/v8.(*baseClient)._process(0xc0000a83e0, 0x0, 0x0, 0x13892e0, 0xc0000bc190, 0x400, 0x203000)
    /${GOPATH}pkg/mod/github.com/go-redis/redis/[email protected]/redis.go:321 +0xe2
github.com/go-redis/redis/v8.(*baseClient).process(0xc0000a83e0, 0x0, 0x0, 0x13892e0, 0xc0000bc190, 0x1646518, 0xc0000e1df0)
    /${GOPATH}pkg/mod/github.com/go-redis/redis/[email protected]/redis.go:307 +0x57
github.com/go-redis/redis/v8.hooks.process(0x0, 0x0, 0x0, 0x0, 0x0, 0x13892e0, 0xc0000bc190, 0xc0000e1e50, 0x157eae0, 0x163a7d0)
    /${GOPATH}pkg/mod/github.com/go-redis/redis/[email protected]/redis.go:57 +0xeb
github.com/go-redis/redis/v8.(*Client).Process(0xc0000d6140, 0x0, 0x0, 0x13892e0, 0xc0000bc190, 0x1, 0xc0000bc190)
    /${GOPATH}pkg/mod/github.com/go-redis/redis/[email protected]/redis.go:599 +0x9c
github.com/go-redis/redis/v8.cmdable.Ping(0xc000098910, 0x0, 0x0, 0x1041f7a)
    /${GOPATH}pkg/mod/github.com/go-redis/redis/[email protected]/commands.go:399 +0xca
main.main()
    /${PROJECT_DIR}/main.go:14 +0x87

Process finished with exit code 2

Can you support an example using go-redis v8 without context or context is nil?

Most helpful comment

Actually, at least for me, it looks really odd to pass a context argument whenever you issue a redis command. Should not this be something to be reviewed?

All 3 comments

Now I has back to v7 with no ctx Context parameter

Passing nil instead of context.Context is considered an anti-pattern in Go and some linters will warn you. Instead you can define some global variable and pass it instead nil:

var ctx = context.TODO()

func some() {
    rdb.Ping(ctx)
}

Actually, at least for me, it looks really odd to pass a context argument whenever you issue a redis command. Should not this be something to be reviewed?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aahoughton picture aahoughton  路  5Comments

youcandoit95 picture youcandoit95  路  3Comments

mollylogue picture mollylogue  路  4Comments

ihsw picture ihsw  路  5Comments

mathvav picture mathvav  路  3Comments