Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent socket response continuation being called multiple times (#3338) #4616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.homeassistant.companion.android.common.data.websocket

import io.homeassistant.companion.android.common.data.websocket.impl.entities.SocketResponse
import java.util.concurrent.atomic.AtomicBoolean
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.SharedFlow
Expand All @@ -24,4 +25,6 @@ data class WebSocketRequest(
val eventTimeout: Long = 0L,
val onEvent: Channel<Any>? = null,
var onResponse: CancellableContinuation<SocketResponse>? = null
)
) {
val hasContinuationBeenInvoked = AtomicBoolean(false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,11 @@ class WebSocketRepositoryImpl @AssistedInject constructor(
val id = response.id!!
activeMessages[id]?.let {
it.onResponse?.let { cont ->
if (cont.isActive) cont.resumeWith(Result.success(response))
if (!it.hasContinuationBeenInvoked.getAndSet(true) && cont.isActive) {
cont.resumeWith(Result.success(response))
} else {
Log.w(TAG, "Response continuation has already been invoked for ${response.id}, ${response.event}")
}
}
if (it.eventFlow == null) {
activeMessages.remove(id)
Expand Down Expand Up @@ -818,7 +822,11 @@ class WebSocketRepositoryImpl @AssistedInject constructor(
.filterValues { it.eventFlow == null }
.forEach {
it.value.onResponse?.let { cont ->
if (cont.isActive) cont.resumeWithException(IOException())
if (!it.value.hasContinuationBeenInvoked.getAndSet(true) && cont.isActive) {
cont.resumeWithException(IOException())
} else {
Log.w(TAG, "Response continuation has already been invoked, skipping IOException")
}
}
activeMessages.remove(it.key)
}
Expand Down