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();
5
  • Connection: keep-alive is the default. You're not writing any POST parameters: is that deliberate? Commented Sep 22, 2014 at 7:45
  • thanks @EJP, I just paste more code to the code quote, It's a post operation. please review it again.. Commented Sep 22, 2014 at 7:59
  • Do you have any update on this? I have exactly the same problem Commented Nov 20, 2014 at 14:18
  • @Hamoriz, Sorry, we don't have a good solution on it. Commented Nov 21, 2014 at 2:55
  • Another victim of this issue... Surely there's a solution for this problem -- this is a core Java library. :-| Commented Feb 5, 2015 at 19:36

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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

1 Comment

It wasn't. See the other answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.