1

There are quite a few questions considering infinite loop of android's SyncAdapter: [1] [2] [3], but none described the problem I encountered.


I am setting up my sync as:

ContentResolver.setIsSyncable(account, AppConstants.AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, AppConstants.AUTHORITY, true);
ContentResolver.addPeriodicSync(account, AppConstants.AUTHORITY, Bundle.EMPTY, 60);

My sync adapter supports uploading (android:supportsUploading="true"), which means that in my ContentProvider I have to check whether the data change comes from my SyncAdapter, and if it does, then I notify change without requesting sync to network.

boolean syncToNetwork = false;
getContext().getContentResolver().notifyChange(uri, null, syncToNetwork);

Still my sync adapter runs in a constant loop, what another reason could there be for triggering another sync?

Community
  • 1
  • 1
Kuba Spatny
  • 26,618
  • 9
  • 40
  • 63

1 Answers1

0

In each sync I request the server for data. For each request I get an access token from my custom Account Authenticator. Instead of saving a password in my account, I decided to save the Oauth2 refresh token, which can then be use to refresh the access token. With each refreshed access token the server also send a new refresh token, which I then update to my account:

accountManager.setPassword(account, refreshToken);

And THAT was the problem. Going through the AOSP codes I discovered the following BroadcastReceiver in the SyncManager:

private BroadcastReceiver mAccountsUpdatedReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
          updateRunningAccounts();

          // Kick off sync for everyone, since this was a radical account change
          scheduleSync(null, UserHandle.USER_ALL, null, null, 0 /* no delay */, false);
     }
};

So what it does, on each account change (adding, deleting, setting password) a broadcast in send to trigger sync for all SyncAdapters, not just your own!

I honestly don't know what what the reasoning for that, but I can see it as exploitable - I let my phone (with my app stuck in infinite loop) run over night, in the morning the battery was drained, but also my FUP - only the Google's Docs, Slides and Sheets apps consumed 143MB each.

Kuba Spatny
  • 26,618
  • 9
  • 40
  • 63