Prevent ANRs when backgrounding app #1775
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
One Line Summary
Prevent ANRs when app is backgrounded due to some disk I/O with session logic and AndroidX Worker usage.
Details
Motivation
The time spent on the main thread in this scenario is significantly contributing to ANRs. The Google Play store penalizes apps with ANRs with lower search rankings and possibly some other negatives.
Scope
Simply move code to a new background thread. No logic changes. Longer term in the 5.0.0 release we can use a thread pool or Kotlin suspend instead.
Full Details
Move calls to methods that do disk I/O from the main thread to a new background thread.
There are reports where this can cause a significant amount of time on main thread. Any disk I/O is a risk on the main thread and should not be done as there is no upper bond of how long it can take. ANRs trigger after 5 seconds of the main thread being busy.
The stacktrace of reported ANRs confirm that appStopped then calls getSessionInfluences which can result in waiting on disk I/O. We have also seen stacktraces with scheduling a AndroidX worker so that was moved into this background thread as well. I isn't known if it does any disk I/O but it was significant enough to show up in some ANRs.
Testing
Unit testing
No unit testing changes.
Manual testing
Confirmed code still fires when the app is backgrounded on an Android 13 emulator.
Affected code checklist
Checklist
Overview
Testing
Final pass
This change is