diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 3bb098b58b..6faf4b50b1 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -1189,18 +1189,19 @@ public class ServiceSynchronize extends LifecycleService { PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":folder." + folder.id); @Override - public void onChanged(final List operations) { - boolean process = false; - List current = new ArrayList<>(); - for (EntityOperation op : operations) { - if (!handling.contains(op.id)) - process = true; - current.add(op.id); + public void onChanged(final List ops) { + final List arrived = new ArrayList<>(); + synchronized (handling) { + for (EntityOperation op : ops) { + if (!handling.contains(op.id)) { + handling.add(op.id); + arrived.add(op); + } + } } - handling = current; - if (handling.size() > 0 && process) { - Log.i(folder.name + " operations=" + operations.size()); + if (arrived.size() > 0) { + Log.i(folder.name + " process operations=" + arrived.size()); (folder.poll ? pollExecutor : folderExecutor).submit(new Runnable() { @Override public void run() { @@ -1229,8 +1230,12 @@ public class ServiceSynchronize extends LifecycleService { db.folder().setFolderError(folder.id, null); } - processOperations(account, folder, operations, isession, istore, ifolder, state); + processOperations(account, folder, arrived, isession, istore, ifolder, state); + synchronized (handling) { + for (EntityOperation op : arrived) + handling.remove(op.id); + } } catch (Throwable ex) { Log.e(folder.name, ex); reportError(account, folder, ex); @@ -3003,18 +3008,19 @@ public class ServiceSynchronize extends LifecycleService { PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":outbox"); @Override - public void onChanged(final List operations) { - boolean process = false; - List current = new ArrayList<>(); - for (EntityOperation op : operations) { - if (!handling.contains(op.id)) - process = true; - current.add(op.id); + public void onChanged(final List ops) { + final List arrived = new ArrayList<>(); + synchronized (handling) { + for (EntityOperation op : ops) { + if (!handling.contains(op.id)) { + handling.add(op.id); + arrived.add(op); + } + } } - handling = current; - if (handling.size() > 0 && process) { - Log.i(outbox.name + " operations=" + operations.size()); + if (arrived.size() > 0) { + Log.i(outbox.name + " process operations=" + arrived.size()); executor.submit(new Runnable() { @Override public void run() { @@ -3023,8 +3029,13 @@ public class ServiceSynchronize extends LifecycleService { Log.i(outbox.name + " process"); db.folder().setFolderSyncState(outbox.id, "syncing"); - processOperations(null, outbox, operations, null, null, null, state); + processOperations(null, outbox, arrived, null, null, null, state); db.folder().setFolderError(outbox.id, null); + + for (EntityOperation op : arrived) + synchronized (handling) { + handling.remove(op.id); + } } catch (Throwable ex) { Log.e(outbox.name, ex); reportError(null, outbox, ex);