Skip to content

Commit

Permalink
Protect declaredRuntimeMetadata with a sync.Mutex (#2733)
Browse files Browse the repository at this point in the history
This should fix the immediate issue. We should probably refactor this to
avoid the global, but that can be done later.

Fixes #2728
  • Loading branch information
iwahbe authored Dec 13, 2024
1 parent b9a6a39 commit f7f43b6
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pkg/tfbridge/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package tfbridge

import (
"sync"

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
"github.com/pulumi/pulumi-terraform-bridge/v3/unstable/metadata"
)
Expand All @@ -34,19 +36,27 @@ type MetadataInfo = info.Metadata
// `bytes` is the embedded metadata file.
func NewProviderMetadata(bytes []byte) *MetadataInfo { return info.NewProviderMetadata(bytes) }

var declaredRuntimeMetadata = map[string]struct{}{
var declaredRuntimeMetadata = struct {
keys map[string]struct{}
m sync.Mutex
}{keys: map[string]struct{}{
autoSettingsKey: {},
"mux": {},
}
}}

func declareRuntimeMetadata(label string) { declaredRuntimeMetadata[label] = struct{}{} }
func declareRuntimeMetadata(label string) {
declaredRuntimeMetadata.m.Lock()
defer declaredRuntimeMetadata.m.Unlock()
declaredRuntimeMetadata.keys[label] = struct{}{}
}

// trim the metadata to just the keys required for the runtime phase
// in the future this method might also substitute compressed contents within some keys
func ExtractRuntimeMetadata(info *MetadataInfo) *MetadataInfo {
data, _ := metadata.New(nil)

for k := range declaredRuntimeMetadata {
declaredRuntimeMetadata.m.Lock()
defer declaredRuntimeMetadata.m.Unlock()
for k := range declaredRuntimeMetadata.keys {
metadata.CloneKey(k, info.Data, data)
}

Expand Down

0 comments on commit f7f43b6

Please sign in to comment.