fix: data race in checks.State.ObjectFailureMessages#38684
Open
sakiphan wants to merge 2 commits into
Open
Conversation
ObjectFailureMessages read the shared statuses and failureMsgs maps
without holding c.mu, while the Report* methods write to them under the
lock from concurrent graph-walk goroutines. This can cause panics
("concurrent map read and map write") or incorrect results.
Acquire c.mu for the duration of the read, matching every other public
method on *State (e.g. ObjectCheckStatus). ObjectFailureMessages does not
call back into any *State method, so there is no deadlock risk.
Adds a regression test that fails under `go test -race` without the fix.
Fixes hashicorp#38578
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
State.ObjectFailureMessagesreads the sharedstatusesandfailureMsgsmaps without taking
c.mu, but theReport*methods write to those same mapsunder the lock while Terraform Core walks the graph concurrently. That's a data
race — under
-raceit's flagged as one, and in the worst case it surfaces as aconcurrent map read and map writepanic or as wrong/missing failure messages.The fix just holds
c.mufor the duration of the read, the same way every otherreader on
*Statealready does (e.g.ObjectCheckStatus). The method doesn'tcall back into any other
*Statemethod, so there's no risk of deadlocking onthe non-reentrant mutex.
I also added a regression test that runs concurrent writers and readers against a
single
State. It reports a race undergo test -race ./internal/checks/...without the lock and passes with it.
Fixes #38578
Target Release
1.16.x
This has been latent since #31268, so it's also a reasonable backport candidate
if the team wants it in a patch release.
Rollback Plan
Changes to Security Controls
No. This only adds a mutex lock around an existing read — no changes to access
controls, encryption, or logging.
CHANGELOG entry