@cryptotaxi247 / kubo / commits / 99fdaa1b4

plugin: fix non-deterministic loading order

fix #9909

Michael Muré committed May 31, 2023 at 17:49 UTC 99fdaa1b4dc46e369a72989d2e1b3a6bfbf1a11c
1 file changed +16 -11
plugin/loader/loader.go
+16 -11
@@ -87,7 +87,7 @@ func (ls loaderState) String() string {
87 // 5. Call Close to close all plugins.
88 type PluginLoader struct {
89 state loaderState
90 - plugins map[string]plugin.Plugin
90 + plugins []plugin.Plugin
91 started []plugin.Plugin
92 config config.Plugins
93 repo string
@@ -95,7 +95,7 @@ type PluginLoader struct {
95
96 // NewPluginLoader creates new plugin loader
97 func NewPluginLoader(repo string) (*PluginLoader, error) {
98 - loader := &PluginLoader{plugins: make(map[string]plugin.Plugin, len(preloadPlugins)), repo: repo}
98 + loader := &PluginLoader{plugins: make([]plugin.Plugin, 0, len(preloadPlugins)), repo: repo}
99 if repo != "" {
100 cfg, err := cserialize.Load(filepath.Join(repo, config.DefaultConfigFile))
101 switch err {
@@ -106,6 +106,7 @@ func NewPluginLoader(repo string) (*PluginLoader, error) {
106 return nil, err
107 }
108 }
109 +
110 for _, v := range preloadPlugins {
111 if err := loader.Load(v); err != nil {
112 return nil, err
@@ -140,18 +141,22 @@ func (loader *PluginLoader) Load(pl plugin.Plugin) error {
141 }
142
143 name := pl.Name()
143 - if ppl, ok := loader.plugins[name]; ok {
144 - // plugin is already loaded
145 - return fmt.Errorf(
146 - "plugin: %s, is duplicated in version: %s, "+
147 - "while trying to load dynamically: %s",
148 - name, ppl.Version(), pl.Version())
144 +
145 + for _, p := range loader.plugins {
146 + if p.Name() == name {
147 + // plugin is already loaded
148 + return fmt.Errorf(
149 + "plugin: %s, is duplicated in version: %s, "+
150 + "while trying to load dynamically: %s",
151 + name, p.Version(), pl.Version())
152 + }
153 }
154 +
155 if loader.config.Plugins[name].Disabled {
156 log.Infof("not loading disabled plugin %s", name)
157 return nil
158 }
154 - loader.plugins[name] = pl
159 + loader.plugins = append(loader.plugins, pl)
160 return nil
161 }
162
@@ -219,10 +224,10 @@ func (loader *PluginLoader) Initialize() error {
224 if err := loader.transition(loaderLoading, loaderInitializing); err != nil {
225 return err
226 }
222 - for name, p := range loader.plugins {
227 + for _, p := range loader.plugins {
228 err := p.Init(&plugin.Environment{
229 Repo: loader.repo,
225 - Config: loader.config.Plugins[name].Config,
230 + Config: loader.config.Plugins[p.Name()].Config,
231 })
232 if err != nil {
233 loader.state = loaderFailed