From 0bd39034b384e7c2183bee9a25a8e112a0d3ea4d Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 13 Jul 2020 09:06:41 +0100 Subject: [PATCH] rootmodule: avoiding loading module twice at the same time 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. --- internal/terraform/rootmodule/root_module.go | 6 +++++- internal/terraform/rootmodule/root_module_manager.go | 5 ++++- internal/terraform/rootmodule/types.go | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/terraform/rootmodule/root_module.go b/internal/terraform/rootmodule/root_module.go index 671d4a561..4819cc6b6 100644 --- a/internal/terraform/rootmodule/root_module.go +++ b/internal/terraform/rootmodule/root_module.go @@ -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() { diff --git a/internal/terraform/rootmodule/root_module_manager.go b/internal/terraform/rootmodule/root_module_manager.go index e188af4df..1726057da 100644 --- a/internal/terraform/rootmodule/root_module_manager.go +++ b/internal/terraform/rootmodule/root_module_manager.go @@ -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 } diff --git a/internal/terraform/rootmodule/types.go b/internal/terraform/rootmodule/types.go index 1d62b9373..ed35945d0 100644 --- a/internal/terraform/rootmodule/types.go +++ b/internal/terraform/rootmodule/types.go @@ -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