Skip to content

Commit f7af808

Browse files
committed
prevent data race in pkg/plugins
Signed-off-by: Victor Vieux <[email protected]>
1 parent 392b816 commit f7af808

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

pkg/plugins/plugins.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@ type plugins struct {
4141
plugins map[string]*Plugin
4242
}
4343

44+
type extpointHandlers struct {
45+
sync.RWMutex
46+
extpointHandlers map[string][]func(string, *Client)
47+
}
48+
4449
var (
45-
storage = plugins{plugins: make(map[string]*Plugin)}
46-
extpointHandlers = make(map[string][]func(string, *Client))
50+
storage = plugins{plugins: make(map[string]*Plugin)}
51+
handlers = extpointHandlers{extpointHandlers: make(map[string][]func(string, *Client))}
4752
)
4853

4954
// Manifest lists what a plugin implements.
@@ -128,15 +133,17 @@ func (p *Plugin) activateWithLock() error {
128133

129134
p.Manifest = m
130135

136+
handlers.RLock()
131137
for _, iface := range m.Implements {
132-
handlers, handled := extpointHandlers[iface]
138+
hdlrs, handled := handlers.extpointHandlers[iface]
133139
if !handled {
134140
continue
135141
}
136-
for _, handler := range handlers {
142+
for _, handler := range hdlrs {
137143
handler(p.name, p.client)
138144
}
139145
}
146+
handlers.RUnlock()
140147
return nil
141148
}
142149

@@ -228,16 +235,18 @@ func Get(name, imp string) (*Plugin, error) {
228235

229236
// Handle adds the specified function to the extpointHandlers.
230237
func Handle(iface string, fn func(string, *Client)) {
231-
handlers, ok := extpointHandlers[iface]
238+
handlers.Lock()
239+
hdlrs, ok := handlers.extpointHandlers[iface]
232240
if !ok {
233-
handlers = []func(string, *Client){}
241+
hdlrs = []func(string, *Client){}
234242
}
235243

236-
handlers = append(handlers, fn)
237-
extpointHandlers[iface] = handlers
244+
hdlrs = append(hdlrs, fn)
245+
handlers.extpointHandlers[iface] = hdlrs
238246
for _, p := range storage.plugins {
239247
p.activated = false
240248
}
249+
handlers.Unlock()
241250
}
242251

243252
// GetAll returns all the plugins for the specified implementation

0 commit comments

Comments
 (0)