Skip to content

Commit 2d1657e

Browse files
Prevent against panic concurrent writes in addContext (#122)
Co-authored-by: Daniel Bradley <[email protected]>
1 parent 0d41bfe commit 2d1657e

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

examples/ec2_remote/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const ami = aws.ec2.getAmiOutput({
2424
mostRecent: true,
2525
filters: [{
2626
name: "name",
27-
values: ["amzn2-ami-hvm-2.0.????????-x86_64-gp2"],
27+
values: ["amzn-ami-hvm-*-x86_64-gp2"],
2828
}],
2929
});
3030

provider/pkg/provider/provider.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"sort"
21+
"sync"
2122

2223
"github.com/golang/protobuf/ptypes/empty"
2324
"github.com/pulumi/pulumi/sdk/v3/go/common/util/mapper"
@@ -34,22 +35,24 @@ import (
3435
)
3536

3637
type commandProvider struct {
37-
host *provider.HostClient
38-
name string
39-
version string
40-
pulumiSchema []byte
41-
cancelFuncs map[context.Context]context.CancelFunc
38+
host *provider.HostClient
39+
name string
40+
version string
41+
pulumiSchema []byte
42+
cancelFuncs map[context.Context]context.CancelFunc
43+
providerMutex *sync.Mutex
4244
}
4345

4446
func makeProvider(host *provider.HostClient, name, version string,
4547
pulumiSchema []byte) (pulumirpc.ResourceProviderServer, error) {
4648
// Return the new provider
4749
return &commandProvider{
48-
host: host,
49-
name: name,
50-
version: version,
51-
pulumiSchema: pulumiSchema,
52-
cancelFuncs: make(map[context.Context]context.CancelFunc),
50+
host: host,
51+
name: name,
52+
version: version,
53+
pulumiSchema: pulumiSchema,
54+
cancelFuncs: make(map[context.Context]context.CancelFunc),
55+
providerMutex: &sync.Mutex{},
5356
}, nil
5457
}
5558

@@ -459,18 +462,24 @@ func (k *commandProvider) GetSchema(ctx context.Context, req *pulumirpc.GetSchem
459462
// to the host to decide how long to wait after Cancel is called before (e.g.)
460463
// hard-closing any gRPC connection.
461464
func (k *commandProvider) Cancel(context.Context, *pbempty.Empty) (*pbempty.Empty, error) {
465+
k.providerMutex.Lock()
466+
defer k.providerMutex.Unlock()
462467
for _, cancel := range k.cancelFuncs {
463468
cancel()
464469
}
465470
return &pbempty.Empty{}, nil
466471
}
467472

468473
func (k *commandProvider) addContext(c context.Context) context.Context {
474+
k.providerMutex.Lock()
475+
defer k.providerMutex.Unlock()
469476
newctx, fn := context.WithCancel(c)
470477
k.cancelFuncs[newctx] = fn
471478
return newctx
472479
}
473480

474481
func (k *commandProvider) removeContext(c context.Context) {
482+
k.providerMutex.Lock()
483+
defer k.providerMutex.Unlock()
475484
delete(k.cancelFuncs, c)
476485
}

0 commit comments

Comments
 (0)