【问题标题】:What is causing golang program being at 100% CPU?是什么导致 golang 程序处于 100% CPU 状态?
【发布时间】:2016-06-26 02:07:32
【问题描述】:

我有一个我编写的 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 使用率正常循环。它需要运行几个小时才能进入这种糟糕的状态。

【问题讨论】:

  • 什么是 fd 9?检查ls -lah /proc/YOURPID/fd/9
  • TCP ftp-001.foo.bar.com:ftp->1a.a1.11a1.ip4.static.sl-reverse.com:30006 (CLOSE_WAIT)
  • 啊,这似乎是我的问题:stackoverflow.com/questions/15912370/…“您的程序仍在运行,并且尚未关闭套接字(内核正在等待它这样做)”
  • 这段代码没有显示问题。看起来你的 ftp 包没有正确关闭连接

标签: go infinite-loop ftp-server


【解决方案1】:

事实证明这根本与 TCP 无关。这是代码中的一个 while 循环,由于“\n”输入问题而永远不会结束。即我有:

for {
  if something {
    break;
  }
}

而且它从来没有坏过。

【讨论】:

    【解决方案2】:

    当您尝试创建新驱动程序出错时,可能尝试关闭 tcpConn。另外,请尝试检查 Server.newConn(tcpConn, driver, Server.Auth) 在连接完成时是否确实关闭了连接。

    【讨论】:

      猜你喜欢
      • 2016-04-15
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多