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

Fix for data race in memstore.LoadNextMsg #4552

Merged
merged 1 commit into from Sep 18, 2023
Merged

Conversation

wallyqs
Copy link
Member

@wallyqs wallyqs commented Sep 17, 2023

Fix for a data race that was found in memory storage while working on a fix for #4551

WARNING: DATA RACE
Read at 0x00c0000a6e38 by goroutine 412:
  github.com/nats-io/nats-server/v2/server.(*memStore).LoadNextMsg()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/memstore.go:954 +0x644
  github.com/nats-io/nats-server/v2/server.(*consumer).getNextMsg()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/consumer.go:3413 +0xb22
  github.com/nats-io/nats-server/v2/server.(*consumer).loopAndGatherMsgs()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/consumer.go:3824 +0x484
  github.com/nats-io/nats-server/v2/server.(*consumer).setLeader.func3()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/consumer.go:1259 +0x44

Previous write at 0x00c0000a6e38 by goroutine 336:
  github.com/nats-io/nats-server/v2/server.(*memStore).recalculateFirstForSubj()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/memstore.go:1070 +0x6a5
  github.com/nats-io/nats-server/v2/server.(*memStore).LoadNextMsg()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/memstore.go:955 +0x653
  github.com/nats-io/nats-server/v2/server.(*consumer).getNextMsg()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/consumer.go:3413 +0xb22
  github.com/nats-io/nats-server/v2/server.(*consumer).loopAndGatherMsgs()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/consumer.go:3824 +0x484
  github.com/nats-io/nats-server/v2/server.(*consumer).setLeader.func3()
      /Users/wallyqs/repos/nats-dev/src/github.com/nats-io/nats-server/server/consumer.go:1259 +0x44

@wallyqs wallyqs marked this pull request as ready for review September 17, 2023 19:52
@wallyqs wallyqs requested a review from a team as a code owner September 17, 2023 19:52
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

On the whole I'm a bit nervous about releasing and relocking in this way as it can sometimes lead to subtle bugs or deadlocks, but short of refactoring this completely, this is probably the quickest fix.

Copy link
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

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

Is there a way to not do this?

Can we introduce a new func that expects the locks held?

@neilalexander
Copy link
Member

I think the function already expects the write lock held, which is the issue. At that point we have a read lock and not a write lock.

@derekcollison
Copy link
Member

I think I would prefer, for now, to move the lock for LoadNextMsg to a write lock and then do a lock already held version of recalulateFirstForSubj().

Signed-off-by: Waldemar Quevedo <wally@synadia.com>
Copy link
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

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

LGTM

@wallyqs wallyqs merged commit fc51af9 into main Sep 18, 2023
2 checks passed
@wallyqs wallyqs deleted the mem-recalculate-race branch September 18, 2023 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants