Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8231218

HttpUrlConnection.getResponseCode() hangs forever

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Microsoft Windows [Version 6.1.7601]
      Copyright (c) 2009 Microsoft Corporation. All rights reserved.


      C:\Program Files\Java\jdk1.8.0_212\bin>java -version
      java version "1.8.0_212"
      Java(TM) SE Runtime Environment (build 1.8.0_212-b10)
      Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)

       

      C:\Program Files\Java\jdk1.8.0_212\bin>javac -version
      javac 1.8.0_212

      A DESCRIPTION OF THE PROBLEM :
      HttpUrlConnection.getResponseCode() hangs forever if Content-Length header is missing.
      Server-Sent Events responses do not have "Content-Length" header. This causes HttpUrlConnection.getResponseCode() to hang if server response header and data come in one TCP packet.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Build and run test example code.

      "C:\Program Files\Java\jdk1.8.0_212\bin\javac" -g -d out_test test.java

      "C:\Program Files\Java\jdk1.8.0_212\bin\java" -cp out_test -agentlib:jdwp=transport=dt_shmem,address=jdbconn,server=y,suspend=n Test





      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program exits normally.
      ACTUAL -
      The program hangs.

      Backtrace is the following:
      C:\Program Files\Java\jdk1.8.0_212\bin>jdb -attach jdbconn
      Set uncaught java.lang.Throwable
      Set deferred uncaught java.lang.Throwable
      Initializing jdb ...
      > suspend
      All threads suspended.
      > where all
      Thread-0:
        [1] java.lang.Thread.sleep (native method)
        [2] Test$1.run (test.java:35)
      Attach Listener:
      Signal Dispatcher:
      Finalizer:
        [1] java.lang.Object.wait (native method)
        [2] java.lang.ref.ReferenceQueue.remove (ReferenceQueue.java:144)
        [3] java.lang.ref.ReferenceQueue.remove (ReferenceQueue.java:165)
        [4] java.lang.ref.Finalizer$FinalizerThread.run (Finalizer.java:216)
      Reference Handler:
        [1] java.lang.Object.wait (native method)
        [2] java.lang.Object.wait (Object.java:502)
        [3] java.lang.ref.Reference.tryHandlePending (Reference.java:191)
        [4] java.lang.ref.Reference$ReferenceHandler.run (Reference.java:153)
      main:
        [1] java.net.SocketInputStream.socketRead0 (native method)
        [2] java.net.SocketInputStream.socketRead (SocketInputStream.java:116)
        [3] java.net.SocketInputStream.read (SocketInputStream.java:171)
        [4] java.net.SocketInputStream.read (SocketInputStream.java:141)
        [5] java.io.BufferedInputStream.fill (BufferedInputStream.java:246)
        [6] java.io.BufferedInputStream.read1 (BufferedInputStream.java:286)
        [7] java.io.BufferedInputStream.read (BufferedInputStream.java:345)
        [8] sun.net.www.http.HttpClient.parseHTTPHeader (HttpClient.java:735)
        [9] sun.net.www.http.HttpClient.parseHTTP (HttpClient.java:678)
        [10] sun.net.www.protocol.http.HttpURLConnection.getInputStream0 (HttpURLConnection.java:1 587)
        [11] sun.net.www.protocol.http.HttpURLConnection.getInputStream (HttpURLConnection.java:1 492)
        [12] java.net.HttpURLConnection.getResponseCode (HttpURLConnection.java:480)
        [13] Test.main (test.java:53)
      >

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.net.URLEncoder;
      import java.net.*;

       

      public class Test
      {
          public static void main(String[] args) throws Exception
          {
              new Thread() {
              @Override public void run() {
                  try {
                     ServerSocket server = new ServerSocket(8087);
                     Socket socket = server.accept();
                     InputStream is = socket.getInputStream();
                     byte h[] = new byte[200];
                      is.read(h);
                         OutputStream os = socket.getOutputStream();

       

                     String s = "HTTP/1.1 200 OK\n"+
                     "Access-Control-Allow-Origin: *\n"+
                     "Content-Type: text/event-stream\n"+
                     "Cache-Control : no-cache\n"+
                     "Connection : keep-alive\n"+
                     "\n"+
                     "data\n";
                     byte[] arr = s.getBytes();

       

                     os.write(arr);
          
                         System.out.println("Data sent\n");
                     Thread.sleep(10000);

       

                  } catch(Exception e) {
                      System.out.println(e.toString());
                  }
              }
              }.start();

       

              try{
                  Thread.sleep(1000);
              }catch(Exception e){}

       

                  URL url = new URL("http://127.0.0.1:8087/eventhub/events");
                  HttpURLConnection c = (HttpURLConnection)url.openConnection();
                  c.setRequestMethod("GET");
                  c.setRequestProperty("Accept", "text/event-stream");
                  c.connect();
                  System.out.println("Resp code=" + c.getResponseCode());
                  BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
                  String line = reader.readLine();
                  System.out.println("Received: " + line);
                  reader.close();
                  c.disconnect();
              }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      There is no workaround.

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal (Inactive)
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: