Commit 9a0bd7ad authored by Lukasz Zimnoch's avatar Lukasz Zimnoch Committed by vyzo

Prevent multiple invocations of relay cancel function

parent 9a0d2f59
......@@ -676,12 +676,22 @@ func (p *PubSub) handleAddRelay(req *addRelayReq) {
p.rt.Join(topic)
}
req.resp <- func() {
// flag used to prevent calling cancel function multiple times
isCancelled := false
relayCancelFunc := func() {
if isCancelled {
return
}
select {
case p.rmRelay <- topic:
isCancelled = true
case <-p.ctx.Done():
}
}
req.resp <- relayCancelFunc
}
// handleRemoveRelay removes one relay reference from bookkeeping.
......
......@@ -629,7 +629,20 @@ func TestTopicRelayReuse(t *testing.T) {
t.Fatal("incorrect number of relays")
}
// only the first invocation should take effect
relay1Cancel()
relay1Cancel()
relay1Cancel()
pubsubs[0].eval <- func() {
res <- pubsubs[0].myRelays[topic] == 2
}
isCorrectNumber = <-res
if !isCorrectNumber {
t.Fatal("incorrect number of relays")
}
relay2Cancel()
relay3Cancel()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment