-
Notifications
You must be signed in to change notification settings - Fork 23
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
Polling alarms can repeat in background indefinitely if app crashes #188
Comments
Thanks for reporting this. The description is clear, and we'll be looking into it. (filed internally as 169505) |
Using AlarmManager is definitely not ideal. It's possible that the reason we've been using that, rather than WorkManager (which seems to me like a better fit for this), is simply that the core Android SDK code is pretty old and we hadn't revisited that functionality since WorkManager became available. I do think we have made an effort to avoid relying on things that come from Jetpack rather than the core API, on the general principle of minimizing dependencies that an app might not want or that might conflict with the version the app is using, but that might not be so bad if WorkManager is not very big and if we're only using basic APIs from it that have been stable. |
I do wonder, though, if |
The possibility that this was all over-engineered from the start, and could be replaced by simpler Java scheduling mechanisms, is certainly something we need to consider. Since again the core SDK code is quite old and has been a bit neglected as development focused on other feature enhancements, it could be that we didn't completely think this through the first time round. I'm not aware of any OS limitations that would prevent a task from doing network requests in the background if it were just running on an ordinary worker thread, but maybe I'm missing something. |
Well I think the |
Is that really the case, though? That was my general impression, but... for instance, the Android "Guide to background work" describes three strategies for executing code while the app is in the background: WorkManager, AlarmManager, and just using an ordinary Java worker thread or Kotlin coroutine. And then it breaks things down further in terms of whether you need the scheduling to persist across app restarts and device restarts or not, and says that if you do, then you need to use WorkManager or AlarmManager (with WorkManager always being preferable unless you actually need to wake the whole device up from sleep). But it does seem to be saying that if you don't need it to persist across app/device restarts, then ordinary worker thread scheduling is OK. At least, I don't know how else to interpret the fact that they're discussing that as an option, on a page that is entirely about background work. |
Well this all comes down to what you mean by "background" here and they are sorting lumping a couple of things together:
You can use worker threads for "background" work in the first case, but you need something like This is all very different for normal polling in the foreground, though. By definition the app process is already running and visible to the user, so normal worker threads would be sufficient there. |
I don't think the intention with "background polling" in the SDK is to keep doing it when the app is not running at all. If the app gets started again, the SDK is going to get started again and will acquire flags again at that time; having woken the app up before that point to do background polls wouldn't have made the flags any fresher. The SDK doesn't do anything with flag values unless the application actually requests those values. The point of background polling would be to make not-entirely-stale flag values available to the application in case the application is actually doing things while it's in the background. And, the intended normal usage of the SDK is that the application explicitly shuts it down if the application knows it is quitting, so the only time polling would persist would be after a crash, which seems even less useful. The reason I'm having to guess about the original rationale, rather than being able to say "we originally did it this way because ____", is that again this core logic is from very early iterations of the SDK project, when it was basically at proof of concept stage and LD did not yet have a large mobile user base, by developers who were not part of the project later on. I think it's entirely possible that some of the decisions they made weren't as fully thought through as they would be if we created the SDK today, and weren't revisited during later rewrites; either way, they weren't well documented. We've been doing a larger rewrite of the SDK as part of an upcoming feature release, and trying to ensure that we're really doing things in optimal ways. By the way, when you say "from the looks of the code and documentation"— is there a particular piece of documentation you can point out that was giving you that impression? I'd like to make sure that what we're saying in docs is really in line with the actual behavior of the SDK. |
@eli-darkly I suppose the documentation is a bit ambiguous because it just says "background" without specifically defining it (similar to how the Google docs use it in two different ways): /**
* Sets how often the client will poll for flag updates when your application is in the background.
* <p>
* The default value is {@link LDConfig#DEFAULT_BACKGROUND_POLLING_INTERVAL_MILLIS}.
*
* @param backgroundPollingIntervalMillis the feature flag polling interval when in the background,
* in milliseconds
* @return the builder
*/
public LDConfig.Builder backgroundPollingIntervalMillis(int backgroundPollingIntervalMillis) {
this.backgroundPollingIntervalMillis = backgroundPollingIntervalMillis;
return this;
} I think looking at the code, though, that fact that If that is not the intention of background polling and that was only meant for when the app was actually running, then there is an additional bug here that needs fixing as well: the |
It is cancelled here... assuming, again, an orderly shutdown where the app has a chance to call |
@eli-darkly So I know that alarm is cancelled when moving into the background, but I don't know of any other "orderly shutdown" hooks once its already in the background. So I'm not sure when the background alarm would ever be cancelled. So yes, this would produce a bug like the one I originally mentioned. But in this case it's far more predictable: for my problem, background polling is disabled and there is an edge case that leads foreground polling to continue in the background, but if background polling is not disabled this new bug should happen almost 100% of the time. |
I'm not sure what you mean by "orderly shutdown hooks". I used the phrase orderly shutdown in this context: "an orderly shutdown where the app has a chance to call It could be that the behavior you're talking about does happen "almost 100% of the time", but I'm somewhat skeptical that no other customer would have noticed it before if so. |
In any case, I'm not sure how productive it is to debate that point since we have already established 1. we should fix it to cancel any previously-created AlarmManager alarms after an app restart if we continue to use AlarmManager, and 2. it may be that we should not continue to use AlarmManager. The general issue of "creating an AlarmManager alarm has the potential to leave it still firing if the app exited unexpectedly" is undeniable since by definition, if an app exits unexpectedly then it did not have the chance to do any kind of cleanup. |
Sure, I was just hoping to shed some light on the idea that this issue may actually be bigger than the narrow case I originally posted and could potentially encompass the current behavior of "background" polling as well. So that is worth looking into for its own purposes. In turns of no one else reporting the issue, it's possible they either thought like I did that "background" polling was actually intended to wake the app up or simply didn't notice because its not something most teams need to check. |
We've released version 3.2.1 to address this. As described in the release notes, we're not dropping the use of AlarmManager yet in this patch, so it is still possible for an app to get woken up by an obsolete polling alarm after a crash; the current fix is that, in such a case, the SDK will immediately cancel that obsolete alarm. So, in the scenario you described where the SDK is configured offline at startup, it will not be setting a new alarm at that point and Android will not repeatedly wake up the app. Please let us know if this patch works for you when you've had a chance to test with it. |
Thanks for the update @eli-darkly! I will try to test this in the next day or so and let you know. |
@eli-darkly I've done some testing and I can confirm it fixes the issue related to the foreground polling period being used in the background. Thanks for your help! I will go ahead and close this issue now. |
On the larger issue of whether we should even be using AlarmManager (or WorkManager) at all, I am still leaning toward thinking we should not, and preliminary work so far has seemed to support the idea that a simpler worker thread/executor mechanism would work fine for both foreground and background polling. It wasn't ever our intention for applications to be restarted for a poll after being closed; that's why the SDK explicitly cancels its alarms as long as it knows it is quitting, but of course there is no guarantee of an orderly shutdown. We just need to make sure that there aren't any unforeseen consequences of changing the implementation, so that's why we did only a smaller fix in this release. |
Yeah that makes sense. I would definitely worry a little about the current functionality being something some consumers might be relying on (intentionally or otherwise). I know there was a time where we had background polling enabled specifically because we wanted the app to fetch updated flags before the user would typically open the app. That way we would have reasonably fresh flag values right on app startup even before the SDK's initial sync would complete. This was important because many users don't use the app very regularly and we need to check some flag values right on app startup. |
Is this a support request?
No
Describe the bug
Under certain conditions (described below) the alarm set for foreground polling is not cancelled when the app is closed and this can result in that polling period being used to wake the app up in the background.
To reproduce
There is a particular setup required here:
offline(true)
) and not set to "online" mode until there is foreground UI. (This is done to prevent the possibility of any network activity in the background).disableBackgroundUpdating(true)
.stream(false)
.pollingIntervalMillis
is set to a low value (like the minimum of 5 minutes).When the app is first foregrounded, the
PollingUpdater
will register a recurring alarm using thepollingIntervalMillis
(which in this case is 5 minutes). Under normal circumstances, when the app moves to the background this alarm will be cancelled (as expected). This is because theForegroundChecker
will triggeronBecameBackground
insideConnectivityManager
:The
attemptTransition(backgroundMode)
will then disable all polling since background polling is disabled:However, none of this code runs if the app shuts down in the foreground unexpectedly for any reason (such as an app crash). This leaves the 5 minute alarm in place and it wakes the app back up in the background. However, because the process is running in the background and is initialized in "offline" mode, the alarm also is not cancelled and is therefore allowed to repeat. Alarms are always typically cancelled when the SDK is initialized when
ConnectivityManager.startUp
is called, but this logic is not run in offline mode:Expected behavior
Polling alarms are always cancelled and reset each time the SDK is initialized (even in offline mode).
More generally, if alarms were not used to implement foreground polling then it would not be possible for foreground polling to accidentally trigger an app to wake up in the background in the first place.
Logs
N/A
SDK version
3.1.4 (and also confirmed with current version 3.2.0)
Language version, developer tools
Kotlin / Java
OS/platform
Android 13
Additional context
N/A
The text was updated successfully, but these errors were encountered: