From 574e61bcc1eab2907b632a2bb4ecb22eb9d276c2 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Thu, 13 Apr 2023 19:39:16 +0530 Subject: [PATCH] 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 ``` --- server/channels/app/server.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/server/channels/app/server.go b/server/channels/app/server.go index 6197c6c55be..65c3dbf371e 100644 --- a/server/channels/app/server.go +++ b/server/channels/app/server.go @@ -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