Android WorkManager *occasionally* stops scheduling jobs after app crash

Viewed 454

I use a WorkManager to schedule periodic background work in my app. It has been working very well for months. Even if the app crashes, it will start the app back up after some time.

But just recently, I noticed that there are times when the WorkManager would stop scheduling new work after the app crashed, but this happens only occasionally. This is troublesome because the behavior is not reliably reproducible, even on the same phone.

(I know the behavior of WorkManager after an app is closed depends on the phone manufacturer, but this is all happening on the exact same phone.)

What I did was simulate a crash by throwing a new RunTimeException somewhere while a Worker is doing work. And then I check the status of the job scheduler:

adb shell dumpsys jobscheduler | grep JOB.*my_app

Sometimes, I will see an entry for the app after the crash, and sometimes, there will be no entry. I've confirmed that if there is an entry in the scheduler, my app will start back up after some time after the crash; and if there is no entry, my app will never start back up again.

Is there some reason why the WorkManager would stop scheduling work after a crash, and do it only sometimes?

Update Upon further investigation, I see that sometimes when the app crashes, I get:

I/ActivityManager: Process [my_app] (pid 32763) has died: fore  +50 TOP 
W/ActivityManager: Scheduling restart of crashed service ...
W/ActivityManager: Scheduling restart of crashed service ...
...
I/ActivityManager: Start proc 644:my_app/u0a232 for service {...}

When this happens, JobScheduler will show an entry for the app and it restarts later.

Sometimes, however, I get:

I/ActivityManager: Process [my_app] (pid 32763) has died: fore  +50 TOP 
W/ActivityManager: Scheduling restart of crashed service ...
W/ActivityManager: Scheduling restart of crashed service ...
...
I/ActivityManager: Force stopping my_app appid=10232 user=0: from pid 31293
I/ActivityManager:   Force stopping service ServiceRecord{...}
I/ActivityManager:   Force stopping service ServiceRecord{...}

In these cases, the JobScheduler entries for my app get emptied out and my app won't restart.

However, the causes the crash are the same in both cases (I simulate it by intentionally throwing an exception), so I don't know why sometimes the system force-stops my app, and sometimes restarts it.

0 Answers
Related