Fix a race condition when evaluating on the root context #2096
+272
−76
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2094
#1944 parallelized the inspection of child modules, but did not take into account side effects that the child modules could cause on the root module. These are unsafe because the root module is shared by each child module (goroutine).
tflint/cmd/inspect.go
Lines 162 to 174 in 90494f3
The side effect reported in #2094 was the call stack. TFLint detects loops while building the call stack to prevent infinite loops when evaluating local values with circular references. This call stack was shared per Runner (Evaluator).
tflint/terraform/evaluator.go
Lines 76 to 82 in a7ebc9b
This means that the call stack with side effects is shared between goroutines.
To prevent this, we moved the call stack into a scope instead of the evaluator. The scope is initialized per evaluation and therefore is not shared between goroutines. As a result, the race condition is fixed.
tflint/terraform/evaluator.go
Lines 84 to 90 in a7ebc9b
To prevent such bugs, we added a test case to run with
go test --race
. In general, this tends to slow down compilation speed, so we only run some tests with--race
, and we expect to increase the number of tests if similar bugs are found.