From 94208932f2d2713b80565936575ae69b2cf365a5 Mon Sep 17 00:00:00 2001 From: Marcel Schramm Date: Sun, 20 Oct 2024 12:03:31 +0200 Subject: [PATCH] Fixes #345 Also adds two more loggers on shutdown in case this happens again. Helpful tool while debugging: https://github.com/sasha-s/go-deadlock --- internal/game/lobby.go | 14 +++++++++----- internal/state/lobbies.go | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/game/lobby.go b/internal/game/lobby.go index a97feb61..a88f4096 100644 --- a/internal/game/lobby.go +++ b/internal/game/lobby.go @@ -243,17 +243,20 @@ func (lobby *Lobby) handleToggleReadinessEvent(player *Player) { func (lobby *Lobby) readyToStart() bool { // Otherwise the game will start and gameover instantly. This can happen // if a lobby is created and the owner refreshes. - if !lobby.HasConnectedPlayers() { - return false - } + var hasConnectedPlayers bool for _, otherPlayer := range lobby.players { - if otherPlayer.Connected && otherPlayer.State != Ready { + if !otherPlayer.Connected { + continue + } + + if otherPlayer.State != Ready { return false } + hasConnectedPlayers = true } - return true + return hasConnectedPlayers } func handleMessage(message string, sender *Player, lobby *Lobby) { @@ -1156,6 +1159,7 @@ func (lobby *Lobby) canDraw(player *Player) bool { func (lobby *Lobby) Shutdown() { lobby.mutex.Lock() defer lobby.mutex.Unlock() + log.Println("Lobby Shutdown: Mutex aqcuired") lobby.Broadcast(&EventTypeOnly{Type: EventTypeShutdown}) } diff --git a/internal/state/lobbies.go b/internal/state/lobbies.go index dd13fc6a..91605c94 100644 --- a/internal/state/lobbies.go +++ b/internal/state/lobbies.go @@ -90,6 +90,7 @@ func GetLobby(id string) *game.Lobby { func ShutdownLobbiesGracefully() { globalStateMutex.Lock() defer globalStateMutex.Unlock() + log.Println("Shutdown: Mutex aqcuired") for _, lobby := range lobbies { // Since a reconnect requires a lookup to the state, all attempts to