Merge pull request #161 from libp2p/fix/races

fix: fix some race conditions in the ds address book
This commit is contained in:
Steven Allen 2021-07-16 19:44:59 -07:00 committed by GitHub
commit 87136c9300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -299,7 +299,16 @@ func (ab *dsAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, ttl tim
func (ab *dsAddrBook) latestPeerRecordSeq(p peer.ID) uint64 {
pr, err := ab.loadRecord(p, true, false)
if err != nil || len(pr.Addrs) == 0 || pr.CertifiedRecord == nil || len(pr.CertifiedRecord.Raw) == 0 {
if err != nil {
// We ignore the error because we don't want to fail storing a new record in this
// case.
log.Errorw("unable to load record", "peer", p, "error", err)
return 0
}
pr.RLock()
defer pr.RUnlock()
if len(pr.Addrs) == 0 || pr.CertifiedRecord == nil || len(pr.CertifiedRecord.Raw) == 0 {
return 0
}
return pr.CertifiedRecord.Seq
@ -552,13 +561,13 @@ func (ab *dsAddrBook) deleteAddrs(p peer.ID, addrs []ma.Multiaddr) (err error) {
return fmt.Errorf("failed to load peerstore entry for peer %v while deleting addrs, err: %v", p, err)
}
pr.Lock()
defer pr.Unlock()
if pr.Addrs == nil {
return nil
}
pr.Lock()
defer pr.Unlock()
pr.Addrs = deleteInPlace(pr.Addrs, addrs)
pr.dirty = true