Jetty - Too Many Open Files Issue on Linux

最近開始使用security audit tool測試我們軟體(搭Jetty9.0.7):

nikto -h 192.168.0.1 -port 80 -root /MyApp -mutate 1

大漏洞沒發現,卻發現在Linux上會經不起大量請求而產生Too many open files Exception:

java.io.IOException: Too many open files
        at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
        at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)
        at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)
        at org.eclipse.jetty.server.ServerConnector.accept(ServerConnector.java:321)
        at org.eclipse.jetty.server.AbstractConnector$Acceptor.run(AbstractConnector.java:460)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
        at java.lang.Thread.run(Thread.java:745)

使用lsof -p $pid會發現很可能是socket沒關閉造成的問題:

java    9403 root  250u  unix 0xffff8802653b1180      0t0 30861424 socket
java    9403 root  251u  unix 0xffff8802653b1180      0t0 30861424 socket
java    9403 root  253u  unix 0xffff8802653b1180      0t0 30861424 socket
java    9403 root  256u  unix 0xffff8802653b1180      0t0 30861424 socket
java    9403 root  258u  unix 0xffff8802653b1180      0t0 30861424 socket
java    9403 root  260u  unix 0xffff8802653b1180      0t0 30861424 socket
java    9403 root  264u  unix 0xffff8802653b1180      0t0 30861424 socket
java    9403 root  269u  unix 0xffff8802653b1180      0t0 30861424 socket
java    9403 root  270u  unix 0xffff8802653b1180      0t0 30861424 socket

(在ulimit沒被設定太小的前提下)
後來爬一下文,原因是Jetty例外處理沒做好而造成的,更新到9.3.9-v20160517測試socket占用問題即消失。