fix potential race condition in chatlist loading (#2001)

this should fix the race condition mentioned at #2000
and in the source code by synchronizing getters/setters of the booleans.
This commit is contained in:
bjoern
2021-07-31 21:24:20 +02:00
committed by GitHub
parent 6de566ac11
commit 06353ef64d
@@ -380,34 +380,34 @@ public class ConversationListFragment extends Fragment
((ConversationSelectedListener)getActivity()).onCreateConversation(chatId);
}
private final Object loadChatlistLock = new Object();
private boolean inLoadChatlist;
private boolean needsAnotherLoad;
private void loadChatlistAsync() {
if (inLoadChatlist) {
synchronized (loadChatlistLock) {
needsAnotherLoad = true;
Log.i(TAG, "chatlist loading debounced");
} else {
if (inLoadChatlist) {
Log.i(TAG, "chatlist loading debounced");
return;
}
inLoadChatlist = true;
Util.runOnAnyBackgroundThread(() -> {
}
needsAnotherLoad = false;
loadChatlist();
while (needsAnotherLoad) {
Util.sleep(100);
Util.runOnAnyBackgroundThread(() -> {
while(true) {
synchronized (loadChatlistLock) {
if (!needsAnotherLoad) {
inLoadChatlist = false;
return;
}
needsAnotherLoad = false;
Log.i(TAG, "executing debounced chatlist loading");
loadChatlist();
}
// in theory, here is a potential race condition:
// if the background thread is exactly here and the main thread checks `inLoadChatlist` at that moment,
// one update will be missing.
// that can probably be fixed with some other multi-theading techniques, any suggestions welcome :)
inLoadChatlist = false;
});
}
Log.i(TAG, "executing debounced chatlist loading");
loadChatlist();
Util.sleep(100);
}
});
}
private void loadChatlist() {