简体   繁体   中英

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. 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.

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. 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. Also, try checking that Server.newConn(tcpConn, driver, Server.Auth) actually closes the connection when it's complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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