1

I'm scheduling a long-running, suspendable, CPU heavy ML inference task to be run when device is on power with BGProcessingTask. According to Apple, I'm supposed to get unlimited CPU for a couple minutes, then a few seconds to stop with the time-expired callback. When I run my task manually (with the LLDB command from Apple's BGTasks documentation) everything works fine.

However, when the task runs organically in the background, it's killed after about a minute with

Terminated due to signal 9

I checked the reason from device logs and it's due to CPU utilization

Event:            cpu usage Action taken:     Process killed CPU:              48 seconds cpu time over 50 seconds (97% cpu average), exceeding limit of 80% cpu over 60 seconds

This doesn't make any sense, as the whole point of BGProcessingTask is disabling the CPU limits for a few minutes to run big tasks in the background.

See this short clip: https://developer.apple.com/videos/play/wwdc2019/707/?time=1069

CPU Monitor is a feature in our systems that automatically terminates apps that use too many CPU cycles in the background in order to protect user's battery life.For the first time ever, we're giving you the ability to turn that off for the duration of your processing task so you can take full advantage of the hardware while the device is plugged in.And finally, we'll make sure that you're eligible to run these tasks as long as you request them when your app is foreground or if your app has been recently used in the foreground.

Any idea why this is happening?

Full callstack:

https://pastebin.com/aaM264Pe

Ryan Tremblay
  • 169
  • 1
  • 3
  • 12
  • "CPU heavy" is exactly what caused the termination. You must share the CPU courteously or you will be terminated. Hard to see what the question can be... Gosh they even tell you what the acceptable limit _is_. – matt Mar 02 '21 at 01:36
  • 1
    @matt The Apple presentation explicitly stated BGProcessing tasks are useful because they're except from CPU Monitor (just like games are in the foreground). Otherwise, it would be pretty tough to run big processing tasks like ML inference on the CPU. Am I misunderstanding something? – Ryan Tremblay Mar 02 '21 at 01:46
  • Also, isn't it supposed to be the OS CPU scheduler's job to decide who gets what CPU allocation in general? – Ryan Tremblay Mar 02 '21 at 02:01
  • Well you make a good point. Could you show your code that configures the BGProcessingTask? Maybe it isn’t one after all – matt Mar 02 '21 at 02:29
  • @matt I'm actually using React Native + a number of frameworks, so that's a little tough. I'm scheduling and running BGTasks with this: https://github.com/transistorsoft/react-native-background-fetch (who are certain my issue isn't their fault here: https://github.com/transistorsoft/react-native-background-fetch/issues/323) to run tasks with my native module here: https://github.com/zaptrem/react-native-transcription – Ryan Tremblay Mar 02 '21 at 03:15
  • Well! I’m going to guess that you are a background task but not a BGProcessingTask – matt Mar 02 '21 at 03:18
  • Also background fetch is not the same as BGProcessingTask – matt Mar 02 '21 at 03:21
  • @matt That's what I originally thought, but the creator insists otherwise here: https://github.com/transistorsoft/react-native-background-fetch/issues/323#issuecomment-787507720 – Ryan Tremblay Mar 02 '21 at 03:37
  • @matt Any ideas? – Ryan Tremblay Mar 16 '21 at 22:16

0 Answers0