-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
always close db connection on error in Open #1302
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #1302 +/- ##
==========================================
+ Coverage 47.16% 47.60% +0.44%
==========================================
Files 12 12
Lines 1533 1523 -10
==========================================
+ Hits 723 725 +2
+ Misses 669 657 -12
Partials 141 141 ☔ View full report in Codecov by Sentry. |
Please fix conflict |
0ae71f8
to
72d0e93
Compare
Conflict fixed and thank you for the quick review! |
// | ||
// Previously, this was handled at each return statement, which was error-prone | ||
// and led to bugs because the database connection was not always closed. | ||
err := func() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this, we can use a named return value and a defer. That will also enable closing it on panic, which is another hole we have today.
defer func() {
if retErr != nil {
conn.Close()
}
}()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would handle closing the conn on error, but if there is a panic the named return error value will almost certainly be nil since the panic will occur before the return statement is executed. We could use a deferred call to recover
if we really cared about closing the conn and releasing its resources:
defer func() {
if e := recover(); e != nil {
conn.Close()
panic(e)
}
}()
IMHO, any panic here should probably be considered terminal so I don't think we need to care too much about leaking resources, but also happy to make the change since this is an externally managed resource.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was a little unclear. I just meant that using a defer
would enable accounting for panics, not the named return itself.
The reason to close in the panic case is that this library cannot control what the caller of Open
does. In particular, they could choose to swallow panics, and it would be good to at least avoid causing a memory leak in that situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was a little unclear. I just meant that using a defer would enable accounting for panics, not the named return itself.
Thanks and yeah was a little confused since in the given code snippet the defer would not close the db conn in the event of a panic (since the named return value would likely be nil).
I updated the code to close the db connection if a panic occurs during setup.
72d0e93
to
04d7782
Compare
This commit fixes a bug where the db connection would not always be closed when an error occurred in Open. Additionally, it makes sure that we always unregister any callbacks associated with the connection, which previously did not always happen. This change consolidates the error handling logic (previously, it had to be done for each return statement) which should make it more difficult to introduce this type of bug in the future.
04d7782
to
7fbde5e
Compare
This commit fixes a bug where the db connection would not always be closed when an error occurred in Open. Additionally, it makes sure that we always unregister any callbacks associated with the connection, which previously did not always happen. This change consolidates the error handling logic (previously, it had to be done for each return statement) which should make it more difficult to introduce this type of bug in the future.