mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
Fix for panic from a bug in selecting a block and an index when num blocks > 32 and we used new binary search in NumPending().
The reason would be that we were not accounting for gaps as mb.first.seq can move. The behavior should always return a valid index and mb if seq is inclusive of range from first to last. The panic could orphan held locks for filestore, consumer and possibly stream. Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
@@ -2452,7 +2452,10 @@ func (fs *fileStore) NumPending(sseq uint64, filter string, lastPerSubject bool)
|
||||
|
||||
// See if we need to figure out starting block per sseq.
|
||||
if sseq > fs.state.FirstSeq {
|
||||
seqStart, _ = fs.selectMsgBlockWithIndex(sseq)
|
||||
// This should not, but can return -1, so make sure we check to avoid panic below.
|
||||
if seqStart, _ = fs.selectMsgBlockWithIndex(sseq); seqStart < 0 {
|
||||
seqStart = 0
|
||||
}
|
||||
}
|
||||
|
||||
var tsa, fsa [32]string
|
||||
@@ -4738,6 +4741,7 @@ func (fs *fileStore) selectMsgBlock(seq uint64) *msgBlock {
|
||||
return mb
|
||||
}
|
||||
|
||||
// Lock should be held.
|
||||
func (fs *fileStore) selectMsgBlockWithIndex(seq uint64) (int, *msgBlock) {
|
||||
// Check for out of range.
|
||||
if seq < fs.state.FirstSeq || seq > fs.state.LastSeq {
|
||||
@@ -4765,6 +4769,13 @@ func (fs *fileStore) selectMsgBlockWithIndex(seq uint64) (int, *msgBlock) {
|
||||
if seq > last {
|
||||
low = mid + 1
|
||||
} else if seq < first {
|
||||
// A message block's first sequence can change here meaning we could find a gap.
|
||||
// We want to behave like above, which if inclusive (we check at start) should
|
||||
// always return an index and a valid mb.
|
||||
// If we have a gap then our seq would be > fs.blks[mid-1].last.seq
|
||||
if mid == 0 || seq > atomic.LoadUint64(&fs.blks[mid-1].last.seq) {
|
||||
return mid, mb
|
||||
}
|
||||
high = mid - 1
|
||||
} else {
|
||||
return mid, mb
|
||||
|
||||
Reference in New Issue
Block a user