Skip to content

Commit 2a84088

Browse files
authored
Merge pull request kubernetes#127064 from macsko/dont_panic_when_scheduling_queue_empty
Don't panic when popping from empty scheduling queue
2 parents 09115bd + 1f157bc commit 2a84088

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

pkg/scheduler/backend/heap/heap.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func (h *data[T]) Len() int { return len(h.queue) }
8585
// Swap implements swapping of two elements in the heap. This is a part of standard
8686
// heap interface and should never be called directly.
8787
func (h *data[T]) Swap(i, j int) {
88+
if i < 0 || j < 0 {
89+
return
90+
}
8891
h.queue[i], h.queue[j] = h.queue[j], h.queue[i]
8992
item := h.items[h.queue[i]]
9093
item.index = i
@@ -102,6 +105,9 @@ func (h *data[T]) Push(kv interface{}) {
102105

103106
// Pop is supposed to be called by container/heap.Pop only.
104107
func (h *data[T]) Pop() interface{} {
108+
if len(h.queue) == 0 {
109+
return nil
110+
}
105111
key := h.queue[len(h.queue)-1]
106112
h.queue = h.queue[0 : len(h.queue)-1]
107113
item, ok := h.items[key]

pkg/scheduler/backend/heap/heap_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ func TestHeapBasic(t *testing.T) {
9494
}
9595
prevNum = num
9696
}
97+
98+
_, err := h.Pop()
99+
if err == nil {
100+
t.Errorf("expected Pop() to error on empty heap")
101+
}
97102
}
98103

99104
// TestHeap_AddOrUpdate_Add tests add capabilities of Heap.AddOrUpdate

0 commit comments

Comments
 (0)