Ensure we only have one IncomingMessageObserver.

We saw a worrisome log that implied there may be a situation where
there's two IncomingMessageObservers. I can't see how that would happen,
but this is a failsafe to prevent that from happening.
fork-5.53.8
Greyson Parrelli 2021-02-10 12:12:40 -05:00 zatwierdzone przez Cody Henthorne
rodzic 158f3d898f
commit 763a12dbc6
1 zmienionych plików z 9 dodań i 0 usunięć

Wyświetl plik

@ -37,6 +37,7 @@ import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
public class IncomingMessageObserver { public class IncomingMessageObserver {
@ -45,6 +46,8 @@ public class IncomingMessageObserver {
public static final int FOREGROUND_ID = 313399; public static final int FOREGROUND_ID = 313399;
private static final long REQUEST_TIMEOUT_MINUTES = 1; private static final long REQUEST_TIMEOUT_MINUTES = 1;
private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger(0);
private static SignalServiceMessagePipe pipe = null; private static SignalServiceMessagePipe pipe = null;
private static SignalServiceMessagePipe unidentifiedPipe = null; private static SignalServiceMessagePipe unidentifiedPipe = null;
@ -59,6 +62,10 @@ public class IncomingMessageObserver {
private volatile boolean terminated; private volatile boolean terminated;
public IncomingMessageObserver(@NonNull Application context) { public IncomingMessageObserver(@NonNull Application context) {
if (INSTANCE_COUNT.incrementAndGet() != 1) {
throw new AssertionError("Multiple observers!");
}
this.context = context; this.context = context;
this.networkAccess = ApplicationDependencies.getSignalServiceNetworkAccess(); this.networkAccess = ApplicationDependencies.getSignalServiceNetworkAccess();
this.decryptionDrainedListeners = new CopyOnWriteArrayList<>(); this.decryptionDrainedListeners = new CopyOnWriteArrayList<>();
@ -160,6 +167,8 @@ public class IncomingMessageObserver {
} }
public void terminateAsync() { public void terminateAsync() {
INSTANCE_COUNT.decrementAndGet();
SignalExecutors.BOUNDED.execute(() -> { SignalExecutors.BOUNDED.execute(() -> {
Log.w(TAG, "Beginning termination."); Log.w(TAG, "Beginning termination.");
terminated = true; terminated = true;