From 3527930f898466747920e759331cae1dd6f618ef Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 11 Feb 2014 20:17:34 +0100 Subject: [PATCH] Fix 'endless' loop in ImapFolderPusher Under certain circumstances it's possible that the 'push state' isn't updated to contain the most recent 'UIDNEXT' value. In that case ImapFolderPusher.start() would execute the same code path through its main loop over and over again, preventing the device from going to sleep. Rather than changing the code to update the 'push state' in the corner case that triggers the behavior described above, this commit introduces another mechanism to track the 'UIDNEXT' value. This should also catch as of yet unknown cases where the 'push state' isn't properly updated. At some point in the future I hope we get to a point where we only persist the 'push state' when we manually stop/restart the service. During normal operation there's no need to read from/write to storage all the time. Fixes issue 4907 --- src/com/fsck/k9/mail/store/ImapStore.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/com/fsck/k9/mail/store/ImapStore.java b/src/com/fsck/k9/mail/store/ImapStore.java index eda66afb9..00672c4f9 100644 --- a/src/com/fsck/k9/mail/store/ImapStore.java +++ b/src/com/fsck/k9/mail/store/ImapStore.java @@ -2978,6 +2978,7 @@ public class ImapStore extends Store { if (K9.DEBUG) Log.i(K9.LOG_TAG, "Pusher starting for " + getLogId()); + long lastUidNext = -1L; while (!stop.get()) { try { long oldUidNext = -1L; @@ -2990,6 +2991,18 @@ public class ImapStore extends Store { } catch (Exception e) { Log.e(K9.LOG_TAG, "Unable to get oldUidNext for " + getLogId(), e); } + + /* + * This makes sure 'oldUidNext' is never smaller than 'UIDNEXT' from + * the last loop iteration. This way we avoid looping endlessly causing + * the battery to drain. + * + * See issue 4907 + */ + if (oldUidNext < lastUidNext) { + oldUidNext = lastUidNext; + } + ImapConnection oldConnection = mConnection; internalOpen(OPEN_MODE_RO); ImapConnection conn = mConnection; @@ -3041,6 +3054,8 @@ public class ImapStore extends Store { if (startUid < 1) { startUid = 1; } + + lastUidNext = newUidNext; if (newUidNext > startUid) { if (K9.DEBUG)