From 2534c5bd453e18c42bdb74e59c9ed78f639ffbfd Mon Sep 17 00:00:00 2001 From: Xie Zheng Date: Mon, 11 Nov 2024 12:26:02 +0800 Subject: [PATCH] Fix the potential race condition of InitializeSubnetService It is possible that an error occurs when trying to send to the already closed `fatalErrors` channel, resulting in a panic. --- .gitignore | 3 ++- pkg/nsx/services/subnet/subnet.go | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 59402d8ae..2adb3be24 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ bin/ .DS_Store go.work -go.work.sum \ No newline at end of file +go.work.sum +.tool-versions \ No newline at end of file diff --git a/pkg/nsx/services/subnet/subnet.go b/pkg/nsx/services/subnet/subnet.go index f53c91ab0..230818c86 100644 --- a/pkg/nsx/services/subnet/subnet.go +++ b/pkg/nsx/services/subnet/subnet.go @@ -73,12 +73,17 @@ func InitializeSubnetService(service common.Service) (*SubnetService, error) { }() select { case <-wgDone: - break + close(fatalErrors) // Clean up fatalErrors channel + return subnetService, nil case err := <-fatalErrors: + // Wait for any pending operations to complete + go func() { + wg.Wait() // Ensure all goroutines complete + close(wgDone) + }() close(fatalErrors) return subnetService, err } - return subnetService, nil } func (service *SubnetService) CreateOrUpdateSubnet(obj client.Object, vpcInfo common.VPCResourceInfo, tags []model.Tag) (string, error) {