JobScheduler posting Jobs twice (not expected)

跟風遠走 提交于 2019-11-29 06:16:05

Thanks for this - I worked on the JobScheduler. Based on your app (thanks!) I managed to repro this pretty easily and track down the cause of the bug.

tl;dr, This is a case which will not happen very often outside of a tutorial app. To work around it in your tutorial, increase the deadline on your job to larger than the amount of time each of your background threads run.

What is happening is that you schedule your jobs in succession, and the JobScheduler runs them pretty much immediately as they are scheduled. However, one second later (the one second is the part that will not happen for a "real" app) the override deadline alarm fires, and the jobscheduler decides very aggressively that any job whose deadline has expired needs to be run again (the API contract states that "deadline expiry" trumps all other considerations), so it puts it into the pending queue. As soon as the executing job is finished, the pending queue is checked, and there's a job there, so it's run.

So, the job will fire 2x if the deadline expires while the job is running. Ensure that the deadline expires either before the job runs (which results in the job running) or after (alarm won't actually land b/c the job's already finished), and everything works as intended.

I've fixed this in Android N (unfortunately M has already shipped), and added a CTS test to ensure it stays fixed. Thanks for bringing it to our attention

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!