Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background monitoring causing crashes when outside region (& ScanHelper thread pools are not shutdown when ScanJob is finished) #781

Closed
fiv opened this issue Nov 21, 2018 · 4 comments

Comments

@fiv
Copy link

fiv commented Nov 21, 2018

Not sure if this should be two different issues or named differently. Thread pools are not properly shutdown for every ScanHelper (so not related to background monitoring). And not sure if the background monitoring is working as it should.

Expected behavior

App is monitoring for predefined beacon region. When app is outside the beacon region and is put to the background, monitoring should continue normally without crashes.

Actual behavior

When outside beacon region and app is put to the background, it will eventually run out of resources / OS will kill the app. One reason for this seems to be that the ScanHelpers thread pools will stay running forever. Every ScanJob will create a new ScanHelper with new thread pool (pool size seems to be CPU cores + 1). So after running the app for a few minutes in the background, it will end up having hundreds of threads, and eventually thousands.

Used settings:

beaconManager.setForegroundScanPeriod(15 * DateUtils.SECOND_IN_MILLIS);
beaconManager.setForegroundBetweenScanPeriod(3 * 60 * DateUtils.SECOND_IN_MILLIS);
beaconManager.setBackgroundScanPeriod(15 * DateUtils.SECOND_IN_MILLIS);
beaconManager.setBackgroundBetweenScanPeriod(21 * 60 * DateUtils.SECOND_IN_MILLIS);

On background and outside the beacon region, ScanJob runs for 15 seconds and then expires. After that ScanJob will call startAndroidOBackgroundScan (scheduleNextScan->startPassiveScanifNeeded->startAndroidOBackgroundScan-StartupBroadcastReceiver->ScanJobScheduler). So basically new ScanJob & new ScanHelper with new threadpool (with my Samsung SM-G935F, pool size is 9 threads) is created every 15 seconds.

Here is log from the app https://gist.github.com/fiv/91aaa7e4405b4572bbef1f12fe2943cb (HUGE)

Profiler screenshot showing increasing amount of threadpools:
screenshot 2018-11-21 at 9 35 57

Steps to reproduce this behavior

I managed to reproduce this using the reference app with the following BeaconManager settings: https://gist.github.com/fiv/20a63d7e7eda820ae8075c20a9840e7a

Mobile device model and OS version

Samsung SM-G935F
Android 8.0.0

Android Beacon Library version

2.15.2 and also master branch (commit 7df874d)

@davidgyoung
Copy link
Member

Thanks for this detailed analysis, @fiv. I suspect this is the root cause of this issue: #647

Based on the docs here it seems there simply needs to be a call to mExecutor.shutdown(); before the ScanHelper is disposed.

This could be put in a finalize() method on the class, or probably better, in a new custom disable() method that gets explicitly called by ScanJob or BeaconService before they exit.

@davidgyoung
Copy link
Member

davidgyoung commented Nov 24, 2018

@fiv, I've coded a fix in #785 and made an experimental release including that fix here:
https://github.com/AltBeacon/android-beacon-library/releases/tag/2.15.3-beta1

Would you be willing to re-run your test setup build this build to see if you can confirm this resolves the issue?

@fiv
Copy link
Author

fiv commented Nov 28, 2018

@davidgyoung Tested with the experimental release 2.15.3-beta1. My app has been running for ~20 hours and seems to be working correctly, so no thread leaks anymore.

@davidgyoung
Copy link
Member

Great, thanks for the assistance @fiv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants