2
0

Fix ValidateConnect with cancelable context

fixes #40
This commit is contained in:
Jack Christensen
2020-05-25 11:49:37 -05:00
parent 8c33aa2443
commit 2647eff567
2 changed files with 12 additions and 2 deletions
+7
View File
@@ -288,6 +288,13 @@ func connect(ctx context.Context, config *Config, fallbackConfig *FallbackConfig
case *pgproto3.ReadyForQuery:
pgConn.status = connStatusIdle
if config.ValidateConnect != nil {
// ValidateConnect may execute commands that cause the context to be watched again. Unwatch first to avoid
// the watch already in progress panic. This is that last thing done by this method so there is no need to
// restart the watch after ValidateConnect returns.
//
// See https://github.com/jackc/pgconn/issues/40.
pgConn.contextWatcher.Unwatch()
err := config.ValidateConnect(ctx, pgConn)
if err != nil {
pgConn.conn.Close()
+5 -2
View File
@@ -346,9 +346,12 @@ func TestConnectWithValidateConnectTargetSessionAttrsReadWrite(t *testing.T) {
config.ValidateConnect = pgconn.ValidateConnectTargetSessionAttrsReadWrite
config.RuntimeParams["default_transaction_read_only"] = "on"
conn, err := pgconn.ConnectConfig(context.Background(), config)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn, err := pgconn.ConnectConfig(ctx, config)
if !assert.NotNil(t, err) {
conn.Close(context.Background())
conn.Close(ctx)
}
}