From 9ed4c58a1f1e20989b8218f484b8a44d7281d5a2 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Srivastav Date: Fri, 27 Oct 2023 15:39:33 +0530 Subject: [PATCH 1/3] Added mutex locks when accessing file object --- pkg/kubelet/kuberuntime/logs/logs_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/kubelet/kuberuntime/logs/logs_test.go b/pkg/kubelet/kuberuntime/logs/logs_test.go index 7b5e2b3a585..e1197f4e650 100644 --- a/pkg/kubelet/kuberuntime/logs/logs_test.go +++ b/pkg/kubelet/kuberuntime/logs/logs_test.go @@ -24,6 +24,7 @@ import ( "io" "os" "path/filepath" + "sync" "testing" "time" @@ -214,12 +215,12 @@ func TestReadLogs(t *testing.T) { } func TestReadRotatedLog(t *testing.T) { + var mu sync.RWMutex tmpDir := t.TempDir() file, err := os.CreateTemp(tmpDir, "logfile") if err != nil { assert.NoErrorf(t, err, "unable to create temp file") } - stdoutBuf := &bytes.Buffer{} stderrBuf := &bytes.Buffer{} containerID := "fake-container-id" @@ -240,7 +241,10 @@ func TestReadRotatedLog(t *testing.T) { Follow: true, } opts := NewLogOptions(&podLogOptions, time.Now()) - ReadLogs(ctx, file.Name(), containerID, opts, fakeRuntimeService, stdoutBuf, stderrBuf) + mu.Lock() + path := file.Name() + mu.Unlock() + ReadLogs(ctx, path, containerID, opts, fakeRuntimeService, stdoutBuf, stderrBuf) }(ctx) // log in stdout @@ -254,6 +258,7 @@ func TestReadRotatedLog(t *testing.T) { // Write 10 lines to log file. // Let ReadLogs start. time.Sleep(50 * time.Millisecond) + for line := 0; line < 10; line++ { // Write the first three lines to log file now := time.Now().Format(types.RFC3339NanoLenient) @@ -277,10 +282,13 @@ func TestReadRotatedLog(t *testing.T) { } newF := filepath.Join(dir, baseName) + mu.Lock() if file, err = os.Create(newF); err != nil { + mu.Unlock() assert.NoError(t, err, "unable to create new log file") return } + mu.Unlock() time.Sleep(20 * time.Millisecond) } } From 0e76e2c2bbfb8378be76b088841f4b631681b52b Mon Sep 17 00:00:00 2001 From: Abhishek Kr Srivastav Date: Fri, 27 Oct 2023 19:12:01 +0530 Subject: [PATCH 2/3] Added mutex locks when accessing file object : lint check fix --- pkg/kubelet/kuberuntime/logs/logs_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kubelet/kuberuntime/logs/logs_test.go b/pkg/kubelet/kuberuntime/logs/logs_test.go index e1197f4e650..205ae7ba7bb 100644 --- a/pkg/kubelet/kuberuntime/logs/logs_test.go +++ b/pkg/kubelet/kuberuntime/logs/logs_test.go @@ -244,7 +244,7 @@ func TestReadRotatedLog(t *testing.T) { mu.Lock() path := file.Name() mu.Unlock() - ReadLogs(ctx, path, containerID, opts, fakeRuntimeService, stdoutBuf, stderrBuf) + _ = ReadLogs(ctx, path, containerID, opts, fakeRuntimeService, stdoutBuf, stderrBuf) }(ctx) // log in stdout From 81bf3a59d1cb60d767c0f94db57aa99f446d2f4b Mon Sep 17 00:00:00 2001 From: Abhishek Kr Srivastav Date: Sat, 28 Oct 2023 17:32:13 +0530 Subject: [PATCH 3/3] Added mutex locks when accessing file object : addressed review comments --- pkg/kubelet/kuberuntime/logs/logs_test.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/pkg/kubelet/kuberuntime/logs/logs_test.go b/pkg/kubelet/kuberuntime/logs/logs_test.go index 205ae7ba7bb..5c99beeb9f2 100644 --- a/pkg/kubelet/kuberuntime/logs/logs_test.go +++ b/pkg/kubelet/kuberuntime/logs/logs_test.go @@ -24,7 +24,6 @@ import ( "io" "os" "path/filepath" - "sync" "testing" "time" @@ -215,7 +214,6 @@ func TestReadLogs(t *testing.T) { } func TestReadRotatedLog(t *testing.T) { - var mu sync.RWMutex tmpDir := t.TempDir() file, err := os.CreateTemp(tmpDir, "logfile") if err != nil { @@ -236,15 +234,13 @@ func TestReadRotatedLog(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() // Start to follow the container's log. + fileName := file.Name() go func(ctx context.Context) { podLogOptions := v1.PodLogOptions{ Follow: true, } opts := NewLogOptions(&podLogOptions, time.Now()) - mu.Lock() - path := file.Name() - mu.Unlock() - _ = ReadLogs(ctx, path, containerID, opts, fakeRuntimeService, stdoutBuf, stderrBuf) + _ = ReadLogs(ctx, fileName, containerID, opts, fakeRuntimeService, stdoutBuf, stderrBuf) }(ctx) // log in stdout @@ -282,13 +278,10 @@ func TestReadRotatedLog(t *testing.T) { } newF := filepath.Join(dir, baseName) - mu.Lock() if file, err = os.Create(newF); err != nil { - mu.Unlock() assert.NoError(t, err, "unable to create new log file") return } - mu.Unlock() time.Sleep(20 * time.Millisecond) } }