Skip to content

Commit

Permalink
Fix the potential race condition of InitializeSubnetService
Browse files Browse the repository at this point in the history
It is possible that an error occurs when trying to send to the already closed `fatalErrors` channel, resulting in a panic.
  • Loading branch information
zhengxiexie committed Nov 11, 2024
1 parent bf1880a commit 2534c5b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
bin/
.DS_Store
go.work
go.work.sum
go.work.sum
.tool-versions
9 changes: 7 additions & 2 deletions pkg/nsx/services/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2534c5b

Please sign in to comment.