Skip to content

Commit

Permalink
plugin: fix non-deterministic loading order
Browse files Browse the repository at this point in the history
fix #9909
  • Loading branch information
MichaelMure authored and Jorropo committed May 31, 2023
1 parent 5a993cf commit 99fdaa1
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions plugin/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func (ls loaderState) String() string {
// 5. Call Close to close all plugins.
type PluginLoader struct {
state loaderState
plugins map[string]plugin.Plugin
plugins []plugin.Plugin
started []plugin.Plugin
config config.Plugins
repo string
}

// NewPluginLoader creates new plugin loader
func NewPluginLoader(repo string) (*PluginLoader, error) {
loader := &PluginLoader{plugins: make(map[string]plugin.Plugin, len(preloadPlugins)), repo: repo}
loader := &PluginLoader{plugins: make([]plugin.Plugin, 0, len(preloadPlugins)), repo: repo}
if repo != "" {
cfg, err := cserialize.Load(filepath.Join(repo, config.DefaultConfigFile))
switch err {
Expand All @@ -106,6 +106,7 @@ func NewPluginLoader(repo string) (*PluginLoader, error) {
return nil, err
}
}

for _, v := range preloadPlugins {
if err := loader.Load(v); err != nil {
return nil, err
Expand Down Expand Up @@ -140,18 +141,22 @@ func (loader *PluginLoader) Load(pl plugin.Plugin) error {
}

name := pl.Name()
if ppl, ok := loader.plugins[name]; ok {
// plugin is already loaded
return fmt.Errorf(
"plugin: %s, is duplicated in version: %s, "+
"while trying to load dynamically: %s",
name, ppl.Version(), pl.Version())

for _, p := range loader.plugins {
if p.Name() == name {
// plugin is already loaded
return fmt.Errorf(
"plugin: %s, is duplicated in version: %s, "+
"while trying to load dynamically: %s",
name, p.Version(), pl.Version())
}
}

if loader.config.Plugins[name].Disabled {
log.Infof("not loading disabled plugin %s", name)
return nil
}
loader.plugins[name] = pl
loader.plugins = append(loader.plugins, pl)
return nil
}

Expand Down Expand Up @@ -219,10 +224,10 @@ func (loader *PluginLoader) Initialize() error {
if err := loader.transition(loaderLoading, loaderInitializing); err != nil {
return err
}
for name, p := range loader.plugins {
for _, p := range loader.plugins {
err := p.Init(&plugin.Environment{
Repo: loader.repo,
Config: loader.config.Plugins[name].Config,
Config: loader.config.Plugins[p.Name()].Config,
})
if err != nil {
loader.state = loaderFailed
Expand Down

0 comments on commit 99fdaa1

Please sign in to comment.