什么原因导致 Golang 程序占用 100% 的 CPU?

huangapple go评论80阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年3月11日 08:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/35929891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定