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

Commit623dcd9

Browse files
authored
fix(cli): fix flakes related to context cancellation when establishing pg connections (#18246)
Since#18195 was merged, we startedrunning CLI tests with postgres instead of just dbmem. This surfacederrors related to context cancellation while establishing postgresconnections.This PR shouldfixcoder/internal#672. Relatedto#15109.
1 parentb5fd3dd commit623dcd9

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
caseerrors.Is(err,context.Canceled):
170170
return
171+
caseerr!=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
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