mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
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:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user