Skip to content

Commit 427930a

Browse files
committed
Fix race condition: ArrayList is not thread-safe.
1 parent 7aae568 commit 427930a

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

android/app/src/main/java/betaflight/configurator/protocols/bluetooth/BetaflightBluetoothPlugin.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
import java.util.List;
4444
import java.util.Locale;
4545
import java.util.Map;
46+
import java.util.Queue;
4647
import java.util.UUID;
4748
import java.util.concurrent.ConcurrentHashMap;
49+
import java.util.concurrent.ConcurrentLinkedQueue;
4850
import java.util.concurrent.atomic.AtomicReference;
4951

5052
/**
@@ -104,7 +106,7 @@ private static final class DiscoveredDevice {
104106
private Runnable connectTimeoutRunnable;
105107
private PluginCall pendingConnectCall;
106108
private String connectedDeviceId;
107-
private final List<PluginCall> pendingStartNotificationCalls = new ArrayList<>();
109+
private final Queue<PluginCall> pendingStartNotificationCalls = new ConcurrentLinkedQueue<>();
108110
private volatile boolean servicesDiscovered = false;
109111

110112
@Override
@@ -832,9 +834,8 @@ private void flushPendingStartNotificationCalls() {
832834
if (!servicesDiscovered || pendingStartNotificationCalls.isEmpty()) {
833835
return;
834836
}
835-
List<PluginCall> queued = new ArrayList<>(pendingStartNotificationCalls);
836-
pendingStartNotificationCalls.clear();
837-
for (PluginCall pendingCall : queued) {
837+
PluginCall pendingCall;
838+
while ((pendingCall = pendingStartNotificationCalls.poll()) != null) {
838839
startNotificationsInternal(pendingCall);
839840
}
840841
}
@@ -843,9 +844,8 @@ private void rejectPendingStartNotifications(String reason) {
843844
if (pendingStartNotificationCalls.isEmpty()) {
844845
return;
845846
}
846-
List<PluginCall> queued = new ArrayList<>(pendingStartNotificationCalls);
847-
pendingStartNotificationCalls.clear();
848-
for (PluginCall pendingCall : queued) {
847+
PluginCall pendingCall;
848+
while ((pendingCall = pendingStartNotificationCalls.poll()) != null) {
849849
pendingCall.setKeepAlive(false);
850850
pendingCall.reject(reason);
851851
}

0 commit comments

Comments
 (0)