Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
hugodutka merged 1 commit intomainfromhugodutka/connect-to-postgres-close-db
Jun 20, 2025

Conversation

hugodutka
Copy link
Contributor

@hugodutkahugodutka commentedJun 19, 2025
edited
Loading

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

deferfunc() {
iferr==nil {
return
}
ifsqlDB!=nil {
_=sqlDB.Close()
sqlDB=nil
}
logger.Error(ctx,"connect to postgres failed",slog.Error(err))
}()

@hugodutkahugodutka changed the titlechore: close db properly on false version.Next in ConnectToPostgreschore: close db properly in early exit paths in ConnectToPostgresJun 19, 2025
@hugodutkahugodutkaforce-pushed thehugodutka/connect-to-postgres-close-db branch from471bd6d toe9f833bCompareJune 19, 2025 09:46
@hugodutkahugodutka marked this pull request as ready for reviewJune 19, 2025 10:36
@hugodutkahugodutka requested a review fromEmyrkJune 19, 2025 10:37
cli/server.go Outdated
Comment on lines 2366 to 2367
err=xerrors.Errorf("no rows returned for version select: %w",version.Err())
returnnil,err
Copy link
Member

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?

Copy link
ContributorAuthor

@hugodutkahugodutkaJun 20, 2025
edited
Loading

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.

Copy link
Member

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.

https://go.dev/play/p/qtQC8VrFy3H

hugodutka reacted with thumbs up emoji
@hugodutkahugodutkaforce-pushed thehugodutka/connect-to-postgres-close-db branch frome9f833b to4aacca3CompareJune 20, 2025 11:05
@hugodutkahugodutkaforce-pushed thehugodutka/connect-to-postgres-close-db branch from4aacca3 to6d07d78CompareJune 20, 2025 11:07
@hugodutkahugodutka merged commit4ceb549 intomainJun 20, 2025
34 checks passed
@hugodutkahugodutka deleted the hugodutka/connect-to-postgres-close-db branchJune 20, 2025 12:11
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsJun 20, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@EmyrkEmyrkEmyrk approved these changes

Assignees

@hugodutkahugodutka

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@hugodutka@Emyrk

[8]ページ先頭

©2009-2025 Movatter.jp