What does WorkManager do with jobs after hitting JobScheduler limit of 100?
Asked Answered
M

2

7

I have scoured the internet for info on this subject but nobody seems to be experiencing this issue anymore.

Remember this fun error?

java.lang.IllegalStateException: JobScheduler 100 job limit exceeded. We count 101 WorkManager jobs in JobScheduler; we have 50 tracked jobs in our DB; our Configuration limit is 50.

with

Caused by: java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs

Well, despite switching a lot of jobs to do enqueueUniqueWork, it appears some of my users are still hitting that limit. My use case is for people with poor and no connectivity doing a lot of stuff offline and thereby creating lots of jobs to update resources on the remote server. I never had this problem with FirebaseJobDispatcher (at least I wasn't aware of it) and some people may be without signal for days, and what happens if somebody can't connect for weeks? It will easily build up hundreds of jobs. Hence, this error.

My question is what happens to these jobs when the limit is reached? Are they simply discarded by the scheduler? Am I losing people's data right now as I type this?

Update

Android Versions: Confirmed on 7.0, 8.0, 8.1, probably a lot more

WorkManager Version: 2.3.0

Mummify answered 13/2, 2020 at 10:46 Comment(0)
P
1

We fixed a bug recently, where there is a mismatch between WorkManagers tracking of jobs in JobScheduler and the actual state of the jobs. This happens in very rare cases, and I think that's what is going on in your case.

https://issuetracker.google.com/issues/149092520 was a recently reported bug. I fixed it before the bug was reported on the public tracker, because one of Google's apps ran into the same edge case.

This change (https://android-review.googlesource.com/c/platform/frameworks/support/+/1226859) has the fix. We should release this as part of WorkManager 2.3.2 very soon.

If you cannot wait, you can use a snapshot build that should be identical to the eventual release.

Here is the gradle snippet you will need:

repositories {
    google()
    maven { url 'https://ci.android.com/builds/submitted/6195753/androidx_snapshot/latest/repository/' }
}

dependencies {
  implementation "androidx.work:work-runtime:2.4.0-SNAPSHOT"
}

One other thing you might need to do is to get rid of stale jobs. It is as simple as calling JobScheduler.cancelAllJobs() before WorkManager is initialized. You can do it inside Application.onCreate() in your Application subclass.

If you need more help, please feel free to reach out on the bug tracker and I can help you out.

Pyrrhuloxia answered 14/2, 2020 at 6:38 Comment(9)
Wow! I thought this was certainly my fault/lack of knowledge! Thank you sir! It would seem I keep finding all the edge cases and intermittent bugs in WorkManager. Can you possibly tell me what happens to work requests at the scheduler limit?Mummify
WorkRequests are still tracked by WorkManager. As your jobs in JobScheduler are done, and slots free up we will then fill those slots with previously unscheduled jobs. You don't lose any data.Pyrrhuloxia
Rahul you just made my weekend. Unfortunately I'm unable to access the snapshot build, despite adding the repositories in that exact fashion. Gradle doesn't know what I'm looking for.Mummify
can you please advise if there's a working snapshot build I can try?Mummify
WorkManager 2.3.2 is now available developer.android.com/jetpack/androidx/releases/work#2.3.2Pyrrhuloxia
@Mummify did the 2.3.2 solved the issue for you? I am using 2.3.4 and I am still getting the error only for few users with lot of crashes for them. @Pyrrhuloxia I didn't cancel all the works. Should I do it only once and once everything is rescheduled the crash won't happen anymore?Jewfish
@Pyrrhuloxia Sir ... Work-manager not working in background, Can you please help me on it? I have used the Periodic task which only works , when my app is in forground. It does not work in background. Please help me on itPushkin
I was running into the same issue earlier too, but that appears to have been fixed by 2.3.2. Now there is a new edge case: Fatal Exception: java.lang.IllegalStateException: JobScheduler 100 job limit exceeded. We count 100 WorkManager jobs in JobScheduler; we have 50 tracked jobs in our DB; our Configuration limit is 50. at androidx.work.impl.background.systemjob.SystemJobScheduler.scheduleInternal(SystemJobScheduler.java:204). Is it related anyhow? It’s on Android 10, moto g(9) play, in background.Overdue
Have you guys solved it? I can see this issue even on relatively new versions of WorkManagerMulder
F
1

From the exception message it seems that you just have 20 WorkRequest tracked by WorkManager. The problem maybe due to a bug in the underlying implementation of JobScheduler, would be useful to understand on which devices/OS version you have seen these errors.

Also, the 100 Jobs limits is per app, and is not limited to WorkManager usage. If in your app (or in a SDK you are including) JobScheduler is used directly, this may help reaching the 100 jobs limits.

Lastly, can you please share which WorkManager version are you using?

Fleece answered 13/2, 2020 at 11:25 Comment(2)
I updated my question to include these things. Additionally, my app doesn't use original implementation JobScheduler for anything. I've converted all async work to use WorkManagerMummify
4 years later, almost to the day. This issue has started showing up for us when migrating from the old Firebase JobDispatcher. We do not use JobScheduler directly anywhere in the code. The only binding for the service, are being done from Google services: com.google.android.datatransport.runtime.scheduling, com.google.android.gms.measurement.AppMeasurementJobService, com.google.android.gms.analytics.AnalyticsJobService, androidx.work.impl.background.systemjob.SystemJobService. This is with androidx.work:work-runtime:2.9.0Selfrealization
P
1

We fixed a bug recently, where there is a mismatch between WorkManagers tracking of jobs in JobScheduler and the actual state of the jobs. This happens in very rare cases, and I think that's what is going on in your case.

https://issuetracker.google.com/issues/149092520 was a recently reported bug. I fixed it before the bug was reported on the public tracker, because one of Google's apps ran into the same edge case.

This change (https://android-review.googlesource.com/c/platform/frameworks/support/+/1226859) has the fix. We should release this as part of WorkManager 2.3.2 very soon.

If you cannot wait, you can use a snapshot build that should be identical to the eventual release.

Here is the gradle snippet you will need:

repositories {
    google()
    maven { url 'https://ci.android.com/builds/submitted/6195753/androidx_snapshot/latest/repository/' }
}

dependencies {
  implementation "androidx.work:work-runtime:2.4.0-SNAPSHOT"
}

One other thing you might need to do is to get rid of stale jobs. It is as simple as calling JobScheduler.cancelAllJobs() before WorkManager is initialized. You can do it inside Application.onCreate() in your Application subclass.

If you need more help, please feel free to reach out on the bug tracker and I can help you out.

Pyrrhuloxia answered 14/2, 2020 at 6:38 Comment(9)
Wow! I thought this was certainly my fault/lack of knowledge! Thank you sir! It would seem I keep finding all the edge cases and intermittent bugs in WorkManager. Can you possibly tell me what happens to work requests at the scheduler limit?Mummify
WorkRequests are still tracked by WorkManager. As your jobs in JobScheduler are done, and slots free up we will then fill those slots with previously unscheduled jobs. You don't lose any data.Pyrrhuloxia
Rahul you just made my weekend. Unfortunately I'm unable to access the snapshot build, despite adding the repositories in that exact fashion. Gradle doesn't know what I'm looking for.Mummify
can you please advise if there's a working snapshot build I can try?Mummify
WorkManager 2.3.2 is now available developer.android.com/jetpack/androidx/releases/work#2.3.2Pyrrhuloxia
@Mummify did the 2.3.2 solved the issue for you? I am using 2.3.4 and I am still getting the error only for few users with lot of crashes for them. @Pyrrhuloxia I didn't cancel all the works. Should I do it only once and once everything is rescheduled the crash won't happen anymore?Jewfish
@Pyrrhuloxia Sir ... Work-manager not working in background, Can you please help me on it? I have used the Periodic task which only works , when my app is in forground. It does not work in background. Please help me on itPushkin
I was running into the same issue earlier too, but that appears to have been fixed by 2.3.2. Now there is a new edge case: Fatal Exception: java.lang.IllegalStateException: JobScheduler 100 job limit exceeded. We count 100 WorkManager jobs in JobScheduler; we have 50 tracked jobs in our DB; our Configuration limit is 50. at androidx.work.impl.background.systemjob.SystemJobScheduler.scheduleInternal(SystemJobScheduler.java:204). Is it related anyhow? It’s on Android 10, moto g(9) play, in background.Overdue
Have you guys solved it? I can see this issue even on relatively new versions of WorkManagerMulder

© 2022 - 2024 — McMap. All rights reserved.