Skip to content

Commit d389434

Browse files
committed
fix deadlock
1 parent 82bd7e2 commit d389434

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

internal/gameServer/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ func (g *GameServer) isConnClosed(err error) bool {
9494
}
9595

9696
func (g *GameServer) bufferHealthAverage(playerNumber int) (float32, error) {
97-
g.GameDataMutex.Lock()
98-
defer g.GameDataMutex.Unlock()
9997
var bufferHealth float32
10098
if g.GameData.BufferHealth[playerNumber].Len() > 0 {
10199
for _, k := range g.GameData.BufferHealth[playerNumber].Keys() {
@@ -118,6 +116,7 @@ func (g *GameServer) ManageBuffer() {
118116
// Find the largest buffer health
119117
var bufferHealth float32
120118
var foundPlayer bool
119+
g.GameDataMutex.Lock() // BufferHealth can be modified by processUDP in a different thread
121120
for i := range 4 {
122121
if g.GameData.CountLag[i] == 0 {
123122
playerBufferHealth, err := g.bufferHealthAverage(i)
@@ -127,6 +126,7 @@ func (g *GameServer) ManageBuffer() {
127126
}
128127
}
129128
}
129+
g.GameDataMutex.Unlock()
130130

131131
if foundPlayer {
132132
if bufferHealth > float32(g.BufferTarget)+0.5 && g.GameData.BufferSize > 0 {

internal/gameServer/udp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (g *GameServer) processUDP(addr *net.UDPAddr) {
150150
return
151151
}
152152
g.GameData.CountLag[sendingPlayerNumber] = g.sendUDPInput(count, addr, playerNumber, spectator != 0, sendingPlayerNumber)
153-
g.GameDataMutex.Lock() // PlayerAlive and BufferHealth can be modified by ManagePlayers in a different thread
153+
g.GameDataMutex.Lock() // PlayerAlive and BufferHealth can be modified in different threads
154154
g.GameData.BufferHealth[sendingPlayerNumber].Add(count, g.GameData.recvBuffer[11])
155155
g.GameData.PlayerAlive[sendingPlayerNumber] = true
156156
g.GameDataMutex.Unlock()

0 commit comments

Comments
 (0)