OSX上的Java进程不会释放套接字

OSX上的Java进程不会释放套接字

本文介绍了Mac OSX上的Java进程不会释放套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在经历了一个奇怪的问题(实际上太多)。



我正在运行一个服务器应用程序,它为自己绑定一个套接字。 / p>

但是一会儿,套接字没有被释放。虽然Eclipse报告Terminate失败,但进程死机,但是它从ps和JConsole / JVisualVM中正确消失。 'lsof'也不再显示该端口。但是,当我尝试将服务器重新启动到同一端口时,我仍然收到此错误:

  .BindException:地址已经在使用
在sun.nio.ch.Net.bind(本机方法)
在sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:126)
在sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)

问题是最糟糕的在我的单元测试中,从未完全运行,因为这将确定在其中一个测试(其中重新创建服务器)之后发生。



我正在运行MacOSX 10.7。 3



Java(TM)SE运行时环境(build 1.6.0_31-b04-415-11M3635)
Java HotSpot(TM)64位服务器虚拟机构建20.6-b01-415,混合模式)



我也有Parallels,而且经常出现的问题是由Parallels网络适配器引起的,但我不确定这完全与这个问题有任何关系(我没有任何联系他们的支持帮助到目前为止)。



唯一有助于解决这种情况的是重启OSX。



任何想法?



-



这是打开套接字的相关代码:

  channel =(ServerSocketChannel)ServerSocketChannel.open()。configureBlocking(false); 
channel.socket()。bind(addr,0);

,它被

  channel.close(); 

但我认为这个过程被卡在这里,然后Eclipse杀死它。



-



netstat -an(对于端口6007):

  tcp4 73 0 127.0.0.1.6007 127.0.0.1.51549 ESTABLISHED 
tcp4 0 0 127.0.0.1.51549 127.0.0.1.6007 ESTABLISHED
tcp4 73 0 127.0。 0.1.6007 127.0.0.1.51544 CLOSE_WAIT
tcp4 0 0 127.0.0.1.6007 127.0.0.1.51543 CLOSE_WAIT
tcp4 0 0 10.37.129.2.6007 *。* LISTEN
tcp4 0 0 10.211.55.2.6007 *。* LISTEN
tcp4 0 0 127.0.0.1.6007 *。* LISTEN
tcp4 0 0 10.50.100.236.6007 *。* LISTEN

-



现在我在每次打开套接字后收到这个异常测试(网这种情况下的输出):

 导致:java.net.SocketTimeoutException:在java处读取超时
。 net.SocketInputStream.socketRead0(Native Method)
在java.net.SocketInputStream.read(SocketInputStream.java:129)
在java.net.SocketInputStream.read(SocketInputStream.java:182)

-



从Eclipse中停止进程我得到终止失败,但是lsof -i TCP:6007没有显示,进程不再被'ps'找到。 netstat输出没有改变...



我可以以某种方式杀死套接字而不重新启动(这将有助于一点点)?



-



更新5.5.12:



我现在在Eclipse中运行测试调试器。这次测试在18种方法之后就被卡住了。 15分钟后,我停了主线。这是堆栈:

 线程[主](已暂停)
FileDispatcher.preClose0(FileDescriptor)行:不可用[native method]
SocketDispatcher.preClose(FileDescriptor)行:41
ServerSocketChannelImpl.implCloseSelectableChannel()行:208 [局部变量不可用]
ServerSocketChannelImpl(AbstractSelectableChannel).implCloseChannel()行:201
ServerSocketChannelImpl(AbstractInterruptibleChannel).close()行:97
...

-



嗯,看起来这个过程没有被杀死,毕竟 - 也不会杀死-9(我注意到过程712,也可能是710是TestNG进程):

  $ kill -9 712 
$ ps xa | grep java
700? ?E 0:00.00(java)
712? ?E 0:00.00(java)
797 s005 S + 0:00.00 grep java

-
编辑:10.5.12:



?上面的ps输出中的E表示进程正在退出。在没有重新启动的情况下,我无法找到任何方法来完全杀死这样一个进程。一些其他应用程序也注意到同样的问题。找不到解决方案:



