Skip to content
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

fix: fix panic when redis client closed before server closed #638

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 6 additions & 1 deletion subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func newSubscriber(params subscriberParams) *subscriber {
func (s *subscriber) shutdown() {
s.logger.Debug("Subscriber shutting down...")
// Signal the subscriber goroutine to stop.
s.done <- struct{}{}
close(s.done)
}

func (s *subscriber) start(wg *sync.WaitGroup) {
Expand Down Expand Up @@ -80,6 +80,11 @@ func (s *subscriber) start(wg *sync.WaitGroup) {
s.logger.Debug("Subscriber done")
return
case msg := <-cancelCh:
if msg == nil {
s.logger.Warn("channel closed")
pubsub.Close()
return
}
cancel, ok := s.cancelations.Get(msg.Payload)
if ok {
cancel()
Expand Down
32 changes: 32 additions & 0 deletions subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ func TestSubscriber(t *testing.T) {
}
}

func TestSubscriberWithRedisClientClosed(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("panic occurred: %v", r)
}
}()
redisClient := setup(t)
r := rdb.NewRDB(redisClient)
defer r.Close()
testBroker := testbroker.NewTestBroker(r)

cancelations := base.NewCancelations()
subscriber := newSubscriber(subscriberParams{
logger: testLogger,
broker: testBroker,
cancelations: cancelations,
})
subscriber.retryTimeout = 1 * time.Second // set shorter retry timeout for testing purpose.

var wg sync.WaitGroup
subscriber.start(&wg)
defer subscriber.shutdown()

time.Sleep(5 * time.Second) // allow some time for subscriber to connect

if err := redisClient.Close(); err != nil {
t.Fatalf("close redis client,%+v", err)
}

time.Sleep(1 * time.Second) // make sure client is closed
}

func TestSubscriberWithRedisDown(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand Down