From edf8869ff27b5dd16a61067842a38c1c5fa6273b Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 5 May 2023 14:13:55 -0400 Subject: [PATCH] fix(communities/manager): use sync.map to fix concurrent map writes err (#3445) --- VERSION | 2 +- protocol/communities/manager.go | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/VERSION b/VERSION index ce66bc27e..48c7ccf0c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.150.0 +0.150.1 diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 3f7bcb409..33602fbfe 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -76,7 +76,7 @@ type Manager struct { walletConfig *params.WalletConfig historyArchiveTasksWaitGroup sync.WaitGroup historyArchiveTasks sync.Map // stores `chan struct{}` - periodicMemberPermissionsTasks map[string]chan struct{} + periodicMemberPermissionsTasks sync.Map // stores `chan struct{}` torrentTasks map[string]metainfo.Hash historyArchiveDownloadTasks map[string]*HistoryArchiveDownloadTask } @@ -150,16 +150,15 @@ func NewManager(identity *ecdsa.PrivateKey, db *sql.DB, encryptor *encryption.Pr } manager := &Manager{ - logger: logger, - stdoutLogger: stdoutLogger, - encryptor: encryptor, - identity: identity, - quit: make(chan struct{}), - transport: transport, - torrentConfig: torrentConfig, - periodicMemberPermissionsTasks: make(map[string]chan struct{}), - torrentTasks: make(map[string]metainfo.Hash), - historyArchiveDownloadTasks: make(map[string]*HistoryArchiveDownloadTask), + logger: logger, + stdoutLogger: stdoutLogger, + encryptor: encryptor, + identity: identity, + quit: make(chan struct{}), + transport: transport, + torrentConfig: torrentConfig, + torrentTasks: make(map[string]metainfo.Hash), + historyArchiveDownloadTasks: make(map[string]*HistoryArchiveDownloadTask), persistence: &Persistence{ logger: logger, db: db, @@ -618,12 +617,12 @@ func (m *Manager) checkMemberPermissions(communityID types.HexBytes) error { func (m *Manager) CheckMemberPermissionsPeriodically(communityID types.HexBytes) { - if _, exists := m.periodicMemberPermissionsTasks[communityID.String()]; exists { + if _, exists := m.periodicMemberPermissionsTasks.Load(communityID.String()); exists { return } cancel := make(chan struct{}) - m.periodicMemberPermissionsTasks[communityID.String()] = cancel + m.periodicMemberPermissionsTasks.Store(communityID.String(), cancel) ticker := time.NewTicker(memberPermissionsCheckInterval) defer ticker.Stop() @@ -636,7 +635,7 @@ func (m *Manager) CheckMemberPermissionsPeriodically(communityID types.HexBytes) m.logger.Debug("failed to check member permissions", zap.Error(err)) } case <-cancel: - delete(m.periodicMemberPermissionsTasks, communityID.String()) + m.periodicMemberPermissionsTasks.Delete(communityID.String()) return } } @@ -665,8 +664,8 @@ func (m *Manager) DeleteCommunityTokenPermission(request *requests.DeleteCommuni // if not we can stop checking token criteria on-chain // for members becomeMemberPermissions := community.TokenPermissionsByType(protobuf.CommunityTokenPermission_BECOME_MEMBER) - if cancel, exists := m.periodicMemberPermissionsTasks[community.IDString()]; exists && len(becomeMemberPermissions) == 0 { - close(cancel) + if cancel, exists := m.periodicMemberPermissionsTasks.Load(community.IDString()); exists && len(becomeMemberPermissions) == 0 { + close(cancel.(chan struct{})) // Need to cast to the chan } m.publish(&Subscription{Community: community})