Skip to content

Commit

Permalink
IBMCloud: Use unique mutex for Client retrieval
Browse files Browse the repository at this point in the history
A shared mutex for all ibmcloud.metadata functions, including the
public Client method, deadlocks calls. Since the Client is a public
method, we should still use a mutex, but it should be a unique
mutex to prevent deadlocking.
  • Loading branch information
cjschaef committed Aug 18, 2022
1 parent a8482db commit 61ff6c8
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions pkg/asset/installconfig/ibmcloud/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type Metadata struct {
computeSubnets map[string]Subnet
controlPlaneSubnets map[string]Subnet

mutex sync.Mutex
mutex sync.Mutex
clientMutex sync.Mutex
}

// NewMetadata initializes a new Metadata object.
Expand Down Expand Up @@ -131,20 +132,22 @@ func (m *Metadata) ControlPlaneSubnets(ctx context.Context) (map[string]Subnet,

// Client returns a client used for making API calls to IBM Cloud services.
func (m *Metadata) Client() (*Client, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.client != nil {
return m.client, nil
}

if m.client == nil {
client, err := NewClient()
if err != nil {
return nil, err
}
err = client.SetVPCServiceURLForRegion(context.TODO(), m.Region)
if err != nil {
return nil, err
}
m.client = client
m.clientMutex.Lock()
defer m.clientMutex.Unlock()

client, err := NewClient()
if err != nil {
return nil, err
}
err = client.SetVPCServiceURLForRegion(context.TODO(), m.Region)
if err != nil {
return nil, err
}
m.client = client
return m.client, nil
}

Expand Down

0 comments on commit 61ff6c8

Please sign in to comment.