[MM-52550] platform/web_conn: fix a possible data race issue (#23255)

* platform/web_conn: fix a possible data race issue

* reflect review comments

---------

Co-authored-by: Ibrahim Serdar Acikgoz <ibrahim@ibrahims-mac.local>
Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Ibrahim Serdar Acikgoz 2023-05-05 11:01:18 +03:00 committed by GitHub
parent d860548a76
commit d030f681eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -171,10 +171,12 @@ func (ps *PlatformService) PopulateWebConnConfig(s *model.Session, cfg *WebConnC
// NewWebConn returns a new WebConn instance. // NewWebConn returns a new WebConn instance.
func (ps *PlatformService) NewWebConn(cfg *WebConnConfig, suite SuiteIFace, runner HookRunner) *WebConn { func (ps *PlatformService) NewWebConn(cfg *WebConnConfig, suite SuiteIFace, runner HookRunner) *WebConn {
userID := cfg.Session.UserId
session := cfg.Session
if cfg.Session.UserId != "" { if cfg.Session.UserId != "" {
ps.Go(func() { ps.Go(func() {
ps.SetStatusOnline(cfg.Session.UserId, false) ps.SetStatusOnline(userID, false)
ps.UpdateLastActivityAtIfNeeded(cfg.Session) ps.UpdateLastActivityAtIfNeeded(session)
}) })
} }
@ -232,9 +234,9 @@ func (ps *PlatformService) NewWebConn(cfg *WebConnConfig, suite SuiteIFace, runn
wc.SetSessionExpiresAt(cfg.Session.ExpiresAt) wc.SetSessionExpiresAt(cfg.Session.ExpiresAt)
wc.SetConnectionID(cfg.ConnectionID) wc.SetConnectionID(cfg.ConnectionID)
wc.Platform.Go(func() { ps.Go(func() {
wc.HookRunner.RunMultiHook(func(hooks plugin.Hooks) bool { runner.RunMultiHook(func(hooks plugin.Hooks) bool {
hooks.OnWebSocketConnect(wc.GetConnectionID(), wc.UserId) hooks.OnWebSocketConnect(wc.GetConnectionID(), userID)
return true return true
}, plugin.OnWebSocketConnectID) }, plugin.OnWebSocketConnectID)
}) })
@ -334,9 +336,10 @@ func (wc *WebConn) Pump() {
wc.Platform.HubUnregister(wc) wc.Platform.HubUnregister(wc)
close(wc.pumpFinished) close(wc.pumpFinished)
userID := wc.UserId
wc.Platform.Go(func() { wc.Platform.Go(func() {
wc.HookRunner.RunMultiHook(func(hooks plugin.Hooks) bool { wc.HookRunner.RunMultiHook(func(hooks plugin.Hooks) bool {
hooks.OnWebSocketDisconnect(wc.GetConnectionID(), wc.UserId) hooks.OnWebSocketDisconnect(wc.GetConnectionID(), userID)
return true return true
}, plugin.OnWebSocketDisconnectID) }, plugin.OnWebSocketDisconnectID)
}) })
@ -353,8 +356,9 @@ func (wc *WebConn) readPump() {
return err return err
} }
if wc.IsAuthenticated() { if wc.IsAuthenticated() {
userID := wc.UserId
wc.Platform.Go(func() { wc.Platform.Go(func() {
wc.Platform.SetStatusAwayIfNeeded(wc.UserId, false) wc.Platform.SetStatusAwayIfNeeded(userID, false)
}) })
} }
return nil return nil

View File

@ -436,8 +436,9 @@ func (h *Hub) Start() {
conns := connIndex.ForUser(webConn.UserId) conns := connIndex.ForUser(webConn.UserId)
if len(conns) == 0 || areAllInactive(conns) { if len(conns) == 0 || areAllInactive(conns) {
userID := webConn.UserId
h.platform.Go(func() { h.platform.Go(func() {
h.platform.SetStatusOffline(webConn.UserId, false) h.platform.SetStatusOffline(userID, false)
}) })
continue continue
} }
@ -452,8 +453,9 @@ func (h *Hub) Start() {
} }
if h.platform.isUserAway(latestActivity) { if h.platform.isUserAway(latestActivity) {
userID := webConn.UserId
h.platform.Go(func() { h.platform.Go(func() {
h.platform.SetStatusLastActivityAt(webConn.UserId, latestActivity) h.platform.SetStatusLastActivityAt(userID, latestActivity)
}) })
} }
case userID := <-h.invalidateUser: case userID := <-h.invalidateUser: