Skip to content

Commit 105008b

Browse files
gluk256nonsense
authored andcommitted
swarm/pss: fixing race condition (#18487)
1 parent 15b9b39 commit 105008b

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

‎swarm/pss/pss.go‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ func (p *Pss) Register(topic *Topic, hndlr *handler) func() {
340340
}
341341
return func() { p.deregister(topic, hndlr) }
342342
}
343+
343344
func (p *Pss) deregister(topic *Topic, hndlr *handler) {
344345
p.handlersMu.Lock()
345346
defer p.handlersMu.Unlock()
@@ -362,13 +363,6 @@ func (p *Pss) deregister(topic *Topic, hndlr *handler) {
362363
delete(handlers, hndlr)
363364
}
364365

365-
// get all registered handlers for respective topics
366-
func (p *Pss) getHandlers(topic Topic) map[*handler]bool {
367-
p.handlersMu.RLock()
368-
defer p.handlersMu.RUnlock()
369-
return p.handlers[topic]
370-
}
371-
372366
// Filters incoming messages for processing or forwarding.
373367
// Check if address partially matches
374368
// If yes, it CAN be for us, and we process it
@@ -427,7 +421,6 @@ func (p *Pss) handlePssMsg(ctx context.Context, msg interface{}) error {
427421
}
428422
}
429423
return nil
430-
431424
}
432425

433426
// Entry point to processing a message for which the current node can be the intended recipient.
@@ -472,13 +465,22 @@ func (p *Pss) process(pssmsg *PssMsg, raw bool, prox bool) error {
472465
p.executeHandlers(psstopic, payload, from, raw, prox, asymmetric, keyid)
473466

474467
return nil
468+
}
475469

470+
// copy all registered handlers for respective topic in order to avoid data race or deadlock
471+
func (p *Pss) getHandlers(topic Topic) (ret []*handler) {
472+
p.handlersMu.RLock()
473+
defer p.handlersMu.RUnlock()
474+
for k := range p.handlers[topic] {
475+
ret = append(ret, k)
476+
}
477+
return ret
476478
}
477479

478480
func (p *Pss) executeHandlers(topic Topic, payload []byte, from PssAddress, raw bool, prox bool, asymmetric bool, keyid string) {
479481
handlers := p.getHandlers(topic)
480482
peer := p2p.NewPeer(enode.ID{}, fmt.Sprintf("%x", from), []p2p.Cap{})
481-
for h := range handlers {
483+
for _, h := range handlers {
482484
if !h.caps.raw && raw {
483485
log.Warn("norawhandler")
484486
continue

0 commit comments

Comments
 (0)