Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025

Conversation

hugodutka
Copy link
Contributor

@hugodutka hugodutka commented Jun 19, 2025

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 in 118bf98

defer func() {
if err == nil {
return
}
if sqlDB != nil {
_ = sqlDB.Close()
sqlDB = nil
}
logger.Error(ctx, "connect to postgres failed", slog.Error(err))
}()

@hugodutka hugodutka changed the title chore: close db properly on false version.Next in ConnectToPostgres chore: close db properly in early exit paths in ConnectToPostgres Jun 19, 2025
@hugodutka hugodutka force-pushed the hugodutka/connect-to-postgres-close-db branch from 471bd6d to e9f833b Compare June 19, 2025 09:46
@hugodutka hugodutka marked this pull request as ready for review June 19, 2025 10:36
@hugodutka hugodutka requested a review from Emyrk June 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())
return nil, 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? Like dbError?

I say that because a few lines up is version, err := sqlDB.QueryContext(ctx, "SHOW server_version_num;"). Does that not shadow the err variable and break this assignment to the correct err here?

Copy link
Contributor Author

@hugodutka hugodutka Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the language spec, this statement assigns to the correct err:

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 a dbNeedsClosing variable and refactored the defer statement to use it instead of err. 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 hugodutka force-pushed the hugodutka/connect-to-postgres-close-db branch from e9f833b to 4aacca3 Compare June 20, 2025 11:05
@hugodutka hugodutka force-pushed the hugodutka/connect-to-postgres-close-db branch from 4aacca3 to 6d07d78 Compare June 20, 2025 11:07
@hugodutka hugodutka merged commit 4ceb549 into main Jun 20, 2025
34 checks passed
@hugodutka hugodutka deleted the hugodutka/connect-to-postgres-close-db branch June 20, 2025 12:11
@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants