developerzjy
developerzjy

Reputation: 31

Android ConnectivityService and android.net.ConnectivityManager$TooManyRequestsException

I've collected a lot of crashes about TooManyRequestsException.

Caused by: android.net.ConnectivityManager$TooManyRequestsException
at android.net.ConnectivityManager.convertServiceException(ConnectivityManager.java:3510)
at android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:3714)
at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:4035)
at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:4016)
at com.tonyodev.fetch2.provider.NetworkInfoProvider.<init>(NetworkInfoProvider.kt:82)
at com.tonyodev.fetch2.fetch.e$b.<init>(FetchModulesBuilder.kt:103)
at com.tonyodev.fetch2.fetch.e.a(FetchModulesBuilder.kt:199)

I'm sure it's not fetch2 problem because it's a singleton in my code.

First, I read source code of ConnectivityManager and found where the exception was thrown, and It is found that the registered callback will be stored in a static variable named sCallbacks. By reflecting sCallbacks and check my code, I confirmed that up to 15 callbacks are registered in my app, so this crash may not be caused by app registering too many callbacks.

Then I read the Android source further and found class ConnectivityService, found the source of the exception:

private void enforceRequestCountLimit() {
    synchronized (mUidToNetworkRequestCount) {
        int networkRequests = mUidToNetworkRequestCount.get(mUid, 0) + 1;
        if (networkRequests >= MAX_NETWORK_REQUESTS_PER_UID) {
            throw new ServiceSpecificException(
                    ConnectivityManager.Errors.TOO_MANY_REQUESTS);
        }
        mUidToNetworkRequestCount.put(mUid, networkRequests);
    }
}

This method is inside the inner class NetworkRequestInfo in ConnectivityService. Every time a NetworkRequestInfo object is created, the count will be incremented by 1 and an exception may be thrown. Through NetworkRequestInfo's name, I think that more than ConnectivityManager.registerNetworkCallback will create NetworkRequestInfo object, But I don't know what other operations will create this object, It's hard for me to keep looking.

About TooManyRequestsException and my analysis, Can anyone help me

Upvotes: 2

Views: 1472

Answers (0)

Related Questions