terraform: Fix data race in MockProvider.ApplyResourceChange

The p.ConfigureProviderCalled read needs to be guarded by the mutex so
that it can't race with the write to that field in the ConfigureProvider
method.

This remains as a pair of explicit calls to Unlock rather than as a
single deferred call because existing tests might be relying on their
ApplyResourceChangeFn being called without the mutex locked. This isn't
necessarily required, but I'm being conservative to reduce the risk of
unexpectedly making subtle changes to the behavior of existing tests.
pull/34738/head
Martin Atkins 3 years ago
parent 532bc60d34
commit 4fb8a1cdb7

@ -397,12 +397,13 @@ func (p *MockProvider) ApplyResourceChange(r providers.ApplyResourceChangeReques
p.Lock()
p.ApplyResourceChangeCalled = true
p.ApplyResourceChangeRequest = r
p.Unlock()
if !p.ConfigureProviderCalled {
p.Unlock()
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Configure not called before ApplyResourceChange %q", r.TypeName))
return resp
}
p.Unlock()
if p.ApplyResourceChangeFn != nil {
return p.ApplyResourceChangeFn(r)

Loading…
Cancel
Save