Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45f81a7

Browse files
authored
fix: prevent terraform init races (#4985)
1 parent d277e28 commit 45f81a7

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

provisioner/terraform/executor.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@ import (
1414
"strings"
1515
"sync"
1616

17-
"golang.org/x/xerrors"
18-
1917
"github.com/hashicorp/go-version"
2018
tfjson "github.com/hashicorp/terraform-json"
19+
"golang.org/x/xerrors"
2120

2221
"github.com/coder/coder/provisionersdk/proto"
2322
)
2423

24+
// initMut is a global mutex that protects the Terraform cache directory from
25+
// concurrent usage by path. Only `terraform init` commands are guarded by this
26+
// mutex.
27+
//
28+
// When cache path is set, we must protect against multiple calls to
29+
// `terraform init`.
30+
//
31+
// From the Terraform documentation:
32+
//
33+
// Note: The plugin cache directory is not guaranteed to be concurrency
34+
// safe. The provider installer's behavior in environments with multiple
35+
// terraform init calls is undefined.
36+
var initMut = &sync.Mutex{}
37+
2538
type executor struct {
26-
initMu sync.Locker
2739
binaryPath string
2840
cachePath string
2941
workdir string
@@ -181,8 +193,8 @@ func (e executor) init(ctx, killCtx context.Context, logr logger) error {
181193
// concurrency safe. The provider installer's behavior in
182194
// environments with multiple terraform init calls is undefined.
183195
if e.cachePath != "" {
184-
e.initMu.Lock()
185-
defer e.initMu.Unlock()
196+
initMut.Lock()
197+
defer initMut.Unlock()
186198
}
187199

188200
return e.execWriteOutput(ctx, killCtx, args, e.basicEnv(), outWriter, errWriter)

provisioner/terraform/executor_test.go renamed to provisioner/terraform/executor_internal_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// nolint:testpackage
21
package terraform
32

43
import (

provisioner/terraform/serve.go

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

98
"github.com/cli/safeexec"
@@ -100,20 +99,14 @@ func Serve(ctx context.Context, options *ServeOptions) error {
10099
}
101100

102101
type server struct {
103-
// initMu protects against executors running `terraform init`
104-
// concurrently when cache path is set.
105-
initMu sync.Mutex
106-
107-
binaryPath string
108-
cachePath string
109-
logger slog.Logger
110-
102+
binaryPath string
103+
cachePath string
104+
logger slog.Logger
111105
exitTimeout time.Duration
112106
}
113107

114108
func (s *server) executor(workdir string) executor {
115109
return executor{
116-
initMu: &s.initMu,
117110
binaryPath: s.binaryPath,
118111
cachePath: s.cachePath,
119112
workdir: workdir,

0 commit comments

Comments
 (0)