简体   繁体   English

是什么导致golang程序占用100%的CPU?

[英]What is causing golang program being at 100% CPU?

I have a golang program I wrote (it's an FTP server) that has 100% CPU when running. 我有一个编写的golang程序(它是一个FTP服务器),运行时具有100%的CPU。 I see in strace: 我在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. 它通常以正常的CPU使用率循环播放。 It takes several hours of it running before it gets into this bad state. 进入这种不良状态需要花费几个小时。

Turns out this wasn't TCP related at all. 原来,这与TCP根本无关。 This was a while loop in the code never ending because of a "\\n" input issue. 由于“ \\ n”输入问题,代码中的while循环永无休止。 ie I had: 即我有:

for {
  if something {
    break;
  }
}

And it never broke. 而且它从未破产。

Perhaps try closing the tcpConn when you try to create a new driver on error. 尝试在错误时创建新驱动程序时,请尝试关闭tcpConn。 Also, try checking that Server.newConn(tcpConn, driver, Server.Auth) actually closes the connection when it's complete. 另外,请尝试检查Server.newConn(tcpConn,驱动程序,Server.Auth)在完成连接后实际上是否关闭了连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM