Fix race condition when terminating client

This commit is contained in:
Trevor Slocum 2023-11-14 00:14:30 -08:00
parent 97fe9ebf0d
commit 019317f4f2
3 changed files with 9 additions and 16 deletions

View file

@ -19,7 +19,7 @@ type serverClient struct {
connected int64
lastActive int64
lastPing int64
commands <-chan []byte
commands chan []byte
playerNumber int
terminating bool
bgammon.Client
@ -153,10 +153,9 @@ func (c *serverClient) Terminate(reason string) {
if reason != "" {
extra = ": " + reason
}
c.sendNotice("Connection terminated" + extra)
go func() {
c.sendNotice("Connection terminated" + extra)
time.Sleep(time.Second)
c.Client.Terminate(reason)
}()

View file

@ -133,12 +133,6 @@ func (c *socketClient) Terminate(reason string) {
}
c.terminated = true
c.conn.Close()
go func() {
c.wgEvents.Wait()
close(c.events)
close(c.commands)
}()
}
func (c *socketClient) Terminated() bool {

View file

@ -135,13 +135,13 @@ func (s *server) addClient(c *serverClient) {
}
func (s *server) removeClient(c *serverClient) {
go func() {
g := s.gameByClient(c)
if g != nil {
g.removeClient(c)
}
c.Terminate("")
}()
g := s.gameByClient(c)
if g != nil {
g.removeClient(c)
}
c.Terminate("")
close(c.commands)
s.clientsLock.Lock()
defer s.clientsLock.Unlock()