Skip to content
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
10 changes: 8 additions & 2 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,16 @@ func (c *PubSub) Ping(ctx context.Context, payload ...string) error {
}
cmd := NewCmd(ctx, args...)

cn, err := c.connWithLock(ctx)
c.mu.Lock()
defer c.mu.Unlock()

cn, err := c.conn(ctx, nil)
if err != nil {
return err
}

err = c.writeCmd(ctx, cn, cmd)
c.releaseConnWithLock(ctx, cn, err, false)
c.releaseConn(ctx, cn, err, false)
return err
}

Expand Down Expand Up @@ -361,6 +364,8 @@ func (c *PubSub) ReceiveTimeout(ctx context.Context, timeout time.Duration) (int
c.cmd = NewCmd(ctx)
}

// Don't hold the lock to allow subscriptions and pings.

cn, err := c.connWithLock(ctx)
if err != nil {
return nil, err
Expand All @@ -371,6 +376,7 @@ func (c *PubSub) ReceiveTimeout(ctx context.Context, timeout time.Duration) (int
})

c.releaseConnWithLock(ctx, cn, err, timeout > 0)

if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,18 @@ var _ = Describe("cluster races", func() {
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(int64(C * N)))
})

It("write cmd data-race", func() {
pubsub := client.Subscribe(ctx)
defer pubsub.Close()

pubsub.Channel(redis.WithChannelHealthCheckInterval(time.Millisecond))
for i := 0; i < 100; i++ {
key := fmt.Sprintf("channel_%d", i)
pubsub.Subscribe(ctx, key)
pubsub.Unsubscribe(ctx, key)
}
})
})

func bigVal() []byte {
Expand Down