fix racism condition / deadlock

This commit is contained in:
bdnugget 2025-01-19 00:51:49 +01:00
parent c40e4ae7ac
commit 509bc8b20b
2 changed files with 20 additions and 11 deletions

View File

@ -18,7 +18,6 @@ type Game struct {
Chat *Chat
MenuOpen bool
QuitChan chan struct{} // Channel to signal shutdown
QuitDone chan struct{} // New channel to signal when cleanup is complete
}
func New() *Game {
@ -42,7 +41,6 @@ func New() *Game {
},
Chat: NewChat(),
QuitChan: make(chan struct{}),
QuitDone: make(chan struct{}),
}
game.Player.UserData = game
game.Chat.userData = game
@ -142,7 +140,7 @@ func (g *Game) DrawPlayer(player *types.Player, model rl.Model) {
rl.DrawModel(model, playerPos, 16, rl.White)
if player.FloatingMessage != nil && time.Now().Before(player.FloatingMessage.ExpireTime) {
if player.FloatingMessage != nil {
screenPos := rl.GetWorldToScreen(rl.Vector3{
X: playerPos.X,
Y: playerPos.Y + 24.0,
@ -150,8 +148,6 @@ func (g *Game) DrawPlayer(player *types.Player, model rl.Model) {
}, g.Camera)
player.FloatingMessage.ScreenPos = screenPos
} else if player.FloatingMessage != nil {
player.FloatingMessage = nil
}
if len(player.TargetPath) > 0 {
@ -293,9 +289,7 @@ func (g *Game) DrawMenu() {
case "Settings":
// TODO: Implement settings
case "Exit Game":
close(g.QuitChan)
<-g.QuitDone
rl.CloseWindow()
g.Shutdown()
}
}
}
@ -310,3 +304,9 @@ func (g *Game) DrawMenu() {
buttonY += buttonSpacing
}
}
func (g *Game) Shutdown() {
close(g.QuitChan)
<-g.Player.QuitDone
rl.CloseWindow()
}

View File

@ -111,9 +111,18 @@ func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Play
for {
select {
case <-quitChan:
<-done // Wait for action goroutine to finish
close(done)
time.Sleep(100 * time.Millisecond) // Give time for disconnect message to be sent
done := make(chan struct{})
go func() {
<-done
close(player.QuitDone)
}()
select {
case <-done:
time.Sleep(100 * time.Millisecond)
case <-time.After(1 * time.Second):
log.Println("Shutdown timed out")
}
return
default:
// Read message length (4 bytes)