Skip to content

Handle file close in defer better for dev command#628

Merged
gorkem merged 1 commit intokitops-ml:mainfrom
gorkem:better-defer
Nov 27, 2024
Merged

Handle file close in defer better for dev command#628
gorkem merged 1 commit intokitops-ml:mainfrom
gorkem:better-defer

Conversation

@gorkem
Copy link
Copy Markdown
Member

@gorkem gorkem commented Nov 26, 2024

Improve the handling of file close in defer by checking for errors during the close

fixes #625, #626

Comment thread pkg/lib/harness/llm_payload_all.go Outdated
Comment on lines +122 to +130
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)
}
}
}()
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work; the defer will execute when leaving the current scope, which is the called function (deferCloseFile)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I should move the defer out. Dunno what I was thinking

Improve the handling of file close in defer
by checking for possible errors during the close
@gorkem gorkem merged commit dc6b874 into kitops-ml:main Nov 27, 2024
@gorkem gorkem deleted the better-defer branch November 27, 2024 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeQL Finding: Writable file handle closed without error handling - file: llm-harness.go

2 participants