Skip to content

Commit

Permalink
fix: wait for notifier/subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Sep 16, 2024
1 parent 89fee4b commit 77fc258
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 10 additions & 4 deletions keysign/signature_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,16 @@ func TestSignatureNotifierBroadcastFirst(t *testing.T) {
p1, p2,
}))

n1.notifierLock.Lock()
assert.Contains(t, n1.notifiers, messageID)
notifier := n1.notifiers[messageID]
n1.notifierLock.Unlock()
var notifier *notifier
var ok bool
assert.Eventually(t, func() bool {
n1.notifierLock.Lock()
defer n1.notifierLock.Unlock()

notifier, ok = n1.notifiers[messageID]
return ok
}, time.Second, time.Millisecond*100)

assert.False(t, notifier.readyToProcess())
assert.Equal(t, defaultNotifierTTL, notifier.ttl)

Expand Down
15 changes: 14 additions & 1 deletion p2p/communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (c *Communication) readFromStream(stream network.Stream) {
return
}
c.logger.Debug().Msgf(">>>>>>>[%s] %s", wrappedMsg.MessageType, string(wrappedMsg.Payload))
channel := c.getSubscriber(wrappedMsg.MessageType, wrappedMsg.MsgID)
channel := c.getSubscriberWithRetry(wrappedMsg.MessageType, wrappedMsg.MsgID)
if nil == channel {
c.logger.Debug().Msgf("no MsgID %s found for this message", wrappedMsg.MsgID)
c.logger.Debug().Msgf("no MsgID %s found for this message", wrappedMsg.MessageType)
Expand Down Expand Up @@ -445,6 +445,19 @@ func (c *Communication) getSubscriber(topic messages.THORChainTSSMessageType, ms
return messageIDSubscribers.GetSubscriber(msgID)
}

// getSubscriberWithRetry tries to get a subscriber a few times to avoid race conditions
func (c *Communication) getSubscriberWithRetry(topic messages.THORChainTSSMessageType, msgID string) chan *Message {
var res chan *Message
for i := 0; i < 3; i++ {
res = c.getSubscriber(topic, msgID)
if res != nil {
return res
}
time.Sleep(time.Millisecond * 50)
}
return res
}

func (c *Communication) CancelSubscribe(topic messages.THORChainTSSMessageType, msgID string) {
c.subscriberLocker.Lock()
defer c.subscriberLocker.Unlock()
Expand Down

0 comments on commit 77fc258

Please sign in to comment.