@@ -148,7 +148,6 @@ func (g *GameServer) ManageBuffer() {
148148 }
149149 }
150150 }
151- g .gameDataMutex .Unlock ()
152151
153152 if leadPlayer > 0 {
154153 if bufferHealth > float32 (g .BufferTarget )+ 0.75 && g .gameData .bufferSize > 0 {
@@ -159,6 +158,7 @@ func (g *GameServer) ManageBuffer() {
159158 g .Logger .Info ("increased buffer size" , "bufferHealth" , bufferHealth , "bufferSize" , g .gameData .bufferSize , "leadPlayer" , leadPlayer )
160159 }
161160 }
161+ g .gameDataMutex .Unlock ()
162162
163163 time .Sleep (time .Second )
164164 }
@@ -170,7 +170,9 @@ func (g *GameServer) ManagePlayers() {
170170 playersActive := false // used to check if anyone is still around
171171 var i byte
172172
173- g .gameDataMutex .Lock () // PlayerAlive and Status can be modified by processUDP in a different thread
173+ // Lock registrations before gameData so TCP paths that take registrationsMutex then gameDataMutex cannot deadlock.
174+ g .registrationsMutex .Lock ()
175+ g .gameDataMutex .Lock ()
174176 for i = range 4 {
175177 _ , ok := g .registrations [i ]
176178 if ok {
@@ -181,9 +183,7 @@ func (g *GameServer) ManagePlayers() {
181183 g .Logger .Info ("player disconnected UDP" , "player" , i , "regID" , g .registrations [i ].regID , "address" , g .gameData .playerAddresses [i ])
182184 g .gameData .status |= (0x1 << (i + 1 ))
183185
184- g .registrationsMutex .Lock () // Registrations can be modified by processTCP
185186 delete (g .registrations , i )
186- g .registrationsMutex .Unlock ()
187187
188188 for k , v := range g .Players {
189189 if v .Number == int (i ) {
@@ -199,6 +199,7 @@ func (g *GameServer) ManagePlayers() {
199199 g .gameData .playerAlive [i ] = false
200200 }
201201 g .gameDataMutex .Unlock ()
202+ g .registrationsMutex .Unlock ()
202203
203204 if ! playersActive {
204205 g .Logger .Info ("no more players, closing room" , "numPlayers" , g .NumberOfPlayers , "clientSHA" , g .ClientSha , "playTime" , time .Since (g .StartTime ).String ())
0 commit comments