From bce446c5b623d53f529eb6f655f85cfd9a1e112b Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Wed, 27 Mar 2024 16:47:15 +0200 Subject: [PATCH] nfd-master: protect node updater pool queueing with a lock Prevents races when (re-)starting the queue. There are no reports on issues related to this (and I haven't come up with any actual failure path in the current code) but better to be safe and follow the best practices. --- pkg/nfd-master/nfd-master.go | 4 ++-- pkg/nfd-master/node-updater-pool.go | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/nfd-master/nfd-master.go b/pkg/nfd-master/nfd-master.go index 7b9dcce6c6..f9d3367583 100644 --- a/pkg/nfd-master/nfd-master.go +++ b/pkg/nfd-master/nfd-master.go @@ -442,7 +442,7 @@ func (m *nfdMaster) nfdAPIUpdateHandler() { } } else { for nodeName := range updateNodes { - m.nodeUpdaterPool.queue.Add(nodeName) + m.nodeUpdaterPool.addNode(nodeName) } } @@ -710,7 +710,7 @@ func (m *nfdMaster) nfdAPIUpdateAllNodes() error { } for _, node := range nodes.Items { - m.nodeUpdaterPool.queue.Add(node.Name) + m.nodeUpdaterPool.addNode(node.Name) } return nil diff --git a/pkg/nfd-master/node-updater-pool.go b/pkg/nfd-master/node-updater-pool.go index 99948cc5f4..4ca03003d2 100644 --- a/pkg/nfd-master/node-updater-pool.go +++ b/pkg/nfd-master/node-updater-pool.go @@ -28,7 +28,7 @@ import ( type nodeUpdaterPool struct { queue workqueue.RateLimitingInterface - sync.Mutex + sync.RWMutex wg sync.WaitGroup nfdMaster *nfdMaster @@ -114,3 +114,9 @@ func (u *nodeUpdaterPool) stop() { u.queue.ShutDown() u.wg.Wait() } + +func (u *nodeUpdaterPool) addNode(nodeName string) { + u.RLock() + defer u.RUnlock() + u.queue.Add(nodeName) +}