我有一个我编写的 golang 程序(它是一个 FTP 服务器),它在运行时具有 100% 的 CPU。我在 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 循环是:
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 使用率正常循环。它需要运行几个小时才能进入这种不良状态。