pulumi/pkg/codegen/hcl2/model
Abhinav Gupta 9b43e56add
pkg/hcl2/model: Make String methods thread-safe
ObjectType.String and a few other types
do a fair bit of computation to build a string representation.
They memoize the result in an internal `s` field.
This causes a data race if two goroutines call `String()`
on the same object at the same time.

Tests in pkg/codegen/pcl ran into the following
from ObjectType.String.

```
WARNING: DATA RACE
Write at 0x00c00055c030 by goroutine 36:
  github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model.(*ObjectType).string()
      [..]/pulumi/pkg/codegen/hcl2/model/type_object.go:300 +0x418
  [.. snip ..]
  github.com/pulumi/pulumi/pkg/v3/codegen/pcl.TestApplyRewriter.func1()
      [..]/pulumi/pkg/codegen/pcl/rewrite_apply_test.go:176 +0x68
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.19.5/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/Cellar/go/1.19.5/libexec/src/testing/testing.go:1493 +0x40

Previous read at 0x00c00055c030 by goroutine 37:
  github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model.(*ObjectType).string()
      [..]/pulumi/pkg/codegen/hcl2/model/type_object.go:276 +0x40
  github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model.(*OutputType).string()
      [..]/pulumi/pkg/codegen/hcl2/model/type_output.go:110 +0x4c
  [..]
```

To fix this, turn the memoized field into an `atomic.Value`
in all types that memoize it.

When two goroutines race to calculate this value,
one of them will do extra work that will be thrown away,
but it won't cause a data race.

Refs #10092
2023-01-18 16:18:17 -08:00
..
format Update error handling (#8406) 2021-11-12 18:37:17 -08:00
pretty Handle recursive types 2023-01-13 11:28:13 -08:00
attribute.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
binder_expression.go Remove traverse errors on dynamic types 2022-11-16 14:41:30 -08:00
binder_expression_test.go ci: radical idea - what if slow tests & no stdout makes GH consider runner dead? 2022-03-06 14:52:13 -08:00
block.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
body.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
diagnostics.go Handle recursive types 2023-01-13 11:28:13 -08:00
expression.go Repair tests expecting nil slices 2023-01-11 21:53:04 -08:00
functions.go Initial implementation of simplified invokes for dotnet and nodejs 2023-01-11 14:17:14 -08:00
print_test.go Escape `${` & `%{` in string literals 2022-12-08 10:46:23 +01:00
printer.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
scope.go Remove traverse errors on dynamic types 2022-11-16 14:41:30 -08:00
spec.md Add an HCL2 static typechecker and semantic model. (#4087) 2020-03-18 09:28:57 -07:00
traversable.go Remove traverse errors on dynamic types 2022-11-16 14:41:30 -08:00
type.go Pretty print PCL types 2022-12-19 17:13:15 +01:00
type_collection.go Update error handling (#8406) 2021-11-12 18:37:17 -08:00
type_const.go golangci-lint: Enable staticcheck 2023-01-14 16:59:46 -08:00
type_enum.go pkg/hcl2/model: Make String methods thread-safe 2023-01-18 16:18:17 -08:00
type_eventuals.go [codegen/hcl2] Improve ConversionFrom perf. (#7545) 2021-07-16 09:56:26 -07:00
type_list.go Handle recursive types 2023-01-13 11:28:13 -08:00
type_map.go Handle recursive types 2023-01-13 11:28:13 -08:00
type_none.go Pretty print PCL types 2022-12-19 17:13:15 +01:00
type_object.go pkg/hcl2/model: Make String methods thread-safe 2023-01-18 16:18:17 -08:00
type_opaque.go Pretty print PCL types 2022-12-19 17:13:15 +01:00
type_opaque_test.go [codegen] simplify opaque types to string newtype (#9770) 2022-06-13 11:13:03 -07:00
type_output.go Handle recursive types 2023-01-13 11:28:13 -08:00
type_promise.go Handle recursive types 2023-01-13 11:28:13 -08:00
type_scope.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
type_set.go Handle recursive types 2023-01-13 11:28:13 -08:00
type_test.go Allow case insensitive prop typecheck on traversal 2022-11-08 09:08:12 -08:00
type_tuple.go pkg/hcl2/model: Make String methods thread-safe 2023-01-18 16:18:17 -08:00
type_union.go pkg/hcl2/model: Make String methods thread-safe 2023-01-18 16:18:17 -08:00
utilities.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
visitor.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00