Golang,postgresql rows.next()恐慌

时间:2017-02-05 02:36:50

标签: sql postgresql go

在使用golang中的postgresql使用rows.next()进行行交互时,我遇到错误。这只发生在几台机器上并且在非常不可预测的时间内,这使得调试变得困难。

panic: runtime error: index out of range [recovered]
        panic: runtime error: index out of range

goroutine 28078 [running]:
panic(0xa63ae0, 0xc420014090)
        /usr/local/go/src/runtime/panic.go:500 +0x1a1
github.com/lib/pq.(*conn).errRecover(0xc420780500, 0xc420d2ef28)
        /share/dpkg-build/pacman-build/src/github.com/lib/pq/error.go:482 +0x57e
panic(0xa63ae0, 0xc420014090)
        /usr/local/go/src/runtime/panic.go:458 +0x243
github.com/lib/pq.binaryDecode(0xc420780728, 0xc4207806f0, 0x0, 0x30, 0xc400000017, 0xa24300, 0xc421fcb4b0)
        /share/dpkg-build/pacman-build/src/github.com/lib/pq/encode.go:76 +0x385
github.com/lib/pq.decode(0xc420780728, 0xc4207806f0, 0x0, 0x30, 0x17, 0x1, 0xa24300, 0xc421fcb4b0)
        /share/dpkg-build/pacman-build/src/github.com/lib/pq/encode.go:61 +0x6c
github.com/lib/pq.(*rows).Next(0xc42281e310, 0xc420b82000, 0x2f, 0x2f, 0x0, 0x0)
        /share/dpkg-build/pacman-build/src/github.com/lib/pq/conn.go:1369 +0x420
database/sql.(*Rows).Next(0xc42005b560, 0xc4204eb590)
        /usr/local/go/src/database/sql/sql.go:1758 +0x6c
github.com/xxxxx/yyyyy/src/product/elasticsearch.getPart1(0xc4204eb590, 0xa24300, 0xc420a04190, 0x0, 0x0) <-- rows.next() here
        /share/dpkg-build/pacman-build/src/github.com/xxxxxx/yyyyyy/src/product/elasticsearch/collector.go:71 +0xff
github.com/xxxxx/yyyyy/src/product/elasticsearch.Get(0xa24300, 0xc420a04190, 0xc420a04190, 0xa24300, 0xc420a04190, 0xc420980a00)
        /share/dpkg-build/pacman-build/src/github.com/xxxxxx/yyyyyy/src/product/elasticsearch/collector.go:29 +0x118

来自上面的调用堆栈恐慌来自binaryDecode函数,我想解释为什么它只发生在某些机器上。可能有一些糟糕的网络包或消息被破坏,因此驱动程序无法解码消息然后失败。

但是我不知道在此之后去哪里,有人有想法吗?

由于

1 个答案:

答案 0 :(得分:0)

驱动程序希望将4字节切片编码为整数,但在尝试索引单个字节时会感到恐慌。

数据是否可能意外为零,或者数据类型少于4个字节?

在驱动程序github.com/lib/pq/encode.go中:

case oid.T_int4:
    return int64(int32(binary.BigEndian.Uint32(s)))

在编码/二进制Uint32中(此行出现索引超出界限的恐慌):

return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
相关问题