Merge pull request #1469 from watjurk/fix/autorelay-race

fix race condition in relayFinder
This commit is contained in:
Steven Allen 2022-05-03 11:20:00 -04:00 committed by GitHub
commit 7b47c4e6b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -61,8 +61,10 @@ type relayFinder struct {
conf *config conf *config
refCount sync.WaitGroup refCount sync.WaitGroup
ctxCancel context.CancelFunc
ctxCancel context.CancelFunc
ctxCancelMx sync.Mutex
peerChan <-chan peer.AddrInfo peerChan <-chan peer.AddrInfo
@ -581,6 +583,8 @@ func (rf *relayFinder) relayAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
} }
func (rf *relayFinder) Start() error { func (rf *relayFinder) Start() error {
rf.ctxCancelMx.Lock()
defer rf.ctxCancelMx.Unlock()
if rf.ctxCancel != nil { if rf.ctxCancel != nil {
return errors.New("relayFinder already running") return errors.New("relayFinder already running")
} }
@ -596,6 +600,8 @@ func (rf *relayFinder) Start() error {
} }
func (rf *relayFinder) Stop() error { func (rf *relayFinder) Stop() error {
rf.ctxCancelMx.Lock()
defer rf.ctxCancelMx.Unlock()
log.Debug("stopping relay finder") log.Debug("stopping relay finder")
if rf.ctxCancel != nil { if rf.ctxCancel != nil {
rf.ctxCancel() rf.ctxCancel()