Skip to content
Open
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
29 changes: 26 additions & 3 deletions mobile/src/main/java/org/openhab/habdroid/util/ItemClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.ParserConfigurationException
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -119,10 +121,33 @@ object ItemClient {
)
var eventSubscription = createSubscription()

suspend fun restartSubscription() {
try {
eventSubscription.cancel()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This raised ClosedReceiveChannelExceptions and crashes the app and I don't know why it isn't catched.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Raised' as in 'without the try-catch'? How exactly does the exception (message) look like?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACRA caught a ClosedReceiveChannelException for org.openhab.habdroid
kotlinx.coroutines.channels.ClosedReceiveChannelException: Channel was closed
	at kotlinx.coroutines.channels.BufferedChannel.getReceiveException(BufferedChannel.kt:1760)
	at kotlinx.coroutines.channels.BufferedChannel.resumeWaiterOnClosedChannel(BufferedChannel.kt:2202)
	at kotlinx.coroutines.channels.BufferedChannel.resumeReceiverOnClosedChannel(BufferedChannel.kt:2191)
	at kotlinx.coroutines.channels.BufferedChannel.cancelSuspendedReceiveRequests(BufferedChannel.kt:2184)
	at kotlinx.coroutines.channels.BufferedChannel.completeClose(BufferedChannel.kt:1961)
	at kotlinx.coroutines.channels.BufferedChannel.isClosed(BufferedChannel.kt:2240)
	at kotlinx.coroutines.channels.BufferedChannel.isClosedForSend0(BufferedChannel.kt:2215)
	at kotlinx.coroutines.channels.BufferedChannel.isClosedForSend(BufferedChannel.kt:2212)
	at kotlinx.coroutines.channels.BufferedChannel.completeCloseOrCancel(BufferedChannel.kt:1933)
	at kotlinx.coroutines.channels.BufferedChannel.closeOrCancelImpl(BufferedChannel.kt:1826)
	at kotlinx.coroutines.channels.BufferedChannel.close(BufferedChannel.kt:1785)
	at kotlinx.coroutines.channels.SendChannel$DefaultImpls.close$default(Channel.kt:95)
	at org.openhab.habdroid.util.HttpClient$SseSubscription.cancel(HttpClient.kt:307)
	at org.openhab.habdroid.util.ItemClient.listenForItemChange$restartSubscription(ItemClient.kt:126)
	at org.openhab.habdroid.util.ItemClient.access$listenForItemChange$restartSubscription(ItemClient.kt:36)
	at org.openhab.habdroid.util.ItemClient$listenForItemChange$resetAliveWatchdog$1.invokeSuspend(ItemClient.kt:141)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:231)
	at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:164)
	at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:466)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:500)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:489)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeUndispatched(CancellableContinuationImpl.kt:587)
	at kotlinx.coroutines.android.HandlerContext$scheduleResumeAfterDelay$$inlined$Runnable$1.run(Runnable.kt:15)
	at android.os.Handler.handleCallback(Handler.java:959)
	at android.os.Handler.dispatchMessage(Handler.java:100)
	at android.os.Looper.loopOnce(Looper.java:232)
	at android.os.Looper.loop(Looper.java:317)
	at android.app.ActivityThread.main(ActivityThread.java:8705)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@9402587, Dispatchers.Main]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can test this by setting the delay in line 139 to less than 10 seconds.

Copy link
Copy Markdown
Contributor

@maniac103 maniac103 Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ping 😉 I totally forgot about this. My solution would be this patch.

} catch (e: Exception) {
Log.e(TAG, "Error cancelling SSE subscription for item $item", e)
}
delay(5.seconds)
eventSubscription = createSubscription()
}

var watchdogJob: Job? = null

fun resetAliveWatchdog() {
watchdogJob?.cancel()
watchdogJob = scope.launch {
delay(30.seconds) // ALIVE event is sent every 10 seconds
Log.d(TAG, "No events received for item $item, restarting subscription")
restartSubscription()
}
}
resetAliveWatchdog()

try {
while (scope.isActive) {
try {
val event = JSONObject(eventSubscription.getNextEvent())
resetAliveWatchdog()
if (event.optString("type") == "ALIVE") {
Log.d(TAG, "Got ALIVE event for item $item")
continue
Expand All @@ -144,9 +169,7 @@ object ItemClient {
Log.e(TAG, "Failed parsing JSON of state change event for item $item", e)
} catch (e: HttpClient.SseFailureException) {
Log.e(TAG, "SSE failure for item $item", e)
eventSubscription.cancel()
delay(5.seconds)
eventSubscription = createSubscription()
restartSubscription()
}
}
} finally {
Expand Down