From ab055e9ba4a6f74d82f6158d303e7b4d5c2398c4 Mon Sep 17 00:00:00 2001 From: paco Date: Mon, 2 Aug 2021 11:42:10 +0800 Subject: [PATCH] fix data race in kubelet volume test: add lock Signed-off-by: Paco Xu Co-authored-by: Jian Zeng --- pkg/kubelet/kubelet_volumes_test.go | 6 +++--- pkg/kubelet/pod_workers_test.go | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/kubelet/kubelet_volumes_test.go b/pkg/kubelet/kubelet_volumes_test.go index fa041f709af..385c4ee6e52 100644 --- a/pkg/kubelet/kubelet_volumes_test.go +++ b/pkg/kubelet/kubelet_volumes_test.go @@ -315,8 +315,8 @@ func TestVolumeUnmountAndDetachControllerDisabled(t *testing.T) { 1 /* expectedSetUpCallCount */, testKubelet.volumePlugin)) // Remove pod - // TODO: this may not be threadsafe (technically waitForVolumeUnmount) - kubelet.podWorkers.(*fakePodWorkers).removeRuntime = map[types.UID]bool{pod.UID: true} + // TODO: technically waitForVolumeUnmount + kubelet.podWorkers.(*fakePodWorkers).setPodRuntimeBeRemoved(pod.UID) kubelet.podManager.SetPods([]*v1.Pod{}) assert.NoError(t, kubelet.volumeManager.WaitForUnmount(pod)) @@ -504,7 +504,7 @@ func TestVolumeUnmountAndDetachControllerEnabled(t *testing.T) { 1 /* expectedSetUpCallCount */, testKubelet.volumePlugin)) // Remove pod - kubelet.podWorkers.(*fakePodWorkers).removeRuntime = map[types.UID]bool{pod.UID: true} + kubelet.podWorkers.(*fakePodWorkers).setPodRuntimeBeRemoved(pod.UID) kubelet.podManager.SetPods([]*v1.Pod{}) assert.NoError(t, waitForVolumeUnmount(kubelet.volumeManager, pod)) diff --git a/pkg/kubelet/pod_workers_test.go b/pkg/kubelet/pod_workers_test.go index 50705275ecd..f2336c6ef72 100644 --- a/pkg/kubelet/pod_workers_test.go +++ b/pkg/kubelet/pod_workers_test.go @@ -105,6 +105,11 @@ func (f *fakePodWorkers) ShouldPodRuntimeBeRemoved(uid types.UID) bool { defer f.statusLock.Unlock() return f.removeRuntime[uid] } +func (f *fakePodWorkers) setPodRuntimeBeRemoved(uid types.UID) { + f.statusLock.Lock() + defer f.statusLock.Unlock() + f.removeRuntime = map[types.UID]bool{uid: true} +} func (f *fakePodWorkers) ShouldPodContentBeRemoved(uid types.UID) bool { f.statusLock.Lock() defer f.statusLock.Unlock()