Skip to content
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 race condition panic in loadbalancer.nextServer #10318

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions pkg/agent/loadbalancer/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,33 @@ func (lb *LoadBalancer) nextServer(failedServer string) (string, error) {
lb.mutex.RLock()
defer lb.mutex.RUnlock()

// note: these fields are not protected by the mutex, so we clamp the index value and update
// the index/current address using local variables, to avoid time-of-check vs time-of-use
// race conditions caused by goroutine A incrementing it in between the time goroutine B
// validates its value, and uses it as a list index.
currentServerAddress := lb.currentServerAddress
nextServerIndex := lb.nextServerIndex

if len(lb.randomServers) == 0 {
return "", errors.New("No servers in load balancer proxy list")
}
if len(lb.randomServers) == 1 {
return lb.currentServerAddress, nil
return currentServerAddress, nil
}
if failedServer != lb.currentServerAddress {
return lb.currentServerAddress, nil
if failedServer != currentServerAddress {
return currentServerAddress, nil
}
if lb.nextServerIndex >= len(lb.randomServers) {
lb.nextServerIndex = 0
if nextServerIndex >= len(lb.randomServers) {
nextServerIndex = 0
}

lb.currentServerAddress = lb.randomServers[lb.nextServerIndex]
lb.nextServerIndex++
currentServerAddress = lb.randomServers[nextServerIndex]
nextServerIndex++

lb.currentServerAddress = currentServerAddress
lb.nextServerIndex = nextServerIndex

return lb.currentServerAddress, nil
return currentServerAddress, nil
}

// dialContext dials a new connection using the environment's proxy settings, and adds its wrapped connection to the map
Expand Down