title: javax.net.ssl.SSLException: Read error: ssl=0x9524b800: I/O error during system call, Connection reset by peer
body: <p>Our clients are starting to see 100s of these "SSLException error - Connection reset by peer" over the last couple of weeks and I can't figure out why</p>
  
  <ol>
  <li><p>We're using Retrofit with okhttp, no special configuration</p>
  
  <pre><code>public class OkHttpClientProvider implements IOkHttpClientProvider {
  
      OkHttpClient okHttpClient;
  
      public OkHttpClientProvider() {
          this.okHttpClient = createClient();
      }
  
      public OkHttpClient getOkHttpClient() {
          return this.okHttpClient;
      }
  
      private OkHttpClient createClient() {
          return new OkHttpClient();
      }
  }
  </code></pre></li>
  </ol>
  
  <p>The above client provider is a singleton. The RestAdapter is built using this injected client (we use dagger) - </p>
  
  <pre><code>RestAdapter.Builder restAdapterBuilder = new RestAdapter.Builder()
                                          .setConverter(converter)
                                          .setEndpoint(networkRequestDetails.getServerUrl())
                                          .setClient(new OkClient(okHttpClientProvider.getOkHttpClient()))
                                          .setErrorHandler(new NetworkSynchronousErrorHandler(eventBus))
                                          );
  </code></pre>
  
  <p>Based on stack overflow solutions what I've found out - </p>
  
  <ol>
  <li><p>The keep alive duration on the server is 180 seconds, OkHttp has a default of 300 seconds</p></li>
  <li><p>The server returns "Connection: close" in its header but the client request sends "Connection: keepAlive"</p></li>
  <li><p>The server supports TLS 1.0 / 1.1 / 1.2 and uses Open SSL</p></li>
  <li><p>Our servers have moved to another hosting provider recently in another geography so I don't know if these are DNS failures or not</p></li>
  <li><p>We've tried tweaking things like keepAlive, reconfigured OpenSSL on the server but for some reason the Android client keeps getting this error</p></li>
  <li><p>It happens immediately without any delay when you try to use the app to post something or pull to refresh (it doesn't even go to network or have a delay before this exception happens which would imply the connection is already broken). But trying it multiple times somehow "fixes it" and we get a success. It happens again later</p></li>
  <li><p>We've invalidated our DNS entries on the server to see if this what caused it but that hasn't helped</p></li>
  <li><p>It mostly happens on LTE but I've seen it on Wifi as well</p></li>
  </ol>
  
  <p>I don't want to disable keep alive because most modern clients don't do that. Also we're using OkHttp 2.4  and this is a problem on post Ice cream sandwich devices so I'm hoping it should take care of these underlying networking issues. The iOS client also gets these exceptions but close to a 100 times less (iOS client uses AFNetworking 2.0). I'm struggling to find new things to try at this point, any help / ideas?</p>
  
  <p><strong>Update - Adding full stack trace through okhttp</strong></p>
  
  <pre><code>      retrofit.RetrofitError: Read error: ssl=0x9dd07200: I/O error during system call, Connection reset by peer
                at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:390)
                at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
                at java.lang.reflect.Proxy.invoke(Proxy.java:397)
                at $Proxy15.getAccessTokenUsingResourceOwnerPasswordCredentials(Unknown Source)
                at com.company.droid.repository.network.NetworkRepository.getAccessTokenUsingResourceOwnerPasswordCredentials(NetworkRepository.java:76)
                at com.company.droid.ui.login.LoginTask.doInBackground(LoginTask.java:88)
                at com.company.droid.ui.login.LoginTask.doInBackground(LoginTask.java:23)
                at android.os.AsyncTask$2.call(AsyncTask.java:292)
                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                at java.lang.Thread.run(Thread.java:818)
         Caused by: javax.net.ssl.SSLException: Read error: ssl=0x9dd07200: I/O error during system call, Connection reset by peer
                at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
                at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:699)
                at okio.Okio$2.read(Okio.java:137)
                at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
                at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
                at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
                at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
                at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
                at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
                at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:917)
                at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:793)
                at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
                at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
                at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
                at com.squareup.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
                at com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
                at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
                at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
                at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
                at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
                at java.lang.reflect.Proxy.invoke(Proxy.java:397)
                at $Proxy15.getAccessTokenUsingResourceOwnerPasswordCredentials(Unknown Source)
                at com.company.droid.repository.network.NetworkRepository.getAccessTokenUsingResourceOwnerPasswordCredentials(NetworkRepository.java:76)
                at com.company.droid.ui.login.LoginTask.doInBackground(LoginTask.java:88)
                at com.company.droid.ui.login.LoginTask.doInBackground(LoginTask.java:23)
                at android.os.AsyncTask$2.call(AsyncTask.java:292)
                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                at java.lang.Thread.run(Thread.java:818)
        ]}
  </code></pre>