pwittchen / ReactiveNetwork

Android library listening network connection state and Internet connectivity with RxJava Observables
http://pwittchen.github.io/ReactiveNetwork/docs/RxJava2.x/
Apache License 2.0
2.53k stars 276 forks source link

java.lang.IllegalArgumentException: Too many NetworkRequests filed in Android Nougat(7.1.1). #461

Open jonesn123 opened 2 years ago

jonesn123 commented 2 years ago

Describe the bug Occur crash when I call the function checkInternetConnectivity(). This is one time issue.

To Reproduce Steps to reproduce the behavior:

  1. call below function fun checkInternetConnectivity(context: Context): Observable = ReactiveNetwork.observeNetworkConnectivity(context) .flatMapSingle { connectivity -> @Suppress("DEPRECATION") if (connectivity.state() === NetworkInfo.State.CONNECTED) { return@flatMapSingle ReactiveNetwork.checkInternetConnectivity() } Single.fromCallable { false } } .take(1) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())

  2. Log cat error

08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: java.lang.IllegalArgumentException: Too many NetworkRequests filed 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.os.Parcel.readException(Parcel.java:1688) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.os.Parcel.readException(Parcel.java:1637) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.net.IConnectivityManager$Stub$Proxy.listenForNetwork(IConnectivityManager.java:2400) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:2856) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:3075) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.github.pwittchen.reactivenetwork.library.rx2.network.observing.strategy.MarshmallowNetworkObservingStrategy.observeNetworkConnectivity(MarshmallowNetworkObservingStrategy.java:80) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork.observeNetworkConnectivity(ReactiveNetwork.java:92) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork.observeNetworkConnectivity(ReactiveNetwork.java:73) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.util.CommonUtils.checkInternetConnectivity(CommonUtils.kt:1) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.AgentService.checkInternetAndAction(AgentService.kt:1) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.AgentService.checkInternetAndAction$default(AgentService.kt:1) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.AgentService.requestRecognition(AgentService.kt:2) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.AgentService.access$requestRecognition(AgentService.kt:1) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.AgentService$f.a(AgentService.kt:2) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.AgentService$f.invoke(AgentService.kt:1) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.floating.a$a.a(FloatingAgentViewManager.kt:2) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.floating.FloatingAgentView$b.onSingleTapUp(FloatingAgentView.kt:1) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.GestureDetector.onTouchEvent(GestureDetector.java:635) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.kakao.i.sdk.agent.floating.FloatingAgentView.onTouch(FloatingAgentView.kt:1) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.View.dispatchTouchEvent(View.java:10019) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2626) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2307) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.View.dispatchPointerEvent(View.java:10243) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6220) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6349) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.os.MessageQueue.nativePollOnce(Native Method) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.os.MessageQueue.next(MessageQueue.java:323) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.os.Looper.loop(Looper.java:136) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at android.app.ActivityThread.main(ActivityThread.java:6119) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at java.lang.reflect.Method.invoke(Native Method) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982) 08-04 14:00:20.685 5771 5771 E MessageQueue-JNI: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872) 08-04 14:00:20.691 5771 5771 D AndroidRuntime: Shutting down VM --------- beginning of crash

Smartphone (please complete the following information):

Additional context I think this issue because of only nougat OS like below. https://stackoverflow.com/questions/41957895/android-nougat-too-many-networkrequests-filed

But I want to protect the crash. So could you please catch this exception in library?

Thanks.

pwittchen commented 2 years ago

Hi,

Thanks for reporting this issue. It seems to be related to the specific version of the Android OS. It looks like Android is blocking situation, when there is too many network requests. I also don't know how many is "too many" Catching this exception won't solve the problem. I think, in this case you should use observe network only or limit somehow these network checks. Also ensure if you are disposing this disposable in the activity/service lifecycle. If it's not disposed, problems like that may occur very often. I will read about this problem and verify if it's possible to handle that properly within the library.

Regards, Piotr

jonesn123 commented 2 years ago

Hi @pwittchen

Thank you so much your message. I'm going to fix my function using firstOrError() as you mention.

fun checkInternetConnectivity(context: Context): Single<Boolean> =
ReactiveNetwork.observeNetworkConnectivity(context)
.flatMapSingle { connectivity ->
@Suppress("DEPRECATION")
if (connectivity.state() === NetworkInfo.State.CONNECTED) {
return@flatMapSingle ReactiveNetwork.checkInternetConnectivity()
}
Single.fromCallable { false }
}
.firstOrError() <-- fixed this line
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())

If this issue occur again. I'll let you know.

Thanks.

jonesn123 commented 2 years ago

Hi @pwittchen

I fixed above, so this crash count was decreased. But, still was occurred same crash in Nougat os.

