square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.02k stars 7.3k forks source link

UnknownHostException from OkHttp3.Dns.lookup cause app crash #1829

Closed DASAR-zz closed 8 years ago

DASAR-zz commented 8 years ago

I saw in Fabric (Crashlitics) crash caused by UnknownHostException from okhttp3.Dns$1.lookup

Here crash report from Fabric: http://crashes.to/s/faa5cfd3554

What I can do to prevent this issue?

JakeWharton commented 8 years ago

You cannot avoid it. On flaky networks, Wi-Fis that require sign in, and other similar cases you won't be able to resolve DNS. All IOException subclasses need handled by your application layer since networks are inherently unreliable to work 100% of the time.

autonomousapps commented 7 years ago

I just ran into this testing my app in Airplane mode. I was surprised, since I'm using RxJava2 and have an onError handler. That handler does get hit, but then the app crashes immediately afterwards. What am I missing?

If it's useful, this is how I create my retrofit service:

    private <T> T createService(String endpoint, Class<T> serviceClass, OkHttpClient okClient) {
        RxJava2CallAdapterFactory rxAdapter = RxJava2CallAdapterFactory.createWithScheduler(RxUtil.ioScheduler);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(endpoint)
                .client(okClient) // base client plus some interceptors for logging/header/useragent
                .addCallAdapterFactory(rxAdapter)
                .addConverterFactory(GsonAdapterFactory.gsonConverterFactory())
                .build();

        return retrofit.create(serviceClass);
    }
bobtune commented 7 years ago

@autonomousapps We have exactly the same scenario. RxJava2 with onError handler, but the exception is crashing the app. Did you get anywhere with this?

autonomousapps commented 7 years ago

@bobtune I haven't seen the error for a few days, but I'm in the middle of an incremental rollout of the latest version of my app (and maybe I've been lucky). Since encountering this, this is what I've done to try to prevent further crashes:

  1. Changed my Retrofit services from returning Flowable<Response<Thing>> to Flowable<Result<Thing>>. I feel like this shouldn't matter, given the javadoc on Response saying it passes IOException onto the onError handler, but I tried anyway. Result gives us direct access to Throwables.
  2. Probably more importantly (I think) I updated my gradle dependencies to the latest version of the rxjava2 adapter. I was using v1.1.0 with the com.jakewharton package. Now I'm using this version:
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'

For good measure, here are all my related dependencies:

    compile 'com.google.code.gson:gson:2.8.0'

    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    compile 'com.squareup.okhttp3:okhttp:3.5.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
    compile 'com.squareup.okio:okio:1.11.0'