lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.04k stars 909 forks source link

conn.Ping() throwing nil pointer errors #1059

Open radhianamri opened 3 years ago

radhianamri commented 3 years ago

Hi, I am intermittently getting the below error when calling conn.Ping() which is being called periodically. The rows object returned by .simpleQuery() is getting nil. https://github.com/lib/pq/blob/master/conn_go18.go#L95

2021/09/28 12:20:04 [16139] panic: runtime error: invalid memory address or nil pointer dereference
2021/09/28 12:20:04 [16139] [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x9efe6f]
2021/09/28 12:20:04 [16139]
2021/09/28 12:20:04 [16139] goroutine 44 [running]:
2021/09/28 12:20:04 [16139] github.com/XXXX/XX/vendor/github.com/lib/pq.(*rows).Close(0x0, 0x0, 0x0)
2021/09/28 12:20:04 [16139]         /share/dpkg-build/cm-build/src/github.com/XXXX/XX/vendor/github.com/lib/pq/conn.go:1494 +0x3f
2021/09/28 12:20:04 [16139] github.com/XXXX/XX/vendor/github.com/lib/pq.(*conn).Ping(0xc00284cdc0, 0x187efa0, 0xc002a1a6c0, 0x0, 0x0)
2021/09/28 12:20:04 [16139]         /share/dpkg-build/cm-build/src/github.com/XXXX/XX/vendor/github.com/lib/pq/conn_go18.go:87 +0xf6
2021/09/28 12:20:04 [16139] database/sql.(*DB).pingDC.func1()
2021/09/28 12:20:04 [16139]         /home/buildbot/.gvm/gos/go1.14.4/src/database/sql/sql.go:768 +0x4a
2021/09/28 12:20:04 [16139] database/sql.withLock(0x18660c0, 0xc001f2a080, 0xc003d83c10)
2021/09/28 12:20:04 [16139]         /home/buildbot/.gvm/gos/go1.14.4/src/database/sql/sql.go:3184 +0x69
2021/09/28 12:20:04 [16139] database/sql.(*DB).pingDC(0xc0001aa240, 0x187efa0, 0xc002a1a6c0, 0xc001f2a080, 0xc003d83cc0, 0x0, 0x0)
2021/09/28 12:20:04 [16139]         /home/buildbot/.gvm/gos/go1.14.4/src/database/sql/sql.go:767 +0x11b
2021/09/28 12:20:04 [16139] database/sql.(*DB).PingContext(0xc0001aa240, 0x187efa0, 0xc002a1a6c0, 0x187efa0, 0xc002a1a6c0)
2021/09/28 12:20:04 [16139]         /home/buildbot/.gvm/gos/go1.14.4/src/database/sql/sql.go:794 +0x170

Would it be possible to check if rows is not nil before closing it in this line? https://github.com/lib/pq/blob/master/conn_go18.go#L99

Seems related to this as well https://github.com/lib/pq/issues/1019

Yowgf commented 2 years ago

I had exactly the same problem.

JuchangGit commented 1 year ago

I have exactly the same problem