-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix the potential race condition of InitializeSubnetService #879
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
bin/ | ||
.DS_Store | ||
go.work | ||
go.work.sum | ||
go.work.sum | ||
.tool-versions |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,20 +65,39 @@ func InitializeSubnetService(service common.Service) (*SubnetService, error) { | |
}, | ||
} | ||
|
||
// Use sync.Once to ensure channel is closed only once | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to change like this,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/vmware-tanzu/nsx-operator/blob/main/pkg/nsx/services/securitypolicy/firewall.go#L116-L116 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this version has no zombie routine. |
||
var closeOnceFatalError sync.Once | ||
var closeOnceWgDone sync.Once | ||
safeCloseFatalError := func(ce chan error) { | ||
closeOnceFatalError.Do(func() { | ||
close(ce) | ||
}) | ||
} | ||
safeCloseWgDone := func(ce chan bool) { | ||
closeOnceWgDone.Do(func() { | ||
close(ce) | ||
}) | ||
} | ||
|
||
wg.Add(1) | ||
go subnetService.InitializeResourceStore(&wg, fatalErrors, ResourceTypeSubnet, nil, subnetService.SubnetStore) | ||
go func() { | ||
wg.Wait() | ||
close(wgDone) | ||
safeCloseWgDone(wgDone) | ||
}() | ||
select { | ||
case <-wgDone: | ||
break | ||
safeCloseFatalError(fatalErrors) // Clean up fatalErrors channel | ||
return subnetService, nil | ||
case err := <-fatalErrors: | ||
close(fatalErrors) | ||
// Wait for any pending operations to complete | ||
go func() { | ||
wg.Wait() // Ensure all goroutines complete | ||
safeCloseWgDone(wgDone) | ||
}() | ||
safeCloseFatalError(fatalErrors) // Clean up fatalErrors channel | ||
return subnetService, err | ||
} | ||
return subnetService, nil | ||
} | ||
|
||
func (service *SubnetService) CreateOrUpdateSubnet(obj client.Object, vpcInfo common.VPCResourceInfo, tags []model.Tag) (string, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change needed?