Skip to content

Commit

Permalink
Fix panic on some errors in OnConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Aug 11, 2020
1 parent bfed7c8 commit 0ff6219
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (db *baseDB) initConn(ctx context.Context, cn *pool.Conn) error {
}

if db.opt.OnConnect != nil {
p := pool.NewSingleConnPool(nil)
p := pool.NewSingleConnPool(db.pool)
p.SetConn(cn)
return db.opt.OnConnect(ctx, newConn(ctx, db.withPool(p)))
}
Expand Down
22 changes: 22 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@ func TestBigColumn(t *testing.T) {
}
}

var _ = Describe("OnConnect error", func() {
It("does not panic", func() {
opt := pgOptions()
opt.OnConnect = func(ctx context.Context, conn *pg.Conn) error {
_, err := conn.Exec("SELECT pg_sleep(10)")
return err
}

db := pg.Connect(opt)
defer db.Close()

ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(500 * time.Millisecond)
cancel()
}()

_, err := db.ExecContext(ctx, "SELECT 1")
Expect(err).To(MatchError("ERROR #57014 canceling statement due to user request"))
})
})

var _ = Describe("DB", func() {
var db *pg.DB
var tx *pg.Tx
Expand Down

0 comments on commit 0ff6219

Please sign in to comment.