-
Notifications
You must be signed in to change notification settings - Fork 12
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
Documentation Request for deferring Close() #8
Comments
Using a defer is preferred because if someone comes in later and puts an early return and doesn't make sure to also call Close then connection exhaustion is possible. This is a severe enough problem that is easy to miss that enforcing a defer is IMO a good course of action. I agree that this linter should make sure that the defer is called immediately or very closely after a function returns the results/tx. I also agree that requiring a defer is stylistic. I will consider removing the defer requirement and instead check all branches call close. |
If that is the case, then I propose that the linter should also require that the "defer rows,Close()" is put immediately on the line after the "rows := ..." line. |
I 100% agree and I've added these improvements to my list. The next step for me is to research how to enforce these rules as I'm fairly new to the |
Not to crash your thread, but can I ask for a quick clarification? Are we saying that we want to:
And/Or:
Would something along the lines of:
be suitable? Given we are defering the call to close before the chance for any early returns is introduced. Edit: Thanks a lot for creating this, we identified a resource leak in our systems the other day and this linter helped us a lot in tracking down all the places. Much appreciate. |
It would not be immediately after My fear with the last example is that if the error check returns and doesn't do something to make Glad I could help. :) |
I have a kind of a false positive there:
If using defer, it won't work correctly as it's executed at the end of the function, as Goland suggests, and if not using it, the linter would complain. |
Defer in a loop can be tricky. A solution: for _, query := range queries {
func() {
statement, err := db.Prepare(query)
if err != nil {
panic(err)
}
defer statement.Close()
if _, err := statement.Exec(); err != nil {
panic(err)
}
}()
} |
Hello, and thanks for the linter.
sqlclosecheck complains that
rows.Close()
should use defer. Can you explain why this is? For example, I don't see a difference between:and
Putting defer here just seems confusing / a mistake / superfluous.
The text was updated successfully, but these errors were encountered: