Skip to content

Commit

Permalink
sdk/queue: move lock before checking queue length (#13146) (#13297)
Browse files Browse the repository at this point in the history
* sdk/queue: move lock before checking queue length

* Add changelog
  • Loading branch information
jasonodonnell committed Nov 29, 2021
1 parent 8d2b80c commit e36348b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelog/13146.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
sdk/queue: move lock before length check to prevent panics.
```
8 changes: 4 additions & 4 deletions sdk/queue/priority_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e36348b

Please sign in to comment.