Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent background service from being killed due to "detect excessive cpu on forked process"

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?

like image 846
Martin C. Avatar asked Jul 06 '16 19:07

Martin C.


1 Answers

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.

like image 50
Nicu Surdu Avatar answered Sep 23 '22 17:09

Nicu Surdu