From 660a7be134e9264e93b0f12165fb7b6e528689af Mon Sep 17 00:00:00 2001 From: Jason O'Donnell <2160810+jasonodonnell@users.noreply.github.com> Date: Mon, 29 Nov 2021 14:54:00 -0500 Subject: [PATCH] sdk/queue: move lock before checking queue length (#13146) * sdk/queue: move lock before checking queue length * Add changelog --- changelog/13146.txt | 3 +++ sdk/queue/priority_queue.go | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 changelog/13146.txt 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