From 013b6b1d6014e26203ea7b07b5b398ebd90e310b Mon Sep 17 00:00:00 2001 From: sashass1315 Date: Mon, 22 Sep 2025 23:12:56 +0300 Subject: [PATCH] fix: race in PriorityQueue.Pop by checking emptiness under write lock (#15726) * fix: race in PriorityQueue.Pop by checking emptiness under write lock * Create sashass1315_fix-priority-queue-pop-lock-race * Update changelog/sashass1315_fix-priority-queue-pop-lock-race Co-authored-by: Preston Van Loon * Move changelog/sashass1315_fix-priority-queue-pop-lock-race to changelog/sashass1315_fix-priority-queue-pop-lock-race.md * Fix bullet in changelog --------- Co-authored-by: Preston Van Loon Co-authored-by: Preston Van Loon --- changelog/sashass1315_fix-priority-queue-pop-lock-race.md | 2 ++ container/queue/priority_queue.go | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 changelog/sashass1315_fix-priority-queue-pop-lock-race.md diff --git a/changelog/sashass1315_fix-priority-queue-pop-lock-race.md b/changelog/sashass1315_fix-priority-queue-pop-lock-race.md new file mode 100644 index 0000000000..237d576b54 --- /dev/null +++ b/changelog/sashass1315_fix-priority-queue-pop-lock-race.md @@ -0,0 +1,2 @@ +### Fixed +- fix race in PriorityQueue.Pop by checking emptiness under write lock. (#15726) diff --git a/container/queue/priority_queue.go b/container/queue/priority_queue.go index 9a240c43bc..3c3d339fc0 100644 --- a/container/queue/priority_queue.go +++ b/container/queue/priority_queue.go @@ -86,13 +86,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, ok := heap.Pop(&pq.data).(*Item) if !ok { return nil, errors.New("unknown type")