Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle file close in defer better for dev command #628

Merged
merged 1 commit into from
Nov 27, 2024

Conversation

gorkem
Copy link
Contributor

@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 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
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
Contributor 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 jozu-ai:main Nov 27, 2024
3 checks passed
@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