10

I got a stuck thread at java.net.SocketInputStream.socketRead0(Native Method). Please see the thread dump below. It's been in this status for 3 hours.

Thread-0" prio=10 tid=0x00007facd02a5000 nid=0x309 runnable [0x00007facd4a43000]
java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
    at sun.security.ssl.InputRecord.read(InputRecord.java:480)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
    - locked <0x00000000e34a0428> (a java.lang.Object)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:884)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
    - locked <0x00000000e34a0590> (a sun.security.ssl.AppInputStream)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    - locked <0x00000000e30408b8> (a java.io.BufferedInputStream)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:633)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322)
    - locked <0x00000000e3031d00> (a sun.net.www.protocol.https.DelegateHttpsURLConnection)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    - locked <0x00000000e3031c80> (a sun.net.www.protocol.https.HttpsURLConnectionImpl)

And I've set the connect timeout and read timeout in URLConnection. Please see the code snippet below. So I don't know why it'd hang at java.net.SocketInputStream.socketRead0. I just have this problem occasionally. Any suggestion is appreciated!

public String sendPost(String params) throws Throwable {
PrintWriter out = null;
String htmlContent = null;
try
{
  StringBuffer strBuffer = new StringBuffer();
  URL url = new URL(this.webUrl);
  URLConnection urlConnection = url.openConnection();
  urlConnection.setConnectTimeout(3000);
  urlConnection.setReadTimeout(3000);

  urlConnection.setRequestProperty("accept", "*/*");
  urlConnection.setRequestProperty("connection", "Keep-Alive");
  urlConnection.setRequestProperty("user-agent", "***");

  urlConnection.setDoOutput(true);
  urlConnection.setDoInput(true);

  out = new PrintWriter(urlConnection.getOutputStream());
  out.print(params);
  out.flush();

  BufferedReader bufferdReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
  String readLine = null;
        try {
            while ((readLine = bufferdReader.readLine()) != null) {
                strBuffer.append(readLine);
            }
        } catch (Throwable e) {
            return htmlContent;
        }
        htmlContent = strBuffer.toString();
        bufferdReader.close();
Kirby
  • 15,127
  • 10
  • 89
  • 104
BurningDocker
  • 133
  • 1
  • 8

2 Answers2

4

I identified this same bug today on our systems. This is a bug in the JDK caused by the native implementation used in java. Was fixed as recently as 21st September 2016, so should be available in later JDK releases.

This is the bug: https://bugs.openjdk.java.net/browse/JDK-8075484

If you can't wait, seems like someone has created a workaround: https://github.com/nukesparrow/JNativeSocket

fmcato
  • 172
  • 1
  • 12
-1

I think this is a bug with URLConnection. Use Sockets and timeout on them to check.