Skip to content

Commit cfb9486

Browse files
committed
DRA taint eviction: avoid nil panic
The timed worker queue actually can have nil entries in its map if the work was kicked off immediately. This looks like an unnecessary special case (it would be fine to call AfterFunc with a duration <= 0 and it would do the right thing), but to avoid more sweeping changes the fix consists of documenting this special behavior and adding a nil check.
1 parent 56adcd0 commit cfb9486

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

pkg/controller/tainteviction/timed_workers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type TimedWorker struct {
5757
}
5858

5959
// createWorker creates a TimedWorker that will execute `f` not earlier than `fireAt`.
60+
// Returns nil if the work was started immediately and doesn't need a timer.
6061
func createWorker(ctx context.Context, args *WorkArgs, createdAt time.Time, fireAt time.Time, f func(ctx context.Context, fireAt time.Time, args *WorkArgs) error, clock clock.WithDelayedExecution) *TimedWorker {
6162
delay := fireAt.Sub(createdAt)
6263
logger := klog.FromContext(ctx)
@@ -90,6 +91,7 @@ func (w *TimedWorker) Cancel() {
9091
type TimedWorkerQueue struct {
9192
sync.Mutex
9293
// map of workers keyed by string returned by 'KeyFromWorkArgs' from the given worker.
94+
// Entries may be nil if the work didn't need a timer and is already running.
9395
workers map[string]*TimedWorker
9496
workFunc func(ctx context.Context, fireAt time.Time, args *WorkArgs) error
9597
clock clock.WithDelayedExecution
@@ -145,6 +147,10 @@ func (q *TimedWorkerQueue) UpdateWork(ctx context.Context, args *WorkArgs, creat
145147
q.Lock()
146148
defer q.Unlock()
147149
if worker, exists := q.workers[key]; exists {
150+
if worker == nil {
151+
logger.V(4).Info("Keeping existing work, already in progress", "item", key)
152+
return
153+
}
148154
if worker.FireAt.Compare(fireAt) == 0 {
149155
logger.V(4).Info("Keeping existing work, same time", "item", key, "createTime", worker.CreatedAt, "firedTime", worker.FireAt)
150156
return

0 commit comments

Comments
 (0)