Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app crash okhttp3 HTTP FAILED: javax.net.ssl.SSLException: Read error: I/O error during system call, Software caused connection abort

I am working on android native kotlin project.
I am testing app in android real Oneplus 6 device with Android version 10.
Please find below details from build.gradle file for latest okhttp and retrofit dependencies which I am using for project.
okhttp

implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"` 

Retrofit

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

I have already set relevant timeouts for okhttpclient.

.connectTimeout(HTTP_CONNECT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(HTTP_READ_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(HTTP_WRITE_TIMEOUT, TimeUnit.SECONDS)

I am using okhttp3.Interceptor. Please find implementation below.

import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

class AuthorizationInterceptor(private val sharedPreferenceService: SharedPreferenceService) :
    Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val requestBuilder = chain.request().newBuilder()
            .addHeader("Content-Type", "application/json")
            .addHeader("Accept", "application/json")

        return chain.proceed(requestBuilder.build())
    }
  }

Observations / Issues

  1. If API call is in progress and we try to change or disable network/internet, okhttp3 itself internally crashing as soon as below line executes from above snippets so App is crashing. return chain.proceed(requestBuilder.build())

I have figured out below okhttp internal crashes logs from my android studio that can help you to find root cause and fixes.

FATAL EXCEPTION: main
    Process: com.pg.posm, PID: 8770
    javax.net.ssl.SSLException: Read error: ssl=0x7afca87588: I/O error during system call, Software caused connection abort
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.pg.posm, PID: 26238
    java.net.ConnectException: Failed to connect to hostname/ip:port

I also tried to wrap above Interceptor snippets using try-catch but still okhttp3 itself crashing so App is crashing.
We don't have any control to solve it.
Please find below Interceptor snippets with wrapped try-catch implementation.

import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import timber.log.Timber
import java.io.IOException

class AuthorizationInterceptor(private val sharedPreferenceService: SharedPreferenceService) :
    Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val requestBuilder = chain.request().newBuilder()
            .addHeader("Content-Type", "application/json")
            .addHeader("Accept", "application/json")
     
        try {
            return chain.proceed(requestBuilder.build())
        }catch (e: Throwable) {
            
            if (e is IOException) {
                
                throw e
            } else {
                
                throw IOException(e)
            }
        }
    }   
}

Anyone can try to make small android project with relevant setup and snippets and see.
Please provide necessary fixes or approach for this blocker issue as many people are facing in the world.
Please let me know in case you need more details. I would more than happy to provide you.

like image 684
Durgesh Patel Avatar asked Sep 30 '20 04:09

Durgesh Patel


1 Answers

I am trying to address the problem. This error isn't surprising.

As you stated

If API call is in progress and we try to change or disable network/internet, okhttp3 itself internally crashing as soon as below line executes from above snippets so App is crashing.

why is this so strange for you?

As logs show

FATAL EXCEPTION: main
Process: com.pg.posm, PID: 8770
javax.net.ssl.SSLException: Read error: ssl=0x7afca87588: I/O error during system call,
Software caused connection abort

you have an SSL/TLS connection which means, before establishing a connection to the server and transferring data, there is a lot of stuff that happens transparently for example:

  • handshaking
  • sending/receiving public keys
  • generating a session key
  • establishing a secure channel

The above actions and whereby the secured channel is only valid for the current connected secure socket, not any other ones.

Again backing your statement

If API call is in progress and we try to change or disable network/internet

The above actions make the previously connected secure socket, invalid and the SslException has been thrown (MitM protection).

I hope the above statements were helpful.

like image 198
A Farmanbar Avatar answered Sep 23 '22 09:09

A Farmanbar