08-06 12:27:58.221  5830  5830 E MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
08-06 12:27:58.250  5830  5830 E MessageQueue-JNI: java.lang.IllegalArgumentException: Too many NetworkRequests filed
08-06 12:27:58.250  5830  5830 E MessageQueue-JNI:  at android.os.Parcel.readException(Parcel.java:1688)
08-06 12:27:58.250  5830  5830 E MessageQueue-JNI:  at android.os.Parcel.readException(Parcel.java:1637)
08-06 12:27:58.250  5830  5830 E MessageQueue-JNI:  at android.net.IConnectivityManager$Stub$Proxy.listenForNetwork(IConnectivityManager.java:2400)
08-06 12:27:58.250  5830  5830 E MessageQueue-JNI:  at android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:2856)
08-06 12:27:58.250  5830  5830 E MessageQueue-JNI:  at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:3075)
08-06 12:27:58.250  5830  5830 E MessageQueue-JNI:  at com.github.pwittchen.reactivenetwork.library.rx2.network.observing.strategy.MarshmallowNetworkObservingStrategy.observeNetworkConnectivity(MarshmallowNetworkObservingStrategy.java:80)

So, If possible to handle that properly within the library to next version, please let me know. Thanks.

pwittchen commented 2 years ago

Thanks for the update.

I will try to look into this problem, but at this moment I'm not really sure if it's related to the library code or the Android OS. From your log I see, crash is at this line:

https://github.com/pwittchen/ReactiveNetwork/blob/RxJava2.x/library/src/main/java/com/github/pwittchen/reactivenetwork/library/rx2/network/observing/strategy/MarshmallowNetworkObservingStrategy.java#L80

when registering network callback (manager.registerNetworkCallback(request, networkCallback);), so maybe we need to introduce a state, which will keep an information whether this callback was already registered or not.

I will try to check it later.

Regards, Piotr

ExpensiveBelly commented 2 years ago

For those running into this issue and have minimum SDK 24, the fix is to simply do this, which is what we ended up doing, no need for this library:

@Singleton
class NetworkConnectivityMonitor
@Inject
constructor(context: Context) : INetworkConnectivityMonitor, LifecycleObserver {

    private val appStateSubject = BehaviorSubject.create<AppState>()

    private val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

    private val isConnectedSubject = BehaviorSubject.create<Boolean>()
    override val isConnectedObservable = isConnectedSubject.hide()

    private val callback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            isConnectedSubject.onNext(true)
        }

        override fun onLost(network: Network) {
            isConnectedSubject.onNext(false)
        }

        override fun onLosing(network: Network, maxMsToLive: Int) {
            isConnectedSubject.onNext(false)
        }

        override fun onUnavailable() {
            isConnectedSubject.onNext(false)
        }

        override fun onBlockedStatusChanged(network: Network, blocked: Boolean) {
            isConnectedSubject.onNext(!blocked)
        }
    }

    init {
        appStateSubject.subscribe { appState ->
            when (appState) {
                AppState.FOREGROUND -> connectivityManager.registerDefaultNetworkCallback(callback)
                AppState.BACKGROUND -> connectivityManager.unregisterNetworkCallback(callback)
            }
        }
    }

    init {
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onForeground() {
        appStateSubject.onNext(AppState.FOREGROUND)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onBackground() {
        appStateSubject.onNext(AppState.BACKGROUND)
    }
}

enum class AppState {
    FOREGROUND,
    BACKGROUND
}

interface INetworkConnectivityMonitor {
    val isConnectedObservable: Observable<Boolean>
}

This will listen for connectivity changes when the app is in foreground.

At this point all you need to do if you want to listen for connectivity changes you just subscribe to isConnectedObservable. You can have as many subscribers as you want, it uses a Subject under the scenes so we only registerDefaultNetworkCallback once, regardless of the amount of subscribers.

Hope it's useful.

rpadma commented 2 years ago

@pwittchen - I'm running into a similar issue - adding the logs from the library if that helps. Version - 0.13.0

8-19-2021 12:05:37.012-0400 (thread main 2) E | NoOpCrashReporter: Uncaught exception on thread main (2)
android.net.ConnectivityManager$TooManyRequestsException
    at android.net.ConnectivityManager.convertServiceException(ConnectivityManager.java:3977)
    at android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:4205)
    at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:4594)
    at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:4564)
    at com.github.pwittchen.reactivenetwork.library.network.observing.strategy.MarshmallowNetworkObservingStrategy.observeNetworkConnectivity(MarshmallowNetworkObservingStrategy.java:59)
    at com.github.pwittchen.reactivenetwork.library.ReactiveNetwork.observeNetworkConnectivity(ReactiveNetwork.java:97)
    at com.github.pwittchen.reactivenetwork.library.ReactiveNetwork.observeNetworkConnectivity(ReactiveNetwork.java:77)
Blafasel3 commented 2 years ago

Running into the same issue an Android wit the following SDK versions: minSdkVersion 23 targetSdkVersion 28

Sadly no way to figure out in which scenario this happens.