Adjust LibSignalChatConnection.disconnect() so we only transition to DISCONNECTED once.

This commit is contained in:
andrew-signal
2025-03-21 15:30:36 -04:00
committed by Cody Henthorne
parent 7c9cd8964f
commit 9b98337e82
2 changed files with 44 additions and 10 deletions

View File

@@ -127,15 +127,17 @@ class LibSignalChatConnection(
val state = BehaviorSubject.createDefault(WebSocketConnectionState.DISCONNECTED)
val stateMonitor = state.subscribe { nextState ->
if (nextState == WebSocketConnectionState.DISCONNECTED) {
cleanup()
}
val stateMonitor = state
.skip(1) // Skip the transition to the initial DISCONNECTED state
.subscribe { nextState ->
if (nextState == WebSocketConnectionState.DISCONNECTED) {
cleanup()
}
CHAT_SERVICE_LOCK.withLock {
stateChangedOrMessageReceivedCondition.signalAll()
CHAT_SERVICE_LOCK.withLock {
stateChangedOrMessageReceivedCondition.signalAll()
}
}
}
private fun cleanup() {
Log.i(TAG, "$name [cleanup]")
@@ -243,10 +245,14 @@ class LibSignalChatConnection(
chatConnection!!.disconnect()
.whenComplete(
onSuccess = {
Log.i(TAG, "$name Disconnected")
state.onNext(WebSocketConnectionState.DISCONNECTED)
// This future completion means the WebSocket close frame has been sent off, but we
// have not yet received a close frame back from the server.
// To match the behavior of OkHttpWebSocketConnection, we should transition to DISCONNECTED
// only when we get the close frame back from the server, which happens when
// onConnectionInterrupted is called.
},
onFailure = { throwable ->
// We failed to write the close frame to the server? Something is very wrong, give up and tear down.
Log.w(TAG, "$name Disconnect failed", throwable)
state.onNext(WebSocketConnectionState.DISCONNECTED)
}
@@ -510,7 +516,13 @@ class LibSignalChatConnection(
override fun onConnectionInterrupted(chat: ChatConnection, disconnectReason: ChatServiceException?) {
CHAT_SERVICE_LOCK.withLock {
Log.i(TAG, "$name connection interrupted", disconnectReason)
if (disconnectReason == null) {
// disconnectReason = null means we requested this disconnect earlier, and this is confirmation
// that disconnection is complete.
Log.i(TAG, "$name disconnected")
} else {
Log.i(TAG, "$name connection unexpectedly closed", disconnectReason)
}
chatConnection = null
state.onNext(WebSocketConnectionState.DISCONNECTED)
}