Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Socket read freezes on Linux despite timeout

843790Sep 22 2008 — edited Oct 9 2008
We're using Linux 2.6 multiprocessor machines and Sun JDK 6.
Servers connect to each other over HTTP, setting socket timeout of 60 seconds.
Despite that sometimes sockets freeze forever in read() operation.

The code is like this:
URL url = new URL(endpoint);
URLConnection c = url.openConnection();
c.setConnectTimeout( 60000 );
c.setReadTimeout( 60000 );
InputStream in = c.getInputStream();

byte[] buffer = new byte[256 * 1024];
int readBytes = -1;
while ((readBytes = inputStream.read(buffer)) != -1) {
// use remote data
}

And the frozen stack trace is:

java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
java.io.BufferedInputStream.read(BufferedInputStream.java:317)
sun.net.www.MeteredStream.read(MeteredStream.java:116)
java.io.FilterInputStream.read(FilterInputStream.java:116)
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2391)
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2386)
com.pmstation.shared.admin.core.MoveFile.pipeStream(MoveFile.java:189)
com.pmstation.shared.admin.core.MoveFile.transferFile(MoveFile.java:173)

Timeout not always working?
So to be sure HTTP read from remote server is never blocking I'm forced to do IO on a separate thread? Or there are better ideas?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2008
Added on Sep 22 2008
23 comments
5,101 views