diff --git a/changelog/13146.txt b/changelog/13146.txt new file mode 100644 index 000000000..6bbe85e35 --- /dev/null +++ b/changelog/13146.txt @@ -0,0 +1,3 @@ +```release-note:bug +sdk/queue: move lock before length check to prevent panics. +``` \ No newline at end of file diff --git a/sdk/queue/priority_queue.go b/sdk/queue/priority_queue.go index a0fda087f..399484177 100644 --- a/sdk/queue/priority_queue.go +++ b/sdk/queue/priority_queue.go @@ -85,13 +85,13 @@ func (pq *PriorityQueue) Len() int { // wrapper/convenience method that calls heap.Pop, so consumers do not need to // invoke heap functions directly func (pq *PriorityQueue) Pop() (*Item, error) { - if pq.Len() == 0 { - return nil, ErrEmpty - } - pq.lock.Lock() defer pq.lock.Unlock() + if pq.data.Len() == 0 { + return nil, ErrEmpty + } + item := heap.Pop(&pq.data).(*Item) delete(pq.dataMap, item.Key) return item, nil