From edddabf95c587ce907d020677a80f82e110329e3 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Tue, 7 Jan 2020 17:30:34 +0530 Subject: [PATCH] MM-19628: Fix race in (*registeredPlugin).State access (#13332) * MM-19628: Fix race in (*registeredPlugin).State access We used a pointer to an integer in the State variable, and therefore setting a pointer value directly caused a race condition. Since the registeredPlugin is a private struct, changing it to a normal int creates fewer pointers and lets us to fix the race condition by simply setting the integer value and then doing a registeredPlugins.Store. The alternative to keeping the pointer would be to create a copy of each member of a registeredPlugin and do a store again, which I feel is a roundabout way to achieve this. * Address review comments Co-authored-by: mattermod --- plugin/environment.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin/environment.go b/plugin/environment.go index 78e4f8f19d..38b0a6dd2a 100644 --- a/plugin/environment.go +++ b/plugin/environment.go @@ -29,7 +29,7 @@ type apiImplCreatorFunc func(*model.Manifest) API // plugin is configured as disabled and has not been activated during this server run. type registeredPlugin struct { BundleInfo *model.BundleInfo - State *int + State int failTimeStamps []time.Time lastError error @@ -114,13 +114,13 @@ func (env *Environment) GetPluginState(id string) int { return model.PluginStateNotRunning } - return *rp.(*registeredPlugin).State + return rp.(*registeredPlugin).State } // SetPluginState sets the current state of a plugin (disabled, running, or error) func (env *Environment) SetPluginState(id string, state int) { if rp, ok := env.registeredPlugins.Load(id); ok { - *rp.(*registeredPlugin).State = state + rp.(*registeredPlugin).State = state } } @@ -441,5 +441,5 @@ func (env *Environment) RunMultiPluginHook(hookRunnerFunc func(hooks Hooks) bool func newRegisteredPlugin(bundle *model.BundleInfo) *registeredPlugin { state := model.PluginStateNotRunning - return ®isteredPlugin{failTimeStamps: []time.Time{}, State: &state, BundleInfo: bundle} + return ®isteredPlugin{failTimeStamps: []time.Time{}, State: state, BundleInfo: bundle} }