From f7af80860cf99ce808834260ac190a2a88bc24e2 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 18 Oct 2016 17:34:09 -0700 Subject: [PATCH] prevent data race in pkg/plugins Signed-off-by: Victor Vieux --- pkg/plugins/plugins.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/pkg/plugins/plugins.go b/pkg/plugins/plugins.go index 5acd3f2fbb..432c47d8e5 100644 --- a/pkg/plugins/plugins.go +++ b/pkg/plugins/plugins.go @@ -41,9 +41,14 @@ type plugins struct { plugins map[string]*Plugin } +type extpointHandlers struct { + sync.RWMutex + extpointHandlers map[string][]func(string, *Client) +} + var ( - storage = plugins{plugins: make(map[string]*Plugin)} - extpointHandlers = make(map[string][]func(string, *Client)) + storage = plugins{plugins: make(map[string]*Plugin)} + handlers = extpointHandlers{extpointHandlers: make(map[string][]func(string, *Client))} ) // Manifest lists what a plugin implements. @@ -128,15 +133,17 @@ func (p *Plugin) activateWithLock() error { p.Manifest = m + handlers.RLock() for _, iface := range m.Implements { - handlers, handled := extpointHandlers[iface] + hdlrs, handled := handlers.extpointHandlers[iface] if !handled { continue } - for _, handler := range handlers { + for _, handler := range hdlrs { handler(p.name, p.client) } } + handlers.RUnlock() return nil } @@ -228,16 +235,18 @@ func Get(name, imp string) (*Plugin, error) { // Handle adds the specified function to the extpointHandlers. func Handle(iface string, fn func(string, *Client)) { - handlers, ok := extpointHandlers[iface] + handlers.Lock() + hdlrs, ok := handlers.extpointHandlers[iface] if !ok { - handlers = []func(string, *Client){} + hdlrs = []func(string, *Client){} } - handlers = append(handlers, fn) - extpointHandlers[iface] = handlers + hdlrs = append(hdlrs, fn) + handlers.extpointHandlers[iface] = hdlrs for _, p := range storage.plugins { p.activated = false } + handlers.Unlock() } // GetAll returns all the plugins for the specified implementation