Skip to content

🐛 Priorityqueue: Don't block on Get when queue is shutdown #3243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/controller/priorityqueue/priorityqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ func (w *priorityqueue[T]) GetWithPriority() (_ T, priority int, shutdown bool)
w.waiters.Add(1)

w.notifyItemOrWaiterAdded()

// ref: https://github.com/kubernetes-sigs/controller-runtime/issues/3239
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alvaroaleman Shouldn't we do this at the beginning of the func?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if w.shutdown.Load() {
var zero T
return zero, 0, true
}

item := <-w.get

return item.Key, item.Priority, w.shutdown.Load()
Expand Down
20 changes: 20 additions & 0 deletions pkg/controller/priorityqueue/priorityqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,26 @@ var _ = Describe("Controllerworkqueue", func() {
Expect(metrics.depth["test"]).To(Equal(map[int]int{0: 2}))
})

// ref: https://github.com/kubernetes-sigs/controller-runtime/issues/3239
It("Get from priority queue might get stuck when the priority queue is shut down", func() {
q, _ := newQueue()

q.Add("baz")
// shut down
q.ShutDown()
q.AddWithOpts(AddOpts{After: time.Second}, "foo")
Copy link
Member

@sbueringer sbueringer Jun 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Add still adds the item to the shutdown queue. Is this the expected behavior?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alvaroaleman What's your take on this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't really keep anyone from calling Add as it has no return but we can stop acting on it: #3250

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's what I was thinking


item, priority, isShutDown := q.GetWithPriority()
Expect(item).To(Equal(""))
Expect(priority).To(Equal(0))
Expect(isShutDown).To(BeTrue())

item1, priority1, isShutDown := q.GetWithPriority()
Expect(item1).To(Equal(""))
Expect(priority1).To(Equal(0))
Expect(isShutDown).To(BeTrue())
})

It("items are included in Len() and the queueDepth metric once they are ready", func() {
q, metrics := newQueue()
defer q.ShutDown()
Expand Down
Loading