From 0c8215d4d43fbf65baef6e0d9642850aa1ae7d5c Mon Sep 17 00:00:00 2001 From: Gorkem Ercan Date: Tue, 26 Nov 2024 18:12:30 -0500 Subject: [PATCH] Handle file close in defer better Improve the handling of file close in defer by checking for possible errors during the close --- pkg/lib/harness/llm-harness.go | 14 ++++++++++++-- pkg/lib/harness/llm_payload_all.go | 20 ++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/pkg/lib/harness/llm-harness.go b/pkg/lib/harness/llm-harness.go index 0b786ca5..49d5cb60 100644 --- a/pkg/lib/harness/llm-harness.go +++ b/pkg/lib/harness/llm-harness.go @@ -61,7 +61,7 @@ func (harness *LLMHarness) Init() error { return nil } -func (harness *LLMHarness) Start(modelPath string) error { +func (harness *LLMHarness) Start(modelPath string) (err error) { harnessPath := constants.HarnessPath(harness.ConfigHome) pidFile := filepath.Join(harnessPath, constants.HarnessProcessFile) @@ -108,7 +108,17 @@ func (harness *LLMHarness) Start(modelPath string) error { if err != nil { return fmt.Errorf("failed to open log file for harness: %w", err) } - defer logs.Close() + + defer func() { + if errClose := logs.Close(); errClose != nil { + if err == nil { + err = fmt.Errorf("failed to close log file: %w", errClose) + } else { + err = fmt.Errorf("%v; failed to close log file: %w", err, errClose) + } + } + }() + output.Debugf("Saving server logs to %s", logFile) cmd.Stdout = logs cmd.Stderr = logs diff --git a/pkg/lib/harness/llm_payload_all.go b/pkg/lib/harness/llm_payload_all.go index 1fdde3eb..871163b7 100644 --- a/pkg/lib/harness/llm_payload_all.go +++ b/pkg/lib/harness/llm_payload_all.go @@ -29,7 +29,7 @@ import ( "kitops/pkg/lib/filesystem" ) -func extractFile(fs fs.FS, file, harnessHome string) error { +func extractFile(fs fs.FS, file, harnessHome string) (err error) { srcFile, err := fs.Open(file) if err != nil { return fmt.Errorf("failed to open file %s: %w", file, err) @@ -62,7 +62,7 @@ func extractFile(fs fs.FS, file, harnessHome string) error { if err != nil { return fmt.Errorf("failed to create destination file %s: %w", destFile, err) } - defer dest.Close() + deferCloseFile(dest, &err, "failed to close destination file") if _, err := io.Copy(dest, srcReader); err != nil { return fmt.Errorf("failed to copy payload to %s: %w", destFile, err) @@ -70,7 +70,7 @@ func extractFile(fs fs.FS, file, harnessHome string) error { return nil } -func extractTar(tr *tar.Reader, dir string) error { +func extractTar(tr *tar.Reader, dir string) (err error) { for { header, err := tr.Next() if err == io.EOF { @@ -102,7 +102,7 @@ func extractTar(tr *tar.Reader, dir string) error { if err != nil { return fmt.Errorf("failed to create file %s: %w", outPath, err) } - defer file.Close() + deferCloseFile(file, &err, "failed to close destination file") written, err := io.Copy(file, tr) if err != nil { @@ -118,3 +118,15 @@ func extractTar(tr *tar.Reader, dir string) error { } return nil } + +func deferCloseFile(f *os.File, err *error, msg string) { + defer func() { + if cerr := f.Close(); cerr != nil { + if *err == nil { + *err = fmt.Errorf("%s: %w", msg, cerr) + } else { + *err = fmt.Errorf("%v; %s: %w", *err, msg, cerr) + } + } + }() +} \ No newline at end of file