Skip to content

Fix Data Race in hooksMixin When Using MinIdleConns and AddHook #2814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -40,12 +41,15 @@ type (
)

type hooksMixin struct {
hooksMu *sync.Mutex

slice []Hook
initial hooks
current hooks
}

func (hs *hooksMixin) initHooks(hooks hooks) {
hs.hooksMu = new(sync.Mutex)
hs.initial = hooks
hs.chain()
}
Expand Down Expand Up @@ -116,6 +120,9 @@ func (hs *hooksMixin) AddHook(hook Hook) {
func (hs *hooksMixin) chain() {
hs.initial.setDefaults()

hs.hooksMu.Lock()
defer hs.hooksMu.Unlock()

hs.current.dial = hs.initial.dial
hs.current.process = hs.initial.process
hs.current.pipeline = hs.initial.pipeline
Expand All @@ -138,9 +145,13 @@ func (hs *hooksMixin) chain() {
}

func (hs *hooksMixin) clone() hooksMixin {
hs.hooksMu.Lock()
defer hs.hooksMu.Unlock()

clone := *hs
l := len(clone.slice)
clone.slice = clone.slice[:l:l]
clone.hooksMu = new(sync.Mutex)
return clone
}

Expand All @@ -165,6 +176,8 @@ func (hs *hooksMixin) withProcessPipelineHook(
}

func (hs *hooksMixin) dialHook(ctx context.Context, network, addr string) (net.Conn, error) {
hs.hooksMu.Lock()
defer hs.hooksMu.Unlock()
return hs.current.dial(ctx, network, addr)
}

Expand Down
50 changes: 50 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,53 @@ var _ = Describe("Hook", func() {
Expect(cmd.Val()).To(Equal("Script and hook"))
})
})

var _ = Describe("Hook with MinIdleConns", func() {
var client *redis.Client

BeforeEach(func() {
options := redisOptions()
options.MinIdleConns = 1
client = redis.NewClient(options)
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})

AfterEach(func() {
err := client.Close()
Expect(err).NotTo(HaveOccurred())
})

It("fifo", func() {
var res []string
client.AddHook(&hook{
processHook: func(hook redis.ProcessHook) redis.ProcessHook {
return func(ctx context.Context, cmd redis.Cmder) error {
res = append(res, "hook-1-process-start")
err := hook(ctx, cmd)
res = append(res, "hook-1-process-end")
return err
}
},
})
client.AddHook(&hook{
processHook: func(hook redis.ProcessHook) redis.ProcessHook {
return func(ctx context.Context, cmd redis.Cmder) error {
res = append(res, "hook-2-process-start")
err := hook(ctx, cmd)
res = append(res, "hook-2-process-end")
return err
}
},
})

err := client.Ping(ctx).Err()
Expect(err).NotTo(HaveOccurred())

Expect(res).To(Equal([]string{
"hook-1-process-start",
"hook-2-process-start",
"hook-2-process-end",
"hook-1-process-end",
}))
})
})