Skip to content

Commit 623dcd9

Browse files
authored
fix(cli): fix flakes related to context cancellation when establishing pg connections (#18246)
Since #18195 was merged, we started running CLI tests with postgres instead of just dbmem. This surfaced errors related to context cancellation while establishing postgres connections. This PR should fix coder/internal#672. Related to #15109.
1 parent b5fd3dd commit 623dcd9

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

cli/clitest/clitest.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ func StartWithAssert(t *testing.T, inv *serpent.Invocation, assertCallback func(
168168
switch {
169169
case errors.Is(err, context.Canceled):
170170
return
171+
case err != nil && strings.Contains(err.Error(), "driver: bad connection"):
172+
// When we cancel the context on a query that's being executed within
173+
// a transaction, sometimes, instead of a context.Canceled error we get
174+
// a "driver: bad connection" error.
175+
// https://github.com/lib/pq/issues/1137
176+
return
171177
default:
172178
assert.NoError(t, err)
173179
}

cli/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,10 @@ func ConnectToPostgres(ctx context.Context, logger slog.Logger, driver string, d
23592359
if err != nil {
23602360
return nil, xerrors.Errorf("get postgres version: %w", err)
23612361
}
2362+
defer version.Close()
2363+
if version.Err() != nil {
2364+
return nil, xerrors.Errorf("version select: %w", version.Err())
2365+
}
23622366
if !version.Next() {
23632367
return nil, xerrors.Errorf("no rows returned for version select")
23642368
}
@@ -2367,7 +2371,6 @@ func ConnectToPostgres(ctx context.Context, logger slog.Logger, driver string, d
23672371
if err != nil {
23682372
return nil, xerrors.Errorf("scan version: %w", err)
23692373
}
2370-
_ = version.Close()
23712374

23722375
if versionNum < 130000 {
23732376
return nil, xerrors.Errorf("PostgreSQL version must be v13.0.0 or higher! Got: %d", versionNum)

coderd/database/pubsub/pubsub.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,14 @@ func (p *PGPubsub) startListener(ctx context.Context, connectURL string) error {
552552
sentErrCh = true
553553
}),
554554
}
555-
select {
556-
case err = <-errCh:
557-
if err != nil {
558-
_ = p.pgListener.Close()
559-
return xerrors.Errorf("create pq listener: %w", err)
560-
}
561-
case <-ctx.Done():
555+
// We don't respect context cancellation here. There's a bug in the pq library
556+
// where if you close the listener before or while the connection is being
557+
// established, the connection will be established anyway, and will not be
558+
// closed.
559+
// https://github.com/lib/pq/issues/1192
560+
if err := <-errCh; err != nil {
562561
_ = p.pgListener.Close()
563-
return ctx.Err()
562+
return xerrors.Errorf("create pq listener: %w", err)
564563
}
565564
return nil
566565
}

0 commit comments

Comments
 (0)
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