React-native: Android crash when network changes from Good to Bad

Created on 17 Oct 2016  路  29Comments  路  Source: facebook/react-native

Issue Description

When there are fetch requests made on Android and the network changes from a good connection to a very bad connection, sometimes a crash occurs in RealBufferedSink.java:39.

Steps to Reproduce / Code Snippets

  1. Make constant fetch requests (Once every couple of seconds)
  2. Switch between a good network connection and a very bad one (if an iPhone is available it can be used as a hotspot for the Android device and can switch between LTE and very Bad Network)

Switching between the networks should reproduce the problem fairly quickly.

Expected Results

The error:

java.lang.IllegalStateException: closed
at okio.RealBufferedSink.write(RealBufferedSink.java:39)
at okio.ForwardingSink.write(ForwardingSink.java:35)
at com.facebook.react.modules.network.ProgressRequestBody$1.write(ProgressRequestBody.java:58)
at okio.RealBufferedSink.flush(RealBufferedSink.java:216)
at com.facebook.react.modules.network.ProgressRequestBody.writeTo(ProgressRequestBody.java:48)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:47)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.access$100(RealCall.java:33)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:120)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

Additional Information

  • React Native version: 0.41.0 (All previous version also have this issue)
  • Platform(s) (iOS, Android, or both?): Android
  • Operating System (macOS, Linux, or Windows?): macOS
Android Ran Commands Fixed Stale

Most helpful comment

So, I have found a temporary solution to this problem which is based on catching the error. After some testing it seems that if you catch the OkHttp error the network request will fail but the app won't crash. So if anyone is interested here is the temporary solution:

Edit your MainApplication.java

    @Override
    public void onCreate() {
        super.onCreate();

        // Setup handler for uncaught exceptions.
        Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
        {
        @Override
        public void uncaughtException (Thread thread, Throwable e)
        {
            handleUncaughtException (thread, e);
        }
        });
    }

   public void handleUncaughtException (Thread thread, Throwable e)
    {

        if(e.getMessage() != "closed" || thread.getName() != "OkHttp Dispatcher"){
            //Kill the app
            System.exit(1);
        }else{
            //If the OkHttp error occurs we ignore it
            Log.e("OkHttp Exception","Received exception " + e.getMessage() + "From thread " + thread.getName());
        }

    }

When I have the time I will look into the root cause of this issue as this is a very poor fix. If I find a solution, will make PR.

All 29 comments

@armadilio3 Same issue here. did you got that fixed .?

@saleeh93 still looking into it, maybe you could describe how you can replicate the crash since I am working on a fairly large project and I am only beginning to pinpoint the exact place of origin for this bug

@armadilio3 I am also working on large project and I never had that issue when I am testing it, I got those errors from Crashlytics. I didn't even understand when that is coming up

@armadilio3 Any updates ?

Same issue here :(

@lucasfeliciano Still waiting to get this fixed

So, I have found a temporary solution to this problem which is based on catching the error. After some testing it seems that if you catch the OkHttp error the network request will fail but the app won't crash. So if anyone is interested here is the temporary solution:

Edit your MainApplication.java

    @Override
    public void onCreate() {
        super.onCreate();

        // Setup handler for uncaught exceptions.
        Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
        {
        @Override
        public void uncaughtException (Thread thread, Throwable e)
        {
            handleUncaughtException (thread, e);
        }
        });
    }

   public void handleUncaughtException (Thread thread, Throwable e)
    {

        if(e.getMessage() != "closed" || thread.getName() != "OkHttp Dispatcher"){
            //Kill the app
            System.exit(1);
        }else{
            //If the OkHttp error occurs we ignore it
            Log.e("OkHttp Exception","Received exception " + e.getMessage() + "From thread " + thread.getName());
        }

    }

When I have the time I will look into the root cause of this issue as this is a very poor fix. If I find a solution, will make PR.

@armadilio3 You are tricky 馃槈

@armadilio3 I use yours,but It`s not running.

@yunlongz are you building from source?
I use this "solution" and work fine.

@armadilio3's solution works fine for me. But does anyone have any news on this problem?

Anyone found a real fix ? Problem still exist.

I tried @armadilio3 issue but I still have same crashes (maybe because I use react-native-fabric).

I got the same issue, and @armadilio3 solutions is better...

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

It's not stale it's real and happens in my app too as @armadilio3 described it.

seems related to this bug in okhttp: https://github.com/square/okhttp/issues/3521

That was fixed in okhttp v3.9 but react-native is using v3.6. Can we update the dependency?

@pmachowski We have the same issue seems like there is a solution in #11016 in the last post. We will test it tomorrow. Is there any finite solution to this problem?

Same issue for me.. any solution?

@pavle93 could you find a solution?

@ pavle93 the same problem.... could you find a solution?

@armadilio3's solution works fine for me. but there is an error in it, we have to import android.util.log in MainApplication.java.

import android.util.Log;
@pmachowski Problem still in version 3.9.0

Worked for me by adding this line to android/app/build.gradle
dependencies {
...
compile 'com.squareup.okhttp3:okhttp:3.9.0'
}

@workostap that works for me too but the problem still persist sometimes.

To use the latest okhttp you should also upgrade your react native version.

You can also solve it by using
com.squareup.okhttp3:okhttp:3.5.0 or com.squareup.okhttp3:okhttp:3.6.0
withcom.facebook.react:react-native:0.48.X

trying to combine higher versions with lower of each library will lead you to this error
Undefined is not an object (evaluating 'Sn[e]')

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

(quick update, we merged a fix for it and we'll probably cherry pick for 0.57-rc3)

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

Was this page helpful?
0 / 5 - 0 ratings