sdk/queue: move lock before checking queue length (#13146)

* sdk/queue: move lock before checking queue length

* Add changelog
This commit is contained in:
Jason O'Donnell 2021-11-29 14:54:00 -05:00 committed by GitHub
parent f39f1ce8de
commit 660a7be134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

3
changelog/13146.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
sdk/queue: move lock before length check to prevent panics.
```

View File

@ -85,13 +85,13 @@ func (pq *PriorityQueue) Len() int {
// wrapper/convenience method that calls heap.Pop, so consumers do not need to // wrapper/convenience method that calls heap.Pop, so consumers do not need to
// invoke heap functions directly // invoke heap functions directly
func (pq *PriorityQueue) Pop() (*Item, error) { func (pq *PriorityQueue) Pop() (*Item, error) {
if pq.Len() == 0 {
return nil, ErrEmpty
}
pq.lock.Lock() pq.lock.Lock()
defer pq.lock.Unlock() defer pq.lock.Unlock()
if pq.data.Len() == 0 {
return nil, ErrEmpty
}
item := heap.Pop(&pq.data).(*Item) item := heap.Pop(&pq.data).(*Item)
delete(pq.dataMap, item.Key) delete(pq.dataMap, item.Key)
return item, nil return item, nil