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

[PLAT-7702] Fix currentAppState when called before UIApplicationMain #1248

Merged
merged 1 commit into from Dec 7, 2021

Conversation

nickdowell
Copy link
Contributor

Goal

Fix detection of foreground / background state when app is prewarmed.

In iOS 15 and later, the system may, depending on device conditions, prewarm your app — launch nonrunning application processes to reduce the amount of time the user waits before the app is usable. Prewarming executes an app’s launch sequence up until, but not including, when main() calls UIApplicationMain. This provides the system with an opportunity to build and cache any low-level structures it requires in anticipation of a full launch.

If Bugsnag is started before UIAppliationMain() has been called (e.g. from a SwiftUI app's init() function) UIApplication.sharedApplication will be nil.

When the applicationState message is send to nil, a value of 0 (i.e. active) is returned and causes Bugsnag to think the app is running in the foreground, triggering automatic session tracking and resulting in OOMs to be detected if the process gets terminated before the app is launched by the user.

Changeset

Return UIApplicationStateBackground if sharedApplication is nil.

Testing

To cause an app to be prewarmed, install and run on a device and then reboot it. The OS typically runs it soon after starting up again - within a minute on a device with few apps.

Manually verified with a SwiftUI sample app that a session is still started automatically at the correct time (when the relevant notification is sent) but not before the app is opened.

Did not find a way to replicate the false OOMs, because our OOM detection logic checks for and ignores reboots.

Amended unit tests that relied on applicationState to be active.

@github-actions
Copy link

github-actions bot commented Dec 6, 2021

Infer: No issues found 🎉

OCLint: No issues found 🎉

Bugsnag.framework binary size increased by 272 bytes from 1,252,984 to 1,253,256

Generated by 🚫 Danger

@nickdowell nickdowell merged commit 185098b into next Dec 7, 2021
@nickdowell nickdowell deleted the nickdowell/fix-early-application-state branch December 7, 2021 09:28
This was referenced Dec 8, 2021
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

Successfully merging this pull request may close these issues.

None yet

3 participants