Skip to content

NetHttpTransportTest.testDisconnectShouldNotWaitToReadResponse fails since JDK 23 #2063

@diegomarquezp

Description

@diegomarquezp

Overview

Since JDK 23, NetHttpTransportTest.testDisconnectShouldNotWaitToReadResponse fails with:

[ERROR] com.google.api.client.http.javanet.NetHttpTransportTest.testDisconnectShouldNotWaitToReadResponse -- Time elapsed: 20.06 s <<< ERROR!
java.net.SocketTimeoutException: Read timed out
	at java.base/sun.nio.ch.NioSocketImpl.timedRead(NioSocketImpl.java:278)
	at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:304)
	at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:346)
	at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:796)
	at java.base/java.net.Socket$SocketInputStream.implRead(Socket.java:1116)
	at java.base/java.net.Socket$SocketInputStream.read(Socket.java:1103)
	at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:291)
	at java.base/java.io.BufferedInputStream.read1(BufferedInputStream.java:347)
	at java.base/java.io.BufferedInputStream.implRead(BufferedInputStream.java:420)
	at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:399)
	at java.base/sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:827)
	at java.base/sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:759)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1706)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
	at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:531)
	at com.google.api.client.http.javanet.NetHttpResponse.(NetHttpResponse.java:36)
	at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:152)
	at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84)
	at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012)
	at com.google.api.client.http.javanet.NetHttpTransportTest.testDisconnectShouldNotWaitToReadResponse(NetHttpTransportTest.java:224)

Cause

In the release notes of JDK 23, we have:

core-libs/java.net
➜ HttpServer No Longer Immediately Sends Response Headers (JDK-6968351)

This changes the behavior of FakeServer in a way that although we set the headers before Thread.sleep(), they will still not be sent, blocking the execution of the call:
Test code:

   final HttpHandler handler =
        new HttpHandler() {
          @Override
          public void handle(HttpExchange httpExchange) throws IOException {
            byte[] response = httpExchange.getRequestURI().toString().getBytes();
            httpExchange.sendResponseHeaders(200, response.length); // This does not trigger OutputStream.flush() anynmore

            // Sleep for longer than the test timeout
            try {
              Thread.sleep(100_000);
            } catch (InterruptedException e) {
              throw new IOException("interrupted", e);
            }
            try (OutputStream out = httpExchange.getResponseBody()) {
              out.write(response);
            }
          }
        };

    try (FakeServer server = new FakeServer(handler)) {
      HttpTransport transport = new NetHttpTransport();
      GenericUrl testUrl = new GenericUrl("http://localhost/foo//bar");
      testUrl.setPort(server.getPort());
      com.google.api.client.http.HttpResponse response =
          transport.createRequestFactory().buildGetRequest(testUrl).execute(); // This call is now blocking
      // disconnect should not wait to read the entire content
      response.disconnect(); // This is never reached
}

Blocked code:

 NetHttpResponse(HttpURLConnection connection) throws IOException {
    this.connection = connection;
    int responseCode = connection.getResponseCode(); // this line blocks the execution
    this.responseCode = responseCode == -1 ? 0 : responseCode;
    responseMessage = connection.getResponseMessage();
    List<String> headerNames = this.headerNames;
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions