英文:
What is causing golang program being at 100% CPU?
问题
我有一个我写的golang程序(它是一个FTP服务器),在运行时CPU占用率达到100%。我在strace中看到以下内容:
futex(0xa83918, FUTEX_WAIT, 0, NULL)
read(9, "", 4096) = 0
read(9, "", 4096) = 0
read(9, "", 4096) = 0
read(9, "", 4096) = 0
read(9, "", 4096) = 0
read(8, "", 4096) = 0
read(8, "", 4096) = 0
read(8, "", 4096) = 0
read(8, "", 4096) = 0
read(8, "", 4096) = 0
一遍又一遍。它陷入了某个无限循环中。它的主循环是:
for {
tcpConn, err := listener.Accept()
if err != nil {
Server.logger.Print("listening error")
break
}
driver, err := Server.driverFactory.NewDriver()
if err != nil {
Server.logger.Print("Error creating driver, aborting client connection")
} else {
ftpConn := Server.newConn(tcpConn, driver, Server.Auth)
go ftpConn.Serve()
}
}
有没有什么想法是什么导致了这个无限循环?当程序启动时,它并不处于这种糟糕的状态。它以正常的CPU使用率正常循环。它运行几个小时后才进入这种糟糕的状态。
英文:
I have a golang program I wrote (it's an FTP server) that has 100% CPU when running. I see in strace:
futex(0xa83918, FUTEX_WAIT, 0, NULL
read(9, "", 4096) = 0
read(9, "", 4096) = 0
read(9, "", 4096) = 0
read(9, "", 4096) = 0
read(9, "", 4096) = 0
read(8, "", 4096) = 0
read(8, "", 4096) = 0
read(8, "", 4096) = 0
read(8, "", 4096) = 0
read(8, "", 4096) = 0
Over and over. It's caught in some infinite loop. It's main for loop is:
for {
tcpConn, err := listener.Accept()
if err != nil {
Server.logger.Print("listening error")
break
}
driver, err := Server.driverFactory.NewDriver()
if err != nil {
Server.logger.Print("Error creating driver, aborting client connection")
} else {
ftpConn := Server.newConn(tcpConn, driver, Server.Auth)
go ftpConn.Serve()
}
}
Any idea what is causing the infinite loop? When the program starts it's NOT in this bad state. It loops normally with normal cpu usage. It takes several hours of it running before it gets into this bad state.
答案1
得分: 3
原来这与TCP无关。这是由于代码中的一个while循环永远不会结束,原因是输入中的"\n"问题。例如,我有以下代码:
for {
if something {
break;
}
}
但它从未跳出循环。
英文:
Turns out this wasn't TCP related at all. This was a while loop in the code never ending because of a "\n" input issue. i.e. I had:
for {
if something {
break;
}
}
And it never broke.
答案2
得分: 0
也许在发生错误时,尝试关闭 tcpConn,然后再尝试创建新的驱动程序。此外,尝试检查 Server.newConn(tcpConn, driver, Server.Auth) 在完成后是否确实关闭了连接。
英文:
Perhaps try closing the tcpConn when you try to create a new driver on error. Also, try checking that Server.newConn(tcpConn, driver, Server.Auth) actually closes the connection when it's complete.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论