Java 应用程序中奇怪的网络套接字泄漏
我试图在我们的 Java 守护程序中捕获网络套接字泄漏,该守护程序在 lsof 中具有相当奇怪的输出:
java 11734 root 463u IPv6 225927527 TCP 192.168.254.1:http->192.168.254.1:46149 (CLOSE_WAIT)
java 11734 root 464u IPv6 225927347 TCP 192.168.254.1:http->192.168.254.1:46102 (CLOSE_WAIT)
java 11734 root 465u IPv6 225928791 TCP 192.168.254.1:http->192.168.254.1:46451 (CLOSE_WAIT)
java 11734 root 466u IPv6 225927617 TCP 192.168.254.1:http->192.168.254.1:46170 (CLOSE_WAIT)
java 11734 root 467u IPv6 225930330 TCP 192.168.254.1:http->192.168.254.1:57333 (CLOSE_WAIT)
等等,直到它吃掉所有可用的描述符并导致“文件过多”错误。
知道什么会导致这种情况吗?
提前致谢!
I'm trying to catch a network socket leak in our Java daemon, which has quite a strange output in lsof:
java 11734 root 463u IPv6 225927527 TCP 192.168.254.1:http->192.168.254.1:46149 (CLOSE_WAIT)
java 11734 root 464u IPv6 225927347 TCP 192.168.254.1:http->192.168.254.1:46102 (CLOSE_WAIT)
java 11734 root 465u IPv6 225928791 TCP 192.168.254.1:http->192.168.254.1:46451 (CLOSE_WAIT)
java 11734 root 466u IPv6 225927617 TCP 192.168.254.1:http->192.168.254.1:46170 (CLOSE_WAIT)
java 11734 root 467u IPv6 225930330 TCP 192.168.254.1:http->192.168.254.1:57333 (CLOSE_WAIT)
And so on, until it eats all the available descriptors and leads to "Too many files" error.
Any idea what can cause this?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CLOSE_WAIT
表示连接已被远程对等方关闭,但套接字正在等待本地应用程序将其关闭。所以你没有这样做。您需要检查您是否正确处理 EOS,即在所有可能出现的情况下关闭套接字,并且在任何套接字操作上的
IOExceptions
也是如此 - 您必须响应所有 通过关闭套接字,除了SocketTimeoutException
之外。您还必须确保套接字在finally
块中关闭。CLOSE_WAIT
means that the connection has been closed by the remote peer but the socket is waiting for the local application to close it. So you're not doing that.You need to check that you handle EOS properly, i.e. by closing the socket, under all circumstances where it can arise, and ditto
IOExceptions
on any socket operation - you must respond to all of them exceptSocketTimeoutException
by closing the socket. You must also ensure sockets are closed infinally
blocks.