From ed904ed79dcdbeec69d899d8713d246950d9f671 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 24 Jul 2019 15:42:06 -0700 Subject: [PATCH 1/2] fix race conditions around contact access --- go/gothings/gcd.go | 8 ++++++-- go/gothings/uistate.go | 24 +++++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/go/gothings/gcd.go b/go/gothings/gcd.go index 5de6210f..3ac7365f 100644 --- a/go/gothings/gcd.go +++ b/go/gothings/gcd.go @@ -308,7 +308,9 @@ func (this *GrandCentralDispatcher) savePeerSettings(onion, nick string) { event.Data: nick, })) - this.UIState.contacts[onion].DisplayName = nick + cif, _ := this.UIState.contacts.Load(onion) + c := cif.(*gobjects.Contact) + c.DisplayName = nick this.UIState.UpdateContact(onion) } @@ -352,7 +354,9 @@ func (this *GrandCentralDispatcher) saveGroupSettings(groupID, nick string) { event.Data: nick, })) - this.UIState.contacts[groupID].DisplayName = nick + cif, _ := this.UIState.contacts.Load(groupID) + c := cif.(*gobjects.Contact) + c.DisplayName = nick this.UIState.UpdateContact(groupID) } diff --git a/go/gothings/uistate.go b/go/gothings/uistate.go index 452108e5..9ad46e9d 100644 --- a/go/gothings/uistate.go +++ b/go/gothings/uistate.go @@ -13,20 +13,20 @@ import ( type InterfaceState struct { parentGcd *GrandCentralDispatcher - contacts map[string]*gobjects.Contact + contacts sync.Map // string : *gobjects.Contact messages sync.Map } func NewUIState(gcd *GrandCentralDispatcher) (uis InterfaceState) { - uis = InterfaceState{gcd, make(map[string]*gobjects.Contact), sync.Map{}} + uis = InterfaceState{gcd, sync.Map{}, sync.Map{}} return } func (this *InterfaceState) AddContact(c *gobjects.Contact) { if len(c.Handle) == 32 { // ADD GROUP - if _, found := this.contacts[c.Handle]; !found { + if _, found := this.contacts.Load(c.Handle); !found { this.parentGcd.AddContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted, c.Loading) - this.contacts[c.Handle] = c + this.contacts.Store(c.Handle, c) } return } else if len(c.Handle) != 56 { @@ -35,8 +35,8 @@ func (this *InterfaceState) AddContact(c *gobjects.Contact) { return } - if _, found := this.contacts[c.Handle]; !found { - this.contacts[c.Handle] = c + if _, found := this.contacts.Load(c.Handle); !found { + this.contacts.Store(c.Handle, c) this.parentGcd.AddContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted, false) if the.Peer.GetContact(c.Handle) == nil { the.Peer.AddContact(c.DisplayName, c.Handle, c.Trusted) @@ -46,7 +46,7 @@ func (this *InterfaceState) AddContact(c *gobjects.Contact) { } func (this *InterfaceState) GetContact(handle string) *gobjects.Contact { - if _, found := this.contacts[handle]; !found { + if _, found := this.contacts.Load(handle); !found { if len(handle) == 32 { group := the.Peer.GetGroup(handle) if group != nil { @@ -84,7 +84,12 @@ func (this *InterfaceState) GetContact(handle string) *gobjects.Contact { } } - return this.contacts[handle] + contactIntf, _ := this.contacts.Load(handle) + if contactIntf == nil { + return nil + } + contact := contactIntf.(*gobjects.Contact) + return contact } func (this *InterfaceState) AddSendMessageError(peer string, signature string, err string) { @@ -166,8 +171,9 @@ func (this *InterfaceState) GetMessages(handle string) []*gobjects.Message { } func (this *InterfaceState) UpdateContact(handle string) { - c, found := this.contacts[handle] + cif, found := this.contacts.Load(handle) if found { + c := cif.(*gobjects.Contact) this.parentGcd.UpdateContact(c.Handle, c.DisplayName, c.Image, c.Server, c.Badge, c.Status, c.Trusted, c.Loading) } } From 4c626dc922bcb6716b195e529e2cb4c6e282e647 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 25 Jul 2019 15:22:06 -0700 Subject: [PATCH 2/2] fix race condition where possible group/contact nick is not set right in ui --- go/characters/incominglistener.go | 1 - go/gothings/uistate.go | 12 ++++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/go/characters/incominglistener.go b/go/characters/incominglistener.go index 14ae72a3..40a58224 100644 --- a/go/characters/incominglistener.go +++ b/go/characters/incominglistener.go @@ -18,7 +18,6 @@ func IncomingListener(uiState *gothings.InterfaceState, subscribed chan bool) { the.EventBus.Subscribe(event.NewGroupInvite, q.EventChannel) the.EventBus.Subscribe(event.SendMessageToGroupError, q.EventChannel) the.EventBus.Subscribe(event.SendMessageToPeerError, q.EventChannel) - the.EventBus.Subscribe(event.JoinServer, q.EventChannel) the.EventBus.Subscribe(event.ServerStateChange, q.EventChannel) the.EventBus.Subscribe(event.PeerStateChange, q.EventChannel) subscribed <- true diff --git a/go/gothings/uistate.go b/go/gothings/uistate.go index 9ad46e9d..5185f559 100644 --- a/go/gothings/uistate.go +++ b/go/gothings/uistate.go @@ -50,9 +50,13 @@ func (this *InterfaceState) GetContact(handle string) *gobjects.Contact { if len(handle) == 32 { group := the.Peer.GetGroup(handle) if group != nil { + nick, exists := group.GetAttribute("nick") + if !exists { + nick = group.GroupID[:12] + } this.AddContact(&gobjects.Contact{ handle, - handle, + nick, cwutil.RandomGroupImage(handle), group.GroupServer, 0, @@ -68,9 +72,13 @@ func (this *InterfaceState) GetContact(handle string) *gobjects.Contact { } else { contact := the.Peer.GetContact(handle) if contact != nil && handle != contact.Onion { + nick, exists := contact.GetAttribute("name") + if !exists { + nick = contact.Onion + } this.AddContact(&gobjects.Contact{ handle, - handle, + nick, cwutil.RandomProfileImage(handle), "", 0,