- Notifications
You must be signed in to change notification settings - Fork920
chore: close db properly in early exit paths in ConnectToPostgres#18448
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation
471bd6d
toe9f833b
Comparecli/server.go Outdated
err=xerrors.Errorf("no rows returned for version select: %w",version.Err()) | ||
returnnil,err |
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.
Can we just name this error something else? LikedbError
?
I say that because a few lines up isversion, err := sqlDB.QueryContext(ctx, "SHOW server_version_num;")
. Does that not shadow theerr
variable and break this assignment to the correcterr
here?
hugodutkaJun 20, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
Based onthe language spec, this statement assigns to the correcterr
:
Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) [...]Redeclaration does not introduce a new variable; it just assigns a new value to the original.
Either way, it's non-obvious and if somebody modified this function in the future, it's likely they would forget about the err assignment/close db logic. I added adbNeedsClosing
variable and refactored thedefer
statement to use it instead oferr
. This way, the db will always be closed unless a code path explicitly opts out of it.
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.
TIL! Sorry I was misinformed, I had no idea that was the effect.
e9f833b
to4aacca3
Compare4aacca3
to6d07d78
Compare4ceb549
intomainUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There were some code paths where if we exited early from the function the postgres connection would never get cleaned up.
This is the mechanism that cleans up the db - it requires the err variable to be not nil:
coder/cli/server.go
Lines 2319 to 2328 in118bf98