Skip to content

Commit

Permalink
networking: Fix requests failing when turning network off and on face…
Browse files Browse the repository at this point in the history
…book#19709.

This bug is probably actually a bug in OkHttp: square/okhttp#4079
Both issues linked above contain extensive details about the issue,
its likely origins and how to reproduce it. A short summary of the
issue and the fix in this commit:

On Android, disconnecting from the network somehow corrupts the idle
connections and ongoing calls in okhttp clients. New requests made over
these clients fail. This commit works around that bug by evicting the idle
connection pool when we receive a DISCONNECTED or CONNECTING event (we
don't know yet if only one or both of them cause the issue). Technically,
to fully fix this issue, we would also need to cancel all ongoing calls.
However, cancelling all ongoing calls is aggressive, and not always desired
(when the app disconnects only for a short time, ongoing calls might still
succeed). In practice, just evicting idle connections results in this issue
occurring less often, so let's go with that for now.
  • Loading branch information
roberthoenig committed Jun 28, 2018
1 parent 370bcff commit aa2e00a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@

import javax.annotation.Nullable;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;

import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.core.PermissionAwareActivity;
import com.facebook.react.modules.core.PermissionListener;
import com.facebook.react.modules.network.OkHttpClientProvider;

/**
* Base Activity for React Native applications.
Expand Down Expand Up @@ -50,6 +54,10 @@ protected ReactActivityDelegate createReactActivityDelegate() {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDelegate.onCreate(savedInstanceState);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE)
== PackageManager.PERMISSION_GRANTED) {
OkHttpClientProvider.addNetworkListenerToEvictIdleConnectionsOnNetworkChange(getApplicationContext());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@

package com.facebook.react.modules.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;

import com.facebook.common.logging.FLog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;
Expand All @@ -33,6 +43,9 @@ public class OkHttpClientProvider {
// User-provided OkHttpClient factory
private static @Nullable OkHttpClientFactory sFactory;

private final static Set<OkHttpClient> sClients = Collections.newSetFromMap(
new WeakHashMap<OkHttpClient, Boolean>());

public static void setOkHttpClientFactory(OkHttpClientFactory factory) {
sFactory = factory;
}
Expand All @@ -43,6 +56,47 @@ public static OkHttpClient getOkHttpClient() {
}
return sClient;
}

/*
See https://github.com/facebook/react-native/issues/19709 for context.
We know that connections get corrupted when the connectivity state
changes to disconnected, but the debugging of this issue hasn't been
exhaustive and it's possible that other changes in connectivity also
corrupt idle connections. `CONNECTIVITY_ACTION`s occur infrequently
enough to go safe and evict all idle connections and ongoing calls
for the events DISCONNECTED and CONNECTING. Don't do this for CONNECTED
since it's possible that new calls have already been dispatched by the
time we receive the event.
*/
public static void addNetworkListenerToEvictIdleConnectionsOnNetworkChange(Context context) {
final BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
return;
}
final Bundle extras = intent.getExtras();
final NetworkInfo info = extras.getParcelable("networkInfo");
final NetworkInfo.State state = info.getState();
if (state == NetworkInfo.State.CONNECTED) {
return;
}
final PendingResult result = goAsync();
final Thread thread = new Thread() {
public void run() {
for (OkHttpClient client: sClients) {
client.connectionPool().evictAll();
}
result.finish();
}
};
thread.start();
}
};
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(br, intentFilter);
}

// okhttp3 OkHttpClient is immutable
// This allows app to init an OkHttpClient with custom settings.
Expand All @@ -54,7 +108,9 @@ public static OkHttpClient createClient() {
if (sFactory != null) {
return sFactory.createNewNetworkModuleClient();
}
return createClientBuilder().build();
final OkHttpClient client = createClientBuilder().build();
registerClientToEvictIdleConnectionsOnNetworkChange(client);
return client;
}

public static OkHttpClient.Builder createClientBuilder() {
Expand All @@ -68,6 +124,10 @@ public static OkHttpClient.Builder createClientBuilder() {
return enableTls12OnPreLollipop(client);
}

public static void registerClientToEvictIdleConnectionsOnNetworkChange(OkHttpClient client) {
sClients.add(client);
}

/*
On Android 4.1-4.4 (API level 16 to 19) TLS 1.1 and 1.2 are
available but not enabled by default. The following method
Expand Down

0 comments on commit aa2e00a

Please sign in to comment.