Skip to content

Commit

Permalink
rootmodule: avoiding loading module twice at the same time
Browse files Browse the repository at this point in the history
This would never happen anyway since RootModuleManager is the only caller
and adding new modules is already gated to run only once
but it doesn't hurt to have an extra check.
  • Loading branch information
radeksimko committed Jul 13, 2020
1 parent 801d4df commit 0bd3903
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion internal/terraform/rootmodule/root_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,17 @@ func (rm *rootModule) SetLogger(logger *log.Logger) {
rm.logger = logger
}

func (rm *rootModule) StartLoading() {
func (rm *rootModule) StartLoading() error {
if !rm.IsLoadingDone() {
return fmt.Errorf("root module is already being loaded")
}
ctx, cancelFunc := context.WithCancel(context.Background())
rm.cancelLoading = cancelFunc

go func(ctx context.Context) {
rm.setLoadErr(rm.load(ctx))
}(ctx)
return nil
}

func (rm *rootModule) CancelLoading() {
Expand Down
5 changes: 4 additions & 1 deletion internal/terraform/rootmodule/root_module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ func (rmm *rootModuleManager) AddAndStartLoadingRootModule(ctx context.Context,
}

rmm.logger.Printf("asynchronously loading root module %s", dir)
rm.StartLoading()
err = rm.StartLoading()
if err != nil {
return rm, err
}

return rm, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/rootmodule/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (rms RootModules) Paths() []string {
type RootModule interface {
Path() string
LoadError() error
StartLoading()
StartLoading() error
IsLoadingDone() bool
IsKnownPluginLockFile(path string) bool
IsKnownModuleManifestFile(path string) bool
Expand Down

0 comments on commit 0bd3903

Please sign in to comment.