X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/94742e2f44ca08a64c1851aa1a00c3e9f266d0e9..de7d2b1d64a8c441db09de5cc3284d4fab0bd44b:/hotline/server.go diff --git a/hotline/server.go b/hotline/server.go index 164b93e..e28ff9a 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -152,13 +152,12 @@ func (s *Server) sendTransaction(t Transaction) error { } s.mux.Lock() + defer s.mux.Unlock() client := s.Clients[uint16(clientID)] if client == nil { return fmt.Errorf("invalid client id %v", *t.clientID) } - s.mux.Unlock() - b, err := t.MarshalBinary() if err != nil { return err @@ -565,8 +564,13 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser scanner.Scan() + // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the + // scanner re-uses the buffer for subsequent scans. + buf := make([]byte, len(scanner.Bytes())) + copy(buf, scanner.Bytes()) + var clientLogin Transaction - if _, err := clientLogin.Write(scanner.Bytes()); err != nil { + if _, err := clientLogin.Write(buf); err != nil { return err }