Skip to content

Commit ca6b211

Browse files
committed
fix: Protect against concurrent terraform init
1 parent b42bfa4 commit ca6b211

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

provisioner/terraform/executor.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
)
2424

2525
type executor struct {
26+
initMu sync.Locker
2627
binaryPath string
2728
cachePath string
2829
workdir string
@@ -142,6 +143,19 @@ func (e executor) init(ctx context.Context, logr logger) error {
142143
"-no-color",
143144
"-input=false",
144145
}
146+
147+
// When cache path is set, we must protect against multiple calls
148+
// to `terraform init`.
149+
//
150+
// From the Terraform documentation:
151+
// Note: The plugin cache directory is not guaranteed to be
152+
// concurrency safe. The provider installer's behavior in
153+
// environments with multiple terraform init calls is undefined.
154+
if e.cachePath != "" {
155+
e.initMu.Lock()
156+
defer e.initMu.Unlock()
157+
}
158+
145159
return e.execWriteOutput(ctx, args, e.basicEnv(), outWriter, errWriter)
146160
}
147161

provisioner/terraform/serve.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package terraform
33
import (
44
"context"
55
"path/filepath"
6+
"sync"
67

78
"github.com/cli/safeexec"
89
"github.com/hashicorp/go-version"
@@ -109,15 +110,20 @@ func Serve(ctx context.Context, options *ServeOptions) error {
109110
}
110111

111112
type server struct {
113+
// initMu protects against executors running `terraform init`
114+
// concurrently when cache path is set.
115+
initMu sync.Mutex
116+
112117
binaryPath string
113118
cachePath string
114119
logger slog.Logger
115120
}
116121

117-
func (t server) executor(workdir string) executor {
122+
func (s *server) executor(workdir string) executor {
118123
return executor{
119-
binaryPath: t.binaryPath,
120-
cachePath: t.cachePath,
124+
initMu: &s.initMu,
125+
binaryPath: s.binaryPath,
126+
cachePath: s.cachePath,
121127
workdir: workdir,
122128
}
123129
}

0 commit comments

Comments
 (0)