Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential data race in sentinel #9735

Merged
merged 1 commit into from
Mar 18, 2024
Merged

potential data race in sentinel #9735

merged 1 commit into from
Mar 18, 2024

Conversation

domiwei
Copy link
Member

@domiwei domiwei commented Mar 17, 2024

No description provided.

@domiwei domiwei requested a review from Giulio2002 March 17, 2024 18:42
@@ -170,8 +167,18 @@ func (s *Sentinel) forkWatcher() {
return
}
if prevDigest != digest {
subs := s.subManager.subscriptions
for path, sub := range subs {
copy := func() map[string]*GossipSubscription {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you are right there is a race, however this is not the correct fix, you still use the pointers which means that effectively you are not making a copy. i think a more elegant fix is to use sync.Map as the type in s.subManager.subscriptions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I just tried to resolve data race issue of concurrent r/w on map, but exactly they share the same GossipSubscription address. So the risk turns to be double close on GossipSubscription structure. That's why I introduce closeOnce.
For me more ideal way is to make subscription manipulations single thread like:

for {
  select{
  case <- addSubs:
  case <- removeSubs:
  case <- ticker.C:
  }
}

but we can see if really need this refactor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, closeOnce will save you only from concurrent close

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the actual subscription object is thread-safe fyi, the closeOnce part is fine, but what I would like you to do is: instead of doing a map copy, to just use a sync.Map

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem

@domiwei domiwei merged commit a15603a into devel Mar 18, 2024
6 checks passed
@domiwei domiwei deleted the gossip_potential_data_race branch March 18, 2024 03:12
@Giulio2002 Giulio2002 restored the gossip_potential_data_race branch March 18, 2024 09:56
Giulio2002 added a commit that referenced this pull request Mar 18, 2024
@Giulio2002
Copy link
Contributor

I did not mean to approve it

@domiwei domiwei added the caplin label Apr 17, 2024
@AskAlexSharov AskAlexSharov deleted the gossip_potential_data_race branch July 5, 2024 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants