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

Too many connections opened on OS #47

Closed
lonerwolf opened this issue Dec 21, 2019 · 15 comments
Closed

Too many connections opened on OS #47

lonerwolf opened this issue Dec 21, 2019 · 15 comments
Assignees
Labels
duplicate This issue or pull request already exists waiting for response waiting for the response from commenter

Comments

@lonerwolf
Copy link

2019/12/21 14:21:41 close tcp:[::]:9761->: file already closed
2019/12/21 14:21:41 close tcp [::]:9761: use of closed network connection
2019/12/21 14:21:41

UBUNTU上开启服务,然后WIN上,不断建立连接发送关闭,服务端出现上面问题,不知道如何排查,在EPOLL里面打了很多日志,但是都没显示,是不是ACCEPT这里?

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

System Info (please complete the following information):

  • OS (e.g. Ubuntu 18.04):
  • Go version (e.g. Go 1.13):
  • gnet version (e.g. v1.0.0):

Additional context
Add any other context about the problem here.

@lonerwolf lonerwolf added the bug Something isn't working label Dec 21, 2019
@lonerwolf
Copy link
Author

找到出异常位置
if wakenUp {
wakenUp = false
if err = p.asyncJobQueue.ForEach(); err != nil {
log.Println("Polling wakenUp" + err.Error())
return
}
}

2019/12/21 14:21:40 Polling wakenUpserver is going to be shutdown

@panjf2000
Copy link
Owner

其实这个不是 bug,而是正常的关闭 gnet 的时候close 了两次 fd,所以第二次的时候会提示已经 close 过了,因为不是什么大问题,以前一直没处理这个重复关闭的问题,等会儿我把重复关闭的地方修复下吧

@lonerwolf
Copy link
Author

其实这个不是 bug,而是正常的关闭 gnet 的时候close 了两次 fd,所以第二次的时候会提示已经 close 过了,因为不是什么大问题,以前一直没处理这个重复关闭的问题,等会儿我把重复关闭的地方修复下吧

问题是我没让他关闭,他自己关闭了,不知道是因为什么原因关闭

@panjf2000 panjf2000 added the waiting for response waiting for the response from commenter label Dec 21, 2019
@panjf2000
Copy link
Owner

什么意思?server 自己 shutdown 了?有没有 panic 什么的?

@lonerwolf
Copy link
Author

其实这个不是 bug,而是正常的关闭 gnet 的时候close 了两次 fd,所以第二次的时候会提示已经 close 过了,因为不是什么大问题,以前一直没处理这个重复关闭的问题,等会儿我把重复关闭的地方修复下吧

找到了,又是文件描述符之前的问题。。。。。

@panjf2000
Copy link
Owner

Accept 的时候返回 Bad file descriptor

@lonerwolf
Copy link
Author

Accept 的时候返回 Bad file descriptor

是的,然后ACCEPT退出,然后SVR.STOP关闭所有连接退出。。。因为关闭连接打了日志,关闭的时候太多日志看不见上面的日志

@lonerwolf
Copy link
Author

Accept 的时候返回 Bad file descriptor

不知道有没办法突破WIN限制,每次10000,没几次,WIN客户端就connectex: Only one usage of each socket address (protocol/network address/port) is normally permitted.放DOCKER里测试电脑不够用

@panjf2000 panjf2000 changed the title 新的异常 Too many connections opened on OS Dec 21, 2019
@panjf2000 panjf2000 added duplicate This issue or pull request already exists and removed bug Something isn't working labels Dec 21, 2019
@lonerwolf
Copy link
Author

lonerwolf commented Feb 17, 2020

package main

import (
        "fmt"
        "net"
        "time"

        "golang.org/x/sys/unix"
)

func q(){
        serverAddr := "192.168.2.201:9999"
        tcpAddr, err := net.ResolveTCPAddr("tcp", serverAddr)
        if err != nil {
                fmt.Println("Resolve TCPAddr error", err)
        }
        conn, err := net.DialTCP("tcp4", nil, tcpAddr)
        defer conn.Close()
        time.Sleep(10*time.Millisecond)
}

func main() {
fmt.Println("aa")
        lis,err :=net.Listen("tcp", "0.0.0.0:9999")
        if err!=nil{
                fmt.Println(err)
                return
        }
        _ = lis
        f,err:= lis.(*net.TCPListener).File()
        if err!=nil{
                fmt.Println(err)
                return
        }
        fd := int(f.Fd())
        //unix.SetNonblock(fd, true)

        for{
                nfd, sa, err := unix.Accept(fd)
                if err != nil {
                        if err == unix.EAGAIN {
                                continue
                        }
                        fmt.Printf("asdasd%d %d\n",nfd,fd)
                        fmt.Println(err)
                        return
                }
                fmt.Printf("new %d\n",nfd)
                _ = sa
                unix.Close(nfd)
        }
}

最近又试了一下,用上面这个服务器,客户端不断连接就bad file descriptor. ubuntu 18.04 go 1.13.6 1.13.8

@panjf2000
Copy link
Owner

你不用unix.Accept(fd),直接用lis.Accept()呢?也一样吗?

@lonerwolf
Copy link
Author

lonerwolf commented Feb 17, 2020

你不用unix.Accept(fd),直接用lis.Accept()呢?也一样吗?

我觉得我应该找到问题了

acceptFd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)

	if err != nil {
		fmt.Println("Resolve TCPAddr error", err)
		return
	}

	sockaddrInet4 := &unix.SockaddrInet4{
		Port: 9999,
		Addr: [4]byte{0, 0, 0, 0},
	}

	err = unix.Bind(acceptFd, sockaddrInet4)

	if err != nil {
		fmt.Println("1111111111", err)
		return
	}

	err = unix.Listen(acceptFd, 100)
	if err != nil {
		fmt.Println("22222", err)
		return
	}

	for {
		clientFd, sa, err := unix.Accept(acceptFd)
		if err != nil {
			fmt.Println("333333333", err)
			return
		}
		_ = sa
		fmt.Printf("clientFd:%d\n", clientFd)
		unix.Close(clientFd)
	}

用原生LINUX调用就没有问题

go的lis Accept,会设置连接各种属性

@panjf2000
Copy link
Owner

你这个是哪部分的源码,我怎么找不到?

@lonerwolf
Copy link
Author

lonerwolf commented Feb 17, 2020

你这个是哪部分的源码,我怎么找不到?

gnet.go 227行 ln.ln, err = net.Listen(ln.network, ln.addr)
之后 listener.go 46行 ln.f, err = netln.File(),把这几个提取出来,然后不设置unix.SetNonblock,直接用acceptor_linux.go 12行 unix.Accept(fd)
这几个提取出来运行

大批量连接就bad描述符。我改成原生API,就没问题。觉得是listener.accept做了其他处理,或者其他原因

@panjf2000
Copy link
Owner

我不是说 gnet 的源码,我说你贴的 go 的源码是哪里的,我没找到。

@tangtaoit
Copy link

遇到同样的问题 换成@lonerwolf 的就好了 我设置了SetNonblock也没发现问题

panjf2000 added a commit that referenced this issue Jul 3, 2020
Also enable SO_REUSEPORT to avoid Thundering Herd, besides, Fixes #47 #44 #77
panjf2000 added a commit that referenced this issue Jul 3, 2020
Also enable SO_REUSEPORT to avoid Thundering Herd, besides, Fixes #47 #44 #77
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
duplicate This issue or pull request already exists waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

3 participants