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

Commitcf0daf4

Browse files
committed
fix flakes related to context cancellation while establishing postgres connections
1 parent277c2c7 commitcf0daf4

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
caseerrors.Is(err,context.Canceled):
170170
return
171+
caseerr!=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
iferr!=nil {
23602360
returnnil,xerrors.Errorf("get postgres version: %w",err)
23612361
}
2362+
deferversion.Close()
2363+
ifversion.Err()!=nil {
2364+
returnnil,xerrors.Errorf("version select: %w",version.Err())
2365+
}
23622366
if!version.Next() {
23632367
returnnil,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
iferr!=nil {
23682372
returnnil,xerrors.Errorf("scan version: %w",err)
23692373
}
2370-
_=version.Close()
23712374

23722375
ifversionNum<130000 {
23732376
returnnil,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-
caseerr=<-errCh:
557-
iferr!=nil {
558-
_=p.pgListener.Close()
559-
returnxerrors.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+
iferr:=<-errCh;err!=nil {
562561
_=p.pgListener.Close()
563-
returnctx.Err()
562+
returnxerrors.Errorf("create pq listener: %w",err)
564563
}
565564
returnnil
566565
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp