i am running into a similar issue as described here: Java Linux Nonblocking Socket Timeout Behavior
I have an application implemented with Java NIO. It keeps track of a bunch of sockets, and when they're ready for reading, my application will read in a loop (removed code and some logic for brevity):
if (selkey.isReadable()) {
int nread;
while (true) {
// read the header
nread = mSocketChannel.read(mHeaderBuffer);
if (nread == -1)
return;
handle_message_header();
// read the body
nread = mSocketChannel.read(mPayloadBuffer);
if (nread == -1)
return;
handle_message_body();
}
}
But very, very rarely I receive a timeout exception in the first read():
java.io.IOException: Connection timed out
at sun.nio.ch.FileDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:21)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:202)
at sun.nio.ch.IOUtil.read(IOUtil.java:175)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:243)
I digged into the jdk sources, and the read0 function simply calls read() on the socket handle. The "Connection timed out" exception is thrown if read() returns -1 and errno == ETIMEDOUT.
We do not use soSetTimeout() or the tcp keepalive option. And since i was seeing this only on a client's cluster i am not able to reproduce it (nor do i have the output of netstat or other tools).
I wonder in which cases does the linux kernel return ETIMEDOUT in a nonblocking read()? Is this a bug or a feature?
More information about the machine on which this appeared:
Linux slave1 2.6.18-164.e15 #1 SMP Thu Sep 3 03:28:30 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
CentOS 5.4
Thanks Chris
Edit: According to my log file (and the program flow), the socket was created when the server accepted an incoming connection. Then there was at least one successful recv from that socket, but twice the server then failed to write. And then i caught the exception when reading. The log file does not have much information - i am therefore not 100% sure about my analysis so far. I have added lots of debug output to the socket routines and now i am better prepared for the next time.
Thanks for all the helpful comments!