Skip to content

Commit

Permalink
Networt: Fix incorrect handling of instances in UsedByInstanceDevices (
Browse files Browse the repository at this point in the history
…#14299)

During cherry-pick from Incus of refactor of this function in
e5f5b88 the project filtering logic was
incorrectly returning rather than continuing to next instance.

This meant some instances were being excluded incorrectly.

Also fixes a potential issue with concurrent modification of the leases
slice in `bridge.Leases()` when collecting leases from each cluster
member concurrently.

Fixes #13412
  • Loading branch information
tomponline authored Oct 18, 2024
2 parents 214b65e + 685a4a6 commit c74a664
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion lxd/network/driver_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3632,6 +3632,18 @@ func (n *bridge) Leases(projectName string, clientType request.ClientType) ([]ap
return nil, err
}

leasesCh := make(chan api.NetworkLease)

var wg sync.WaitGroup
wg.Add(1)
go func() {
for lease := range leasesCh {
leases = append(leases, lease)
}

wg.Done()
}()

err = notifier(func(client lxd.InstanceServer) error {
memberLeases, err := client.GetNetworkLeases(n.name)
if err != nil {
Expand All @@ -3641,12 +3653,17 @@ func (n *bridge) Leases(projectName string, clientType request.ClientType) ([]ap
// Add local leases from other members, filtering them for MACs that belong to the project.
for _, lease := range memberLeases {
if lease.Hwaddr != "" && shared.ValueInSlice(lease.Hwaddr, projectMacs) {
leases = append(leases, lease)
leasesCh <- lease
}
}

return nil
})

// Finish up and wait for go routine.
close(leasesCh)
wg.Wait()

if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions lxd/network/network_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func MACDevName(mac net.HardwareAddr) string {
func UsedByInstanceDevices(s *state.State, networkProjectName string, networkName string, networkType string, usageFunc func(inst db.InstanceArgs, nicName string, nicConfig map[string]string) error, filters ...cluster.InstanceFilter) error {
// Get the instances.
projects := map[string]api.Project{}
instances := []db.InstanceArgs{}
var instances []db.InstanceArgs

err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.InstanceList(ctx, func(inst db.InstanceArgs, p api.Project) error {
Expand All @@ -105,7 +105,7 @@ func UsedByInstanceDevices(s *state.State, networkProjectName string, networkNam

// Skip instances who's effective network project doesn't match this Network's project.
if instNetworkProject != networkProjectName {
return nil
continue
}

// Look for NIC devices using this network.
Expand Down

0 comments on commit c74a664

Please sign in to comment.