Skip to content

Ungraceful termination of thread if connection times out  #8

Description

@Gikkman

I've recently had some problems with connecting to the Twitch API, and I have noticed that this library doesn't really handle that error in the HttpResponseHandle class.

 protected void processResponse(HttpURLConnection connection) {
    try {
        // Response
        int responseCode = connection.getResponseCode();
        long contentLength = connection.getContentLength();
        Map<String, List<String>> responseHeaders = connection.getHeaderFields();

        // 'Successful' response codes will be in interval [200,300)
        if (responseCode >= 200 && responseCode < 300) {
            byte[] responseContent = readFrom(connection.getInputStream(), contentLength);
            onSuccess(responseCode, responseHeaders, responseContent);
        } else {
            byte[] responseContent = readFrom(connection.getErrorStream(), contentLength);
            onFailure(responseCode, responseHeaders, responseContent);
        }
    } catch (IOException e) {
        onFailure(e);
    }
}

If this method above encounters a time out on int responseCode = connection.getResponseCode(); then the entire method fails and the thread throws an exception. But this exception is not caught anywhere.

This is the error code I receive:

java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at com.mb3364.http.HttpResponseHandler.processResponse(HttpResponseHandler.java:137)
at com.mb3364.http.HttpClient.request(HttpClient.java:95)
at com.mb3364.http.HttpClient.get(HttpClient.java:139)
at com.mb3364.http.AsyncHttpClient.access$201(AsyncHttpClient.java:12)
at com.mb3364.http.AsyncHttpClient$3.run(AsyncHttpClient.java:68)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

This might be fixable by simply catching the SocketTimeoutException.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions