livego
livego copied to clipboard
panic: invalid memory addresss
这次是真的了。。。推了2个流到livego ,前端是http + flv.js 1.不知道为啥出来的比较慢。 2.打开2个的时候第二个死活出不来,再打开就报了一个panic:runtime error:invalid memory address or nil pointer dereference 然后程序崩溃
请提供错误详细堆栈
2017/06/16 10:20:37 writer.go:159: http flv closed panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x501558]
goroutine 4988871 [running]: bufio.(*Writer).Flush(0xc420012b00, 0xc422c98d44, 0x6e9) /usr/local/Cellar/go/1.8.3/libexec/src/bufio/bufio.go:566 +0x58 bufio.(*Writer).Write(0xc420012b00, 0xc422c98d44, 0x2620, 0x622bc, 0xb, 0x0, 0x0) /usr/local/Cellar/go/1.8.3/libexec/src/bufio/bufio.go:602 +0xdf net/http.(*response).write(0xc420180540, 0x2620, 0xc422c98d44, 0x2620, 0x622bc, 0x0, 0x0, 0xb, 0x0, 0x0) /usr/local/Cellar/go/1.8.3/libexec/src/net/http/server.go:1525 +0x150 net/http.(*response).Write(0xc420180540, 0xc422c98d44, 0x2620, 0x622bc, 0xb, 0x0, 0x0) /usr/local/Cellar/go/1.8.3/libexec/src/net/http/server.go:1495 +0x64 github.com/gwuhaolin/livego/protocol/httpflv.(*FLVWriter).SendPacket(0xc4200f6240, 0x0, 0x0) /Users/halwu/go/src/github.com/gwuhaolin/livego/protocol/httpflv/writer.go:134 +0x20c github.com/gwuhaolin/livego/protocol/httpflv.NewFLVWriter.func1(0xc4200f6240) /Users/halwu/go/src/github.com/gwuhaolin/livego/protocol/httpflv/writer.go:47 +0x2f created by github.com/gwuhaolin/livego/protocol/httpflv.NewFLVWriter /Users/halwu/go/src/github.com/gwuhaolin/livego/protocol/httpflv/writer.go:52 +0x387
我找到重现的方法了。一直调用,用火狐测试大概是27个小时多一点的时候,就会出现这个错误。
看起来有点像是 flvWriter.SendPacket
和 flvWriter.Close
的竞争条件,这里给一个推测,不一定正确:
- Go 的
http
库会在 Handler 返回的时候,调用response.finishRequest
方法,这个方法会调用response
内部的bufio.Writer
的Reset(nil)
。 - 一般而言,
httpflv/server.handleConn
会等待flvWriter
结束。 -
flvWriter.Close
会关闭packetQueue
和closedChan
,后者让handleConn
结束,前者让SendPacket
结束。
但是,SendPacket
在发送之前并不会检查自身是否已经结束:
https://github.com/gwuhaolin/livego/blob/bf65635cef7355fd828b352e61beeedc6467991d/protocol/httpflv/writer.go#L140-L146
所以,假如说有以下的执行序列:
- 某处调用
flvWriter.Close
。 -
handleConn
发现closedChan
关闭,返回;http 库执行finishRequest
。 - 此时,
SendPacket
进入了这个分支: https://github.com/gwuhaolin/livego/blob/bf65635cef7355fd828b352e61beeedc6467991d/protocol/httpflv/writer.go#L110-L130 - 在执行到 https://github.com/gwuhaolin/livego/blob/bf65635cef7355fd828b352e61beeedc6467991d/protocol/httpflv/writer.go#L140 或者 https://github.com/gwuhaolin/livego/blob/bf65635cef7355fd828b352e61beeedc6467991d/protocol/httpflv/writer.go#L144 的时候,就可能会遇到往空 io.Writer 写数据的 panic。
@gwuhaolin 这个问题解决了吗?
panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x18 pc=0x63f603]
goroutine 77131459 [running]: bufio.(*Writer).Flush(0xc004717800, 0xc00579f94e, 0x61) c:/go/src/bufio/bufio.go:591 +0x63 bufio.(*Writer).Write(0xc004717800, 0xc00579f94e, 0x1dd, 0x236b2, 0xb, 0x0, 0x0) c:/go/src/bufio/bufio.go:627 +0x101 net/http.(*response).write(0xc0002b6c40, 0x1dd, 0xc00579f94e, 0x1dd, 0x236b2, 0x0, 0x0, 0xb, 0x0, 0x0) c:/go/src/net/http/server.go:1578 +0x2dc net/http.(*response).Write(0xc0002b6c40, 0xc00579f94e, 0x1dd, 0x236b2, 0xb, 0x0, 0x0) c:/go/src/net/http/server.go:1547 +0x5d github.com/gwuhaolin/livego/protocol/httpflv.(*FLVWriter).SendPacket(0xc001420000, 0xa8bb28, 0xc00324f1e0) C:/Users/renzo/code/nslive/protocol/httpflv/writer.go:144 +0x198 github.com/gwuhaolin/livego/protocol/httpflv.NewFLVWriter.func1(0xc001420000) C:/Users/renzo/code/nslive/protocol/httpflv/writer.go:49 +0x36 created by github.com/gwuhaolin/livego/protocol/httpflv.NewFLVWriter C:/Users/renzo/code/nslive/protocol/httpflv/writer.go:48 +0x362
使用6路推流原画时,当推流速度很小的情况下(测试时约为0.6x),会出现以上错误
时不时会出现这个问题,但是没找到出现的规律。这个问题有解决方案了吗? ` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x50ada2]
goroutine 153 [running]: bufio.(*Writer).Flush(0xc0000d5bc0, 0xc0011dfbef, 0x7f5) G:/Program Files/Go/src/bufio/bufio.go:607 +0x62 bufio.(*Writer).Write(0xc0000d5bc0, 0xc0011dfbef, 0x2981, 0x5b411, 0xb, 0x0, 0x0) G:/Program Files/Go/src/bufio/bufio.go:643 +0xfc net/http.(*response).write(0xc00021c2a0, 0x2981, 0xc0011dfbef, 0x2981, 0x5b411, 0x0, 0x0, 0xb, 0x0, 0x0) G:/Program Files/Go/src/net/http/server.go:1615 +0x38a net/http.(*response).Write(0xc00021c2a0, 0xc0011dfbef, 0x2981, 0x5b411, 0xb, 0x0, 0x0) G:/Program Files/Go/src/net/http/server.go:1573 +0x56 github.com/gwuhaolin/livego/protocol/httpflv.(*FLVWriter).SendPacket(0xc0000c4000, 0x7fc2f1b3f028, 0xc0000765a0) E:/go/src/livego/protocol/httpflv/writer.go:151 +0x198 github.com/gwuhaolin/livego/protocol/httpflv.NewFLVWriter.func1(0xc0000c4000) E:/go/src/livego/protocol/httpflv/writer.go:55 +0x2f created by github.com/gwuhaolin/livego/protocol/httpflv.NewFLVWriter E:/go/src/livego/protocol/httpflv/writer.go:54 +0x35f `