Skip to content

Commit

Permalink
fix race with socket disconnect and announce
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfeldman committed Apr 2, 2024
1 parent e9ee5c9 commit e848a39
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,15 @@ func (s *Server) handleAnnounce(msg *Msg) {
var ann ThingMsgAnnounce
msg.Unmarshal(&ann)

s.socketsMu.Lock()
defer s.socketsMu.Unlock()

socket := msg.src
if _, ok = s.sockets[socket]; !ok {
fmt.Printf("Ignoring announcement: socket already disconnected: %s\r\n", socket)
socket.Close()
return
}

fmt.Printf("\r\n*** ANNOUNCE %s %s %s %s\r\n", socket, ann.Id, ann.Model, ann.Name)

Expand Down Expand Up @@ -147,14 +155,6 @@ func (s *Server) handleAnnounce(msg *Msg) {
thinger.SetOnline(true)
socket.SetTag(id)

s.socketsMu.Lock()
defer s.socketsMu.Unlock()

_, ok = s.sockets[socket]
if !ok {
s.dumpSockets("announce")
}

s.sockets[socket] = thinger

msg.Marshal(&ThingMsg{"get/state"}).Reply()
Expand Down

0 comments on commit e848a39

Please sign in to comment.