NIO设置SO_LINGER引发的异常
欢迎关注Github:https://github.com/teaey/
### 背景
银时跟我讲,想从 Netty3迁移到Netty4 。
问其原因是由于 Netty3在容器里会报错,错误堆栈:
java.io.IOException: 无法马上完毕一个非阻止性套接字操作。
at sun.nio.ch.SocketDispatcher.close0(Native Method)
at sun.nio.ch.SocketDispatcher.preClose(SocketDispatcher.java:44)
at sun.nio.ch.SocketChannelImpl.implCloseSelectableChannel(SocketChannelImpl.java:677)
at java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel(AbstractSelectableChannel.java:201)
at java.nio.channels.spi.AbstractInterruptibleChannel.close(AbstractInterruptibleChannel.java:97)
### 分析
看到这个问题,之前我也没有遇到过,只是假设 netty3有这个问题,netty4应该也会存在。那就看看究竟什么导致这个问题。
找到 SocketDispatcher的close0 方法,这是个本地方法:
找到 Windows的实现:
Windows平台通过调用closesocket( winsock2.h)关闭套接字。
if no error occurs, closesocket returns zero. Otherwise, a value of SOCKET_ERROR is
returned, and a specific error code can be retrieved by calling WSAGetLastError.
这里非常明显是发生出错误向上抛出了异常。
于是回过头去看下代码,果然,图为截取的 Netty代码片段:
巨硬的文档是这么说的:
Setting the l_onoff member of the linger structure
to nonzero and the l_linger member with a nonzero timeout interval on a nonblocking socket is not recommended.
意思是,在非堵塞的 Socket情况下不建议设置SO_LINGER參数。
In this case, the call to closesocket will
fail with an error of WSAEWOULDBLOCK if
the close operation cannot be completed immediately. If closesocket fails
with WSAEWOULDBLOCK the
socket handle is still valid, and a disconnect is not initiated. The application must call closesocket again to close the socket.
假设设置了 SO_LINGER,而且制定了超时时间,这时,我们调用 closesocket方法,方法不能马上完毕的话,会抛出 WSAEWOULDBLOCK 错误。可是,这个 socket此时还是有效的,能够一段时间之后再次调用 close方法进行关闭尝试。
### 解决方法
终于,解决方法去掉
这行代码。
改进之后,在调用 close方法时,不会抛出异常而且在底层 socket关闭前,系统会尽可能的把将缓冲队列的数据发送给对端。原文例如以下:
If the l_onoff member of the LINGER structure
is zero on a stream socket, the closesocket call will return immediately and does not receive whether the socket is blocking or nonblocking. However, any data queued for transmission will be sent, if possible, before the underlying socket is closed.
### 总结
在使用NIO 的时候,最好不要配置 SO_LINGER,假设设置了该參数,在 close的时候如缓冲区有数据待写出,会抛出 IOException。
后记:近期银时发现,Zookeeper之前的版本号也是有设置这个參数,而且在最新版本号去掉了这个參数,难道大神们的代码也是Ctrl+C,Ctrl+V过来的。呵呵。
### 參考资料