From 6ab50f162c896e8480f32da4f2ce4297255e928b Mon Sep 17 00:00:00 2001 From: Congqi Xia Date: Tue, 5 Nov 2024 15:41:33 +0800 Subject: [PATCH 1/2] fix: Skip EOF error when default empty yaml file Related to #37404 Signed-off-by: Congqi Xia --- pkg/config/file_source.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/config/file_source.go b/pkg/config/file_source.go index 4eace878d890f..535e87d5c2963 100644 --- a/pkg/config/file_source.go +++ b/pkg/config/file_source.go @@ -19,6 +19,7 @@ package config import ( "bytes" "fmt" + "io" "os" "path/filepath" "sync" @@ -146,7 +147,7 @@ func (fs *FileSource) loadFromFile() error { var node yaml.Node decoder := yaml.NewDecoder(bytes.NewReader(data)) - if err := decoder.Decode(&node); err != nil { + if err := decoder.Decode(&node); err != nil && !errors.Is(err, io.EOF) { return errors.Wrap(err, "YAML unmarshal failed: "+configFile) } From 5eb6eef2f5190767e3bb66f48a1caff4fb45943a Mon Sep 17 00:00:00 2001 From: Congqi Xia Date: Tue, 5 Nov 2024 15:43:12 +0800 Subject: [PATCH 2/2] Add unit test for issue case Signed-off-by: Congqi Xia --- pkg/config/file_source_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 pkg/config/file_source_test.go diff --git a/pkg/config/file_source_test.go b/pkg/config/file_source_test.go new file mode 100644 index 0000000000000..60abdb110ed7d --- /dev/null +++ b/pkg/config/file_source_test.go @@ -0,0 +1,32 @@ +package config + +import ( + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestEmptyYaml(t *testing.T) { + file, err := os.CreateTemp(os.TempDir(), "milvus_ut_config_fs_*.yaml") + require.NoError(t, err) + + filepath := file.Name() + + file.WriteString("#") + file.Close() + + defer os.Remove(filepath) + + fs := NewFileSource(&FileInfo{ + Files: []string{filepath}, + RefreshInterval: time.Hour, + }) + + _, err = fs.GetConfigurations() + assert.NoError(t, err) + + fs.Close() +}