Skip to content

Commit

Permalink
Merge pull request #3 from boreq/closed-mutex
Browse files Browse the repository at this point in the history
Fix a data race in the close method
  • Loading branch information
czeslavo authored Feb 5, 2020
2 parents a50d137 + 8436e6a commit 3059fcb
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/firestore/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type Subscriber struct {
closed bool
closing chan struct{}
allSubscriptionsWaitingGroup sync.WaitGroup

closedMutex sync.Mutex
}

func NewSubscriber(config SubscriberConfig, logger watermill.LoggerAdapter) (*Subscriber, error) {
Expand All @@ -97,10 +99,15 @@ func NewSubscriber(config SubscriberConfig, logger watermill.LoggerAdapter) (*Su
}

func (s *Subscriber) Subscribe(ctx context.Context, topic string) (<-chan *message.Message, error) {
s.closedMutex.Lock()

if s.closed {
s.closedMutex.Unlock()
return nil, errors.New("subscriber is closed")
}

s.closedMutex.Unlock()

ctx, cancel := context.WithCancel(ctx)

subscriptionName := s.config.GenerateSubscriptionName(topic)
Expand Down Expand Up @@ -155,6 +162,9 @@ func (s *Subscriber) subscriptionCollection(topic string) *firestore.CollectionR
}

func (s *Subscriber) Close() error {
s.closedMutex.Lock()
defer s.closedMutex.Unlock()

if s.closed {
return nil
}
Expand Down

0 comments on commit 3059fcb

Please sign in to comment.