Dariush Kaveh
Dariush Kaveh

Reputation: 33

Java.util.concurrent.RejectedExecutionException when using WorkManager

I am using work manager and kotlin coroutines

implementation "androidx.work:work-runtime-ktx:2.0.0"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0'

My worker class extend CoroutineWorker

and I schedule like this

private const val WORK_TAG = "myWork"
  private val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()

  fun schedule() {
         val work =
            OneTimeWorkRequestBuilder<MyWorker>().setConstraints(constraints).setInitialDelay(6, TimeUnit.HOURS)
                .addTag(WORK_TAG).build()
        WorkManager.getInstance().enqueue(work)
    }

In Google Play console I notice some crash on some Android version (https://ibb.co/DzBkpzy)

Fatal Exception: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 bqHint=4 (has extras) } in androidx.work.impl.constraints.trackers.NetworkStateTracker$NetworkStateBroadcastReceiver@2645398e
       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:988)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:7007)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

And

Caused by java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@d6c9311 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@1c2ffc76[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
       at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
       at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
       at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:298)
       at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:503)
       at java.util.concurrent.Executors$DelegatedScheduledExecutorService.schedule(Executors.java:644)
       at androidx.work.impl.background.systemalarm.WorkTimer.startTimer(WorkTimer.java:82)
       at androidx.work.impl.background.systemalarm.DelayMetCommandHandler.onAllConstraintsMet(DelayMetCommandHandler.java:100)
       at androidx.work.impl.constraints.WorkConstraintsTracker.onConstraintMet(WorkConstraintsTracker.java:150)
       at androidx.work.impl.constraints.controllers.ConstraintController.updateCallback(ConstraintController.java:134)
       at androidx.work.impl.constraints.controllers.ConstraintController.onConstraintChanged(ConstraintController.java:141)
       at androidx.work.impl.constraints.trackers.ConstraintTracker.setState(ConstraintTracker.java:103)
       at androidx.work.impl.constraints.trackers.NetworkStateTracker$NetworkStateBroadcastReceiver.onReceive(NetworkStateTracker.java:170)
       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:978)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:7007)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

In crash report log I don't see any line for my code. Can you help me. Thanks

Upvotes: 3

Views: 2966

Answers (3)

moyheen
moyheen

Reputation: 702

According to the official release doc, a fix was implemented in Version 2.2.0-rc01.

Fixed a bug in the AlarmManager implementation that causes the Service to shutdown prematurely and resulting in a RejectedExecutionException in rare cases

https://developer.android.com/jetpack/androidx/releases/work#2.2.0-rc01

Upvotes: 1

Rahul
Rahul

Reputation: 21104

This has been fixed in a newer version of WorkManager. You can use versions 2.0.1-rc01 or 1.0.1-rc01.

Upvotes: 6

Kiskae
Kiskae

Reputation: 25573

ScheduledThreadPoolExecutor@1c2ffc76[Terminated

The workmanager tried to schedule the task on a terminated threadpool. Since that thread pool is not controlled by you and it does not seem to be a common issue it would be extremely difficult to create an actual fix for this.

At most you could try reporting it as a bug in the androidx library, but without more information it probably can't be 'fixed'

Upvotes: 0

Related Questions