MM-26571: Move the semaphore creation before hub start (#14938)

This avoids the race of assigning the sema field from the start method
which runs in a goroutine.

The race that happens is

==================

WARNING: DATA RACE
Write at 0x00c003a9d1b0 by goroutine 67:
  github.com/mattermost/mattermost-server/v5/app.(*PushNotificationsHub).start()
      /home/agniva/mattermost/mattermost-server/app/notification_push.go:264 +0x90

Previous read at 0x00c003a9d1b0 by goroutine 69:
  github.com/mattermost/mattermost-server/v5/app.(*Server).createPushNotificationsHub()
      /home/agniva/mattermost/mattermost-server/app/notification_push.go:260 +0x1d5
This commit is contained in:
Agniva De Sarker
2020-06-30 13:15:05 +05:30
committed by GitHub
parent f305cfe9ae
commit 08457860cc

View File

@@ -255,14 +255,13 @@ func (s *Server) createPushNotificationsHub() {
notificationsChan: make(chan PushNotification, buffer),
app: fakeApp,
wg: new(sync.WaitGroup),
sema: make(chan struct{}, runtime.NumCPU()*8), // numCPU * 8 is a good amount of concurrency.
}
go hub.start()
s.PushNotificationsHub = hub
}
func (hub *PushNotificationsHub) start() {
hub.sema = make(chan struct{}, runtime.NumCPU()*8) // numCPU * 8 is a good amount of concurrency.
for notification := range hub.notificationsChan {
// Adding to the waitgroup first.
hub.wg.Add(1)