From 254a477b737c5499639e20a0afdae8e1ef92aeed Mon Sep 17 00:00:00 2001 From: Thomas Parrott Date: Fri, 18 Oct 2024 11:13:33 +0100 Subject: [PATCH 1/2] lxd/network/network/utils: Fix incorrect handling of instances in UsedByInstanceDevices During refactor of this function in e5f5b887ae11d50f9b6bb99df2a008d2edd6e4e9 the project filtering logic was incorrectly returning rather than continuing to next instance. This meant some instances were being excluded incorrectly. Signed-off-by: Thomas Parrott --- lxd/network/network_utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lxd/network/network_utils.go b/lxd/network/network_utils.go index c7c6e57eddba..159a12e7e1bc 100644 --- a/lxd/network/network_utils.go +++ b/lxd/network/network_utils.go @@ -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 { @@ -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. From 685a4a6c7caccbee847150c4994bbce74487594c Mon Sep 17 00:00:00 2001 From: Thomas Parrott Date: Fri, 18 Oct 2024 11:30:47 +0100 Subject: [PATCH 2/2] lxd/network/driver/bridge: Handle concurrent collection of member leases correctly Appending to a slice concurrently is not supported. Signed-off-by: Thomas Parrott --- lxd/network/driver_bridge.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lxd/network/driver_bridge.go b/lxd/network/driver_bridge.go index 60e7ba365fe4..f5243cf4c0b7 100644 --- a/lxd/network/driver_bridge.go +++ b/lxd/network/driver_bridge.go @@ -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 { @@ -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 }