Skip to content

Commit cf0daf4

Browse files
committed
fix flakes related to context cancellation while establishing postgres connections
1 parent 277c2c7 commit cf0daf4

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

cli/clitest/clitest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ 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 a test exits it cancels the context. If a transaction is open
173+
// and a query is being executed, instead of a context.Canceled error
174+
// we get a "driver: bad connection" error.
175+
return
171176
default:
172177
assert.NoError(t, err)
173178
}

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