MM-52009: Fix hub data race (#22925)

After the monorepo integration, the boards product
was started before we started the hub.

This led to a data race where the GetHubForUserId was actually
called before the hub was initialized.

To fix this we initialize the platform first, and then the products.

https://mattermost.atlassian.net/browse/MM-52009
```release-note
NONE
```
This commit is contained in:
Agniva De Sarker 2023-04-13 19:39:16 +05:30 committed by GitHub
parent 35d3e3e91e
commit 574e61bcc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -260,8 +260,17 @@ func NewServer(options ...Option) (*Server, error) {
product.CommandKey: app,
}
// Step 4: Initialize products.
// Depends on s.httpService.
// It is important to initialize the hub only after the global logger is set
// to avoid race conditions while logging from inside the hub.
// Step 4: Start platform
s.platform.Start()
// NOTE: There should be no call to App.Srv().Channels() before step 5 is done
// otherwise it will throw a panic.
// Step 5: Initialize products.
// Depends on s.httpService, and depends on the hub to be initialized.
// Otherwise we run into race conditions.
err = s.initializeProducts(product.GetProducts(), serviceMap)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize products")
@ -275,11 +284,6 @@ func NewServer(options ...Option) (*Server, error) {
}
app.ch = channelsWrapper.app.ch
// It is important to initialize the hub only after the global logger is set
// to avoid race conditions while logging from inside the hub.
// Step 5: Start hub in platform which the hub depends on s.Channels() (step 4)
s.platform.Start()
// -------------------------------------------------------------------------
// Everything below this is not order sensitive and safe to be moved around.
// If you are adding a new field that is non-channels specific, please add