解决方案

所以看起来问题在于在Mac版本的JDK 6中实现Selector。安装新的Oracle JDK 7u4可以解决这个问题,而与Selector的使用方式无关。


I am experiencing an odd problem every now and then (too often actually).

I am running a server application, which is binding a socket for itself.

But once in a while, the socket is not released. The process dies, although Eclipse reports that Terminate failed, however it disappears properly from 'ps' and JConsole/JVisualVM. 'lsof' also displays nothing for the port anymore. But still, I get this error when I try to start the server again to the same port:

Caused by: java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind(Native Method)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:126)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)

The problem is worst in my unit tests, which never run fully, because this will for sure occur after one of the tests (which all recreate the server).

I am running MacOSX 10.7.3

Java(TM) SE Runtime Environment (build 1.6.0_31-b04-415-11M3635)Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01-415, mixed mode)

I have also Parallels, and often the problem looks like it's caused by the Parallels network adapter, but I am not sure if it has anything to do with this problem after all (I have contacted their support without any help so far).

The only thing that helps to resolve the situation is to reboot OSX.

Any ideas?

--

This is the relevant code to open the socket:

channel = (ServerSocketChannel) ServerSocketChannel.open().configureBlocking(false);
 channel.socket().bind( addr, 0 );

and it is closed by

  channel.close();

But I assume that the process gets stuck here and then Eclipse kills it.

--

netstat -an (for port 6007):

tcp4      73      0  127.0.0.1.6007         127.0.0.1.51549        ESTABLISHED
tcp4       0      0  127.0.0.1.51549        127.0.0.1.6007         ESTABLISHED
tcp4      73      0  127.0.0.1.6007         127.0.0.1.51544        CLOSE_WAIT
tcp4       0      0  127.0.0.1.6007         127.0.0.1.51543        CLOSE_WAIT
tcp4       0      0  10.37.129.2.6007       *.*                    LISTEN
tcp4       0      0  10.211.55.2.6007       *.*                    LISTEN
tcp4       0      0  127.0.0.1.6007         *.*                    LISTEN
tcp4       0      0  10.50.100.236.6007     *.*                    LISTEN

--

And now I get this exception after the socket is opened for every test (netstat output from this situation):

Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:129)
    at java.net.SocketInputStream.read(SocketInputStream.java:182)

--

Stopping the process from eclipse I got "Terminate failed", but lsof -i TCP:6007 is displaying nothing and the process is no longer found by 'ps'. netstat output did not change...

Can I somehow kill the socket without rebooting (that would help a litte bit already)?

--

UPDATE 5.5.12:

I ran the tests now in Eclipse debugger. This time the tests got stuck after 18 methods. I stopped the main thread after it was stuck around 15 minutes. This is the stack:

Thread [main] (Suspended)
    FileDispatcher.preClose0(FileDescriptor) line: not available [native method]
    SocketDispatcher.preClose(FileDescriptor) line: 41
    ServerSocketChannelImpl.implCloseSelectableChannel() line: 208 [local variables unavailable]
    ServerSocketChannelImpl(AbstractSelectableChannel).implCloseChannel() line: 201
    ServerSocketChannelImpl(AbstractInterruptibleChannel).close() line: 97
...

--

Hmm, it looks like the process is not killed, after all - and does not die to kill -9 either (I noticed that process 712 and probably also 710 are the TestNG processes):

$ kill -9 712
$ ps xa | grep java
  700   ??  ?E     0:00.00 (java)
  712   ??  ?E     0:00.00 (java)
  797 s005  S+     0:00.00 grep java

--Edit: 10.5.12:

?E in the ps output above means that the process is exiting. I could not find any means to kill such a process fully without rebooting. The same issue has been noticed with some other applications. No solutions found:

http://www.google.com/search?q=ps+process+is+exiting+osx

解决方案

So it seems that the problem lies in the implementation of Selector in the Mac version of JDK 6. Installing the new Oracle JDK 7u4 fixes the issue, independent of how the Selector is used.

这篇关于Mac OSX上的Java进程不会释放套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 03:26