Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux环境下loopCloseConn引起的panic #110

Closed
hihaha opened this issue Jul 8, 2020 · 3 comments
Closed

linux环境下loopCloseConn引起的panic #110

hihaha opened this issue Jul 8, 2020 · 3 comments
Assignees
Labels
bug Something isn't working pending development Requested PR owner to improve code and waiting for the result waiting for response waiting for the response from commenter

Comments

@hihaha
Copy link
Contributor

hihaha commented Jul 8, 2020

Linux环境下使用gnet遇到如下panic,
OnClosed, ts:2020-07-08 15:14:45.124678462 +0800 CST m=+19.847459851, cnn:0xc0002f2000
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x68fad0]

goroutine 530 [running]:
github.com/panjf2000/gnet/ringbuffer.(*RingBuffer).IsEmpty(...)
github.com/panjf2000/gnet/ringbuffer/ring_buffer.go:372
github.com/panjf2000/gnet.(*eventloop).loopCloseConn(0xc0002a6000, 0xc0002f2000, 0x0, 0x0, 0x0, 0xf)
github.com/panjf2000/gnet/eventloop_unix.go:193 +0x30
github.com/panjf2000/gnet.(*conn).Close.func1(0x0, 0x0)
github.com/panjf2000/gnet/connection_unix.go:234 +0x3b
github.com/panjf2000/gnet/internal.(*AsyncJobQueue).ForEach(0xc000290028, 0xc000292000, 0x8)
github.com/panjf2000/gnet/internal/queue.go:56 +0xae
github.com/panjf2000/gnet/internal/netpoll.(*Poller).Polling(0xc000290000, 0xc00017df78, 0x0, 0x0)
github.com/panjf2000/gnet/internal/netpoll/epoll.go:112 +0x29e
github.com/panjf2000/gnet.(*server).activateSubReactor(0xc000164090, 0xc0002a6000)
github.com/panjf2000/gnet/reactor_linux.go:50 +0xb1
github.com/panjf2000/gnet.(*server).startSubReactors.func1.1(0xc000164090, 0xc0002a6000)
github.com/panjf2000/gnet/server_unix.go:91 +0x35
created by github.com/panjf2000/gnet.(*server).startSubReactors.func1
github.com/panjf2000/gnet/server_unix.go:90 +0x6a

产生这个panic的原因:异步多goroutine对conn操作,conn先Close, 后续又另一个AsycWrite,再进入函数
func (el *eventloop) loopCloseConn(c *conn, err error) 时,conn已经被release了,
if !c.outboundBuffer.IsEmpty() && err == nil {
_ = el.loopWrite(c)
}
上面if的判断中c.outboundBuffer 已经是nil,所以发生了panic。

修正建议, 将判断outboundBuffer是否为空的逻辑下移到判断conn是否opened的后面。

--- a/src/github.com/panjf2000/gnet/eventloop_unix.go
+++ b/src/github.com/panjf2000/gnet/eventloop_unix.go
@@ -190,15 +190,15 @@ func (el *eventloop) loopWrite(c *conn) error {
 }

 func (el *eventloop) loopCloseConn(c *conn, err error) error {
-       if !c.outboundBuffer.IsEmpty() && err == nil {
-               _ = el.loopWrite(c)
-       }
-
        if !c.opened {
                el.svr.logger.Debugf("The fd=%d in event-loop(%d) is already closed, skipping it", c.fd, el.idx)
                return nil
        }

+       if !c.outboundBuffer.IsEmpty() && err == nil {
+               _ = el.loopWrite(c)
+       }
+
        err0, err1 := el.poller.Delete(c.fd), unix.Close(c.fd)
        if err0 == nil && err1 == nil {
                delete(el.connections, c.fd)
@hihaha hihaha added the bug Something isn't working label Jul 8, 2020
@panjf2000
Copy link
Owner

Nice catch! @hihaha

有兴趣提个 PR 修复吗?

@panjf2000 panjf2000 added pending development Requested PR owner to improve code and waiting for the result waiting for response waiting for the response from commenter labels Jul 8, 2020
@hihaha
Copy link
Contributor Author

hihaha commented Jul 8, 2020

Nice catch! @hihaha

有兴趣提个 PR 修复吗?

可以的

@panjf2000
Copy link
Owner

@hihaha
今天能够提交 PR 吗?这只是个小改动,我想尽快修复,如果你没时间的话我就自己修复了。

0-haha pushed a commit to 0-haha/gnet that referenced this issue Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending development Requested PR owner to improve code and waiting for the result waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

2 participants