Skip to content

Commit

Permalink
Handle file close in defer better
Browse files Browse the repository at this point in the history
Improve the handling of file close in defer
by checking for possible errors during the close
  • Loading branch information
gorkem committed Nov 26, 2024
1 parent 95d44a2 commit 0c8215d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 12 additions & 2 deletions pkg/lib/harness/llm-harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions pkg/lib/harness/llm_payload_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -62,15 +62,15 @@ 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)
}
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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
}()
}

0 comments on commit 0c8215d

Please sign in to comment.