Skip to content

fix(cli): fix flakes related to context cancellation when establishing pg connections #18246

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 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/clitest/clitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func StartWithAssert(t *testing.T, inv *serpent.Invocation, assertCallback func(
switch {
case errors.Is(err, context.Canceled):
return
case err != nil && strings.Contains(err.Error(), "driver: bad connection"):
// When we cancel the context on a query that's being executed within
// a transaction, sometimes, instead of a context.Canceled error we get
// a "driver: bad connection" error.
// https://github.com/lib/pq/issues/1137
return
default:
assert.NoError(t, err)
}
Expand Down
5 changes: 4 additions & 1 deletion cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,10 @@ func ConnectToPostgres(ctx context.Context, logger slog.Logger, driver string, d
if err != nil {
return nil, xerrors.Errorf("get postgres version: %w", err)
}
defer version.Close()
if version.Err() != nil {
return nil, xerrors.Errorf("version select: %w", version.Err())
}
if !version.Next() {
return nil, xerrors.Errorf("no rows returned for version select")
}
Expand All @@ -2367,7 +2371,6 @@ func ConnectToPostgres(ctx context.Context, logger slog.Logger, driver string, d
if err != nil {
return nil, xerrors.Errorf("scan version: %w", err)
}
_ = version.Close()

if versionNum < 130000 {
return nil, xerrors.Errorf("PostgreSQL version must be v13.0.0 or higher! Got: %d", versionNum)
Expand Down
15 changes: 7 additions & 8 deletions coderd/database/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,15 +552,14 @@ func (p *PGPubsub) startListener(ctx context.Context, connectURL string) error {
sentErrCh = true
}),
}
select {
case err = <-errCh:
if err != nil {
_ = p.pgListener.Close()
return xerrors.Errorf("create pq listener: %w", err)
}
case <-ctx.Done():
// We don't respect context cancellation here. There's a bug in the pq library
// where if you close the listener before or while the connection is being
// established, the connection will be established anyway, and will not be
// closed.
// https://github.com/lib/pq/issues/1192
if err := <-errCh; err != nil {
_ = p.pgListener.Close()
return ctx.Err()
return xerrors.Errorf("create pq listener: %w", err)
}
return nil
}
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy