11

I am debugging an issue of the Syncthing Android wrapper. The Android application wraps a native binary of the Syncthing project and provides some additional functionality like starting/stopping based on connecting WiFi, power, etc.

Unfortunately, there have been issues about the service not auto-starting on WiFi change anymore, especially with handsets that have been upgraded to Android 6. Since my personal handset has been upgraded to 6.0.1 recently, I finally am able to debug into the issue and today I noticed the following:

07-06 20:52:26.562  1181  1363 I ActivityManager: [BgDetect]chkExcessCpu doKills: true uptime: 300309
07-06 20:52:26.960  1181  1363 I ActivityManager: [BgDetect]detect excessive cpu on forked process libsyncthing.so(pid : 2758) plan to stop it 371060 during 300309
07-06 20:52:26.960  1181  1363 I ActivityManager: [BgDetect]force stop com.nutomic.syncthingandroid.debug (uid 10132)
07-06 20:52:26.965  1181  1363 I ActivityManager: Force stopping com.nutomic.syncthingandroid.debug appid=10132 user=0: from pid 1181
07-06 20:52:26.966  1181  1363 I ActivityManager: Killing 1360:com.nutomic.syncthingandroid.debug/u0a132 (adj 15): stop com.nutomic.syncthingandroid.debug
07-06 20:52:26.968  1181  1363 W ActivityManager: Scheduling restart of crashed service com.nutomic.syncthingandroid.debug/com.nutomic.syncthingandroid.syncthing.SyncthingService in 1000ms
07-06 20:52:26.968  1181  1354 V ActivityManager: killProcessGroupAsync took 1 ms for PID 1360 on thread 14
07-06 20:52:26.971  1181  6410 I libprocessgroup: Killing pid 2758 in uid 10132 as part of process group 1360
07-06 20:52:26.973  1181  6410 I libprocessgroup: Killing pid 2758 in uid 10132 as part of process group 1360
07-06 20:52:26.974  1181  1363 I ActivityManager:   Force stopping service ServiceRecord{47304c8 u0 com.nutomic.syncthingandroid.debug/com.nutomic.syncthingandroid.syncthing.SyncthingService}

The Syncthing protocol is quite heavy on the CPU side, regarding encryption, hashing, etc. So it seems that the service is force-stopped due to the high CPU demands of the forked native binary.

The application is already marked as "No battery optimization", but still it is being killed. Is there a way to prevent the ActivityManager from killing the application due to the CPU demands of the forked process?

tom redfern
  • 30,562
  • 14
  • 91
  • 126
Martin C.
  • 12,140
  • 7
  • 40
  • 52
  • 1
    Hmm, a grep for `BgDetect` over the 6.0 branch of AOSP didn't return anything, so it _might_ be an addition of OxygenOS. – Martin C. Jul 06 '16 at 22:52
  • It seems, promoting the service to "foreground" with `startForeground()` mitigates the problems. But I consider this not a optimal solution. – Martin C. Jul 08 '16 at 20:25
  • 1
    Adding startForeground() does not solve this issue unfortunately – phcaze May 19 '17 at 19:35
  • 1
    still killed by system even using startForeground() – felangga Jul 31 '18 at 07:05
  • The solution / explanation is here https://stackoverflow.com/questions/53612307/foreground-notification-service-not-working-in-one-plus-devices/56195214#56195214 – rmindzstar May 18 '19 at 09:39

2 Answers2

3

Unfortunately, this is a OnePlus / OxygenOS feature that will kill any background process based on who-knows what rules in order to give the appearance of a better battery life.

For now, I found a non-code workaround in a GitHub issue that seems to help, but is far from ideal: pin/lock the application on the recents screen.

Nicu Surdu
  • 8,172
  • 9
  • 68
  • 108
0

Was testing my foreground service and realized it was a performance issue mainly CPU. So I enforced performance with the profiler and added to the white list.

Reduced the consumption cpu, ram and battery and its done.

Foreground notification service not working in one plus devices

rmindzstar
  • 338
  • 2
  • 11