From a6540880c92bcb9439fa036c935c504e8f62dbb1 Mon Sep 17 00:00:00 2001 From: andrew-signal Date: Wed, 21 May 2025 07:43:18 -0400 Subject: [PATCH] Cancel chatConnectionFuture when we get disconnect() while CONNECTING. --- .../internal/websocket/LibSignalChatConnection.kt | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/libsignal-service/src/main/java/org/whispersystems/signalservice/internal/websocket/LibSignalChatConnection.kt b/libsignal-service/src/main/java/org/whispersystems/signalservice/internal/websocket/LibSignalChatConnection.kt index 9a96a9339f..b834fdbd49 100644 --- a/libsignal-service/src/main/java/org/whispersystems/signalservice/internal/websocket/LibSignalChatConnection.kt +++ b/libsignal-service/src/main/java/org/whispersystems/signalservice/internal/websocket/LibSignalChatConnection.kt @@ -240,13 +240,10 @@ class LibSignalChatConnection( // OkHttpWebSocketConnection will terminate a connection if disconnect() is called while // the connection itself is still CONNECTING, so we carry forward that behavior here. if (state.value == WebSocketConnectionState.CONNECTING) { - // The right way to do this is to cancel the CompletableFuture returned by connectChat(). - // This will terminate forward progress on the connection attempt, and mostly closely match - // what OkHttpWebSocketConnection does. - // Unfortunately, libsignal's CompletableFuture does not yet support cancellation. - // So, instead, we set a flag to disconnect() as soon as the connection completes. - // TODO [andrew]: Add cancellation support to CompletableFuture and use it here - state.onNext(WebSocketConnectionState.DISCONNECTING) + Log.i(TAG, "$name Cancelling connection attempt...") + // This is safe because we just checked that state == CONNECTING + chatConnectionFuture!!.cancel(true) + state.onNext(WebSocketConnectionState.DISCONNECTED) return }