Skip to content

Commit

Permalink
Fix concurrency bug in port allocator.
Browse files Browse the repository at this point in the history
In some cases, not having this locked would
cause a panic, which crashes the controller!
  • Loading branch information
markmandel committed Jan 26, 2019
1 parent 6996baa commit 78d4023
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/gameservers/portallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,22 @@ func (pa *PortAllocator) Allocate(gs *v1alpha1.GameServer) *v1alpha1.GameServer
// DeAllocate marks the given port as no longer allocated
func (pa *PortAllocator) DeAllocate(gs *v1alpha1.GameServer) {
// skip if it wasn't previously allocated
if _, ok := pa.gameServerRegistry[gs.ObjectMeta.UID]; !ok {

found := func() bool {
pa.mutex.RLock()
defer pa.mutex.RUnlock()
if _, ok := pa.gameServerRegistry[gs.ObjectMeta.UID]; ok {
return true
}
return false
}

if !found() {
pa.logger.WithField("gs", gs.ObjectMeta.Name).
Info("Did not allocate this GameServer. Ignoring for DeAllocation")
return
}

pa.mutex.Lock()
defer pa.mutex.Unlock()
for _, p := range gs.Spec.Ports {
Expand Down

0 comments on commit 78d4023

Please sign in to comment.