mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-25 01:00:16 -05:00
copy Dependencies before sorting in state
Instances of the same AbsResource may share the same Dependencies, which could point to the same backing array of values. Since address values are not pointers, and not meant to be shared, we must copy the value before sorting the slice in-place. Because individual instances of the same resource may be encoded to state concurrently, failure to copy the slice first can result in a data race.
This commit is contained in:
@@ -115,7 +115,12 @@ func (o *ResourceInstanceObject) Encode(ty cty.Type, schemaVersion uint64) (*Res
|
||||
// stored in state as an array. To avoid pointless thrashing of state in
|
||||
// refresh-only runs, we can either override comparison of dependency lists
|
||||
// (more desirable, but tricky for Reasons) or just sort when encoding.
|
||||
sort.Slice(o.Dependencies, func(i, j int) bool { return o.Dependencies[i].String() < o.Dependencies[j].String() })
|
||||
// Encoding of instances can happen concurrently, so we must copy the
|
||||
// dependencies to avoid mutating what may be a shared array of values.
|
||||
dependencies := make([]addrs.ConfigResource, len(o.Dependencies))
|
||||
copy(dependencies, o.Dependencies)
|
||||
|
||||
sort.Slice(dependencies, func(i, j int) bool { return dependencies[i].String() < dependencies[j].String() })
|
||||
|
||||
return &ResourceInstanceObjectSrc{
|
||||
SchemaVersion: schemaVersion,
|
||||
@@ -123,7 +128,7 @@ func (o *ResourceInstanceObject) Encode(ty cty.Type, schemaVersion uint64) (*Res
|
||||
AttrSensitivePaths: pvm,
|
||||
Private: o.Private,
|
||||
Status: o.Status,
|
||||
Dependencies: o.Dependencies,
|
||||
Dependencies: dependencies,
|
||||
CreateBeforeDestroy: o.CreateBeforeDestroy,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user