From a77f73ea71b6183027284b0d20e9bbef56e627f9 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 12 Jan 2020 19:28:19 +0100 Subject: [PATCH] Prevent race condition on quit --- .../eu/faircode/email/ServiceSynchronize.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index c91ece2687..80555ca727 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -90,6 +90,7 @@ import me.leolin.shortcutbadger.ShortcutBadger; import static android.os.Process.THREAD_PRIORITY_BACKGROUND; public class ServiceSynchronize extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener { + private Integer lastStartId = null; private Boolean lastSuitable = null; private long lastLost = 0; private int lastAccounts = 0; @@ -179,7 +180,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences for (TupleAccountNetworkState prev : accountStates) stop(prev); - quit(); + quit(null); accountStates.clear(); coreStates.clear(); @@ -261,7 +262,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build()); } } else - quit(); + quit(lastStartId); } } @@ -343,7 +344,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences }); } - private void quit() { + private void quit(final Integer startId) { EntityLog.log(ServiceSynchronize.this, "Service quit"); queue.submit(new Runnable() { @@ -357,13 +358,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences Log.w(ex); } - DB db = DB.getInstance(ServiceSynchronize.this); - List ops = db.operation().getOperations(EntityOperation.SYNC); - for (EntityOperation op : ops) - db.folder().setFolderSyncState(op.folder, null); + if (startId != null) { + stopSelf(startId); + + if (startId.equals(lastStartId)) { + DB db = DB.getInstance(ServiceSynchronize.this); + List ops = db.operation().getOperations(EntityOperation.SYNC); + for (EntityOperation op : ops) + db.folder().setFolderSyncState(op.folder, null); + } - stopSelf(); - EntityLog.log(ServiceSynchronize.this, "### quited"); + EntityLog.log(ServiceSynchronize.this, "### quit requested"); + } } }); } @@ -577,6 +583,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences @Override public int onStartCommand(Intent intent, int flags, int startId) { + lastStartId = startId; String action = (intent == null ? null : intent.getAction()); String reason = (intent == null ? null : intent.getStringExtra("reason")); EntityLog.log(ServiceSynchronize.this, "### Service command " + intent +