Skip to content

Fix potential deadlock in MultiLastSeqs #6899

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

Merged
merged 2 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion server/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3042,7 +3042,13 @@ func (fs *fileStore) SubjectsState(subject string) map[string]SimpleState {
func (fs *fileStore) AllLastSeqs() ([]uint64, error) {
fs.mu.RLock()
defer fs.mu.RUnlock()
return fs.allLastSeqsLocked()
}

// allLastSeqsLocked will return a sorted list of last sequences for all
// subjects, but won't take the lock to do it, to avoid the issue of compounding
// read locks causing a deadlock with a write lock.
func (fs *fileStore) allLastSeqsLocked() ([]uint64, error) {
if fs.state.Msgs == 0 || fs.noTrackSubjects() {
return nil, nil
}
Expand Down Expand Up @@ -3113,7 +3119,7 @@ func (fs *fileStore) MultiLastSeqs(filters []string, maxSeq uint64, maxAllowed i

// See if we can short circuit if we think they are asking for all last sequences and have no maxSeq or maxAllowed set.
if maxSeq == 0 && maxAllowed <= 0 && fs.filterIsAll(filters) {
return fs.AllLastSeqs()
return fs.allLastSeqsLocked()
}

lastBlkIndex := len(fs.blks) - 1
Expand Down
8 changes: 7 additions & 1 deletion server/memstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,13 @@ func (ms *memStore) SubjectsState(subject string) map[string]SimpleState {
func (ms *memStore) AllLastSeqs() ([]uint64, error) {
ms.mu.RLock()
defer ms.mu.RUnlock()
return ms.allLastSeqsLocked()
}

// allLastSeqsLocked will return a sorted list of last sequences for all
// subjects, but won't take the lock to do it, to avoid the issue of compounding
// read locks causing a deadlock with a write lock.
func (ms *memStore) allLastSeqsLocked() ([]uint64, error) {
if len(ms.msgs) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -685,7 +691,7 @@ func (ms *memStore) MultiLastSeqs(filters []string, maxSeq uint64, maxAllowed in

// See if we can short circuit if we think they are asking for all last sequences and have no maxSeq or maxAllowed set.
if maxSeq == 0 && maxAllowed <= 0 && ms.filterIsAll(filters) {
return ms.AllLastSeqs()
return ms.allLastSeqsLocked()
}

// Implied last sequence.
Expand Down
Loading