question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Websocket connection on wear OS does not stop when application is placed in the background

See original GitHub issue

Home Assistant Android version:

latest master

Android version:

11

Phone model:

Pixel watch

Home Assistant version:

2022.10.5

Last working Home Assistant release (if known):

n/a

Description of problem:

When the wear OS app is launched a websocket connection to get all entity states is registered. Then when the user puts the app in the background by pressing the crown to get to the home screen the connection remains active until the app is removed from recents.

Traceback (if applicable, to get the logs you may refer to: https://companion.home-assistant.io/docs/troubleshooting/faqs/#android-crash-logs):

Logs
2022-10-28 20:13:54.705 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:54.706 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:54.707 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:54.738 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:54.771 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:54.786 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:54.793 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:54.868 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:54.869 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:54.869 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:55.463 14751-25207 WebSocketRepository     pid-14751                            D  Unsubscribing from subscribe_events with data {event_type=state_changed}
2022-10-28 20:13:55.464 14751-25207 WebSocketRepository     pid-14751                            D  Sending message 92: {type=unsubscribe_events, subscription=87, id=92}
2022-10-28 20:13:55.467 14751-25688 WebSocketRepository     pid-14751                            D  Unsubscribing from subscribe_events with data {event_type=device_registry_updated}
2022-10-28 20:13:55.468 14751-25207 WebSocketRepository     pid-14751                            D  Message number 92 sent
2022-10-28 20:13:55.477 14751-25688 WebSocketRepository     pid-14751                            D  Sending message 93: {type=unsubscribe_events, subscription=89, id=93}
2022-10-28 20:13:55.478 14751-25688 WebSocketRepository     pid-14751                            D  Message number 93 sent
2022-10-28 20:13:55.506 14751-25802 WebSocketRepository     pid-14751                            D  Unsubscribing from subscribe_events with data {event_type=area_registry_updated}
2022-10-28 20:13:55.507 14751-25802 WebSocketRepository     pid-14751                            D  Sending message 94: {type=unsubscribe_events, subscription=88, id=94}
2022-10-28 20:13:55.508 14751-25802 WebSocketRepository     pid-14751                            D  Message number 94 sent
2022-10-28 20:13:55.511 14751-25693 WebSocketRepository     pid-14751                            D  Unsubscribing from subscribe_events with data {event_type=entity_registry_updated}
2022-10-28 20:13:55.512 14751-25693 WebSocketRepository     pid-14751                            D  Sending message 95: {type=unsubscribe_events, subscription=90, id=95}
2022-10-28 20:13:55.512 14751-25693 WebSocketRepository     pid-14751                            D  Message number 95 sent
2022-10-28 20:13:55.809 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:55.810 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:55.811 14751-25859 WebSocketRepository     pid-14751                            D  Received event for unknown subscription, unsubscribing
2022-10-28 20:13:55.812 14751-25859 WebSocketRepository     pid-14751                            D  Sending message 96: {type=unsubscribe_events, subscription=87, id=96}
2022-10-28 20:13:55.814 14751-25859 WebSocketRepository     pid-14751                            D  Message number 96 sent
2022-10-28 20:13:56.006 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.017 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:56.018 14751-25857 WebSocketRepository     pid-14751                            D  Received event for unknown subscription, unsubscribing
2022-10-28 20:13:56.019 14751-25857 WebSocketRepository     pid-14751                            D  Sending message 97: {type=unsubscribe_events, subscription=87, id=97}
2022-10-28 20:13:56.019 14751-25857 WebSocketRepository     pid-14751                            D  Message number 97 sent
2022-10-28 20:13:56.022 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:56.023 14751-25856 WebSocketRepository     pid-14751                            D  Received event for unknown subscription, unsubscribing
2022-10-28 20:13:56.023 14751-25856 WebSocketRepository     pid-14751                            D  Sending message 98: {type=unsubscribe_events, subscription=87, id=98}
2022-10-28 20:13:56.024 14751-25856 WebSocketRepository     pid-14751                            D  Message number 98 sent
2022-10-28 20:13:56.026 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:56.028 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.031 14751-25857 WebSocketRepository     pid-14751                            D  Received event for unknown subscription, unsubscribing
2022-10-28 20:13:56.032 14751-25857 WebSocketRepository     pid-14751                            D  Sending message 99: {type=unsubscribe_events, subscription=87, id=99}
2022-10-28 20:13:56.034 14751-25857 WebSocketRepository     pid-14751                            D  Message number 99 sent
2022-10-28 20:13:56.036 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:56.037 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.038 14751-25857 WebSocketRepository     pid-14751                            D  Received event for unknown subscription, unsubscribing
2022-10-28 20:13:56.039 14751-25857 WebSocketRepository     pid-14751                            D  Sending message 100: {type=unsubscribe_events, subscription=87, id=100}
2022-10-28 20:13:56.040 14751-25857 WebSocketRepository     pid-14751                            D  Message number 100 sent
2022-10-28 20:13:56.089 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:56.090 14751-14789 WebSocketRepository     pid-14751                            D  Message number 87 received
2022-10-28 20:13:56.090 14751-25859 WebSocketRepository     pid-14751                            D  Received event for unknown subscription, unsubscribing
2022-10-28 20:13:56.090 14751-25856 WebSocketRepository     pid-14751                            D  Received event for unknown subscription, unsubscribing
2022-10-28 20:13:56.090 14751-25859 WebSocketRepository     pid-14751                            D  Sending message 101: {type=unsubscribe_events, subscription=87, id=101}
2022-10-28 20:13:56.091 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.091 14751-14789 WebSocketRepository     pid-14751                            D  Message number 92 received
2022-10-28 20:13:56.092 14751-25859 WebSocketRepository     pid-14751                            D  Message number 101 sent
2022-10-28 20:13:56.093 14751-25856 WebSocketRepository     pid-14751                            D  Sending message 102: {type=unsubscribe_events, subscription=87, id=102}
2022-10-28 20:13:56.094 14751-25856 WebSocketRepository     pid-14751                            D  Message number 102 sent
2022-10-28 20:13:56.141 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.144 14751-14789 WebSocketRepository     pid-14751                            D  Message number 93 received
2022-10-28 20:13:56.174 14751-14789 WebSocketRepository     pid-14751                            D  Message number 94 received
2022-10-28 20:13:56.192 14751-14789 WebSocketRepository     pid-14751                            D  Message number 95 received
2022-10-28 20:13:56.193 14751-14789 WebSocketRepository     pid-14751                            D  Message number 96 received
2022-10-28 20:13:56.194 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.195 14751-14789 WebSocketRepository     pid-14751                            D  Message number 97 received
2022-10-28 20:13:56.196 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.197 14751-14789 WebSocketRepository     pid-14751                            D  Message number 98 received
2022-10-28 20:13:56.199 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.199 14751-14789 WebSocketRepository     pid-14751                            D  Message number 99 received
2022-10-28 20:13:56.200 14751-14789 WebSocketRepository     pid-14751                            D  Message number 100 received
2022-10-28 20:13:56.227 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.228 14751-14789 WebSocketRepository     pid-14751                            D  Message number 101 received
2022-10-28 20:13:56.232 14751-14789 WebSocketRepository     pid-14751                            D  Websocket: onMessage (text)
2022-10-28 20:13:56.232 14751-14789 WebSocketRepository     pid-14751                            D  Message number 102 received

Screenshot of problem:

Additional information:

The log snippet is a bit weird as its still showing messages coming in after unsubscribing so something seems amiss here. 🤔

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jpelgromcommented, Oct 29, 2022

Edit: in my research I did come to find out that viewModelScope only cancels during onDestroy so I think thats why we never see it stop?

Yes the scope won’t be cancelled on pause, otherwise your viewmodels would become pretty useless. A ViewModel isn’t supposed to care about the activity lifecycle 😃

So I think the best solution would be to either move stuff around or do it in a different scope that we then start/stop from the activity’s lifecycle with something like lifecycleScope.launch { lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) { /** collect stuff **/ } }? It is important to distinguish first load and subsequent loads though; at first a loading spinner is required but for later loads there is already some data we can show.

0reactions
dshokouhicommented, Oct 30, 2022

lifecycleScope.launch { lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) { /** collect stuff **/ } }

I did see this in my limited research as well but my understanding here is not that great

It is important to distinguish first load and subsequent loads though; at first a loading spinner is required but for later loads there is already some data we can show.

I agree the first call may not even complete before the screen times out while the user is waiting

Read more comments on GitHub >

github_iconTop Results From Across the Web

Network access and sync on Wear OS
With Wear OS by Google, a watch can communicate with a network directly, without access to an Android or iOS phone.
Read more >
Android app SocketTimeoutException after 300000ms ...
The app is able to connect to end-point when the smartwatch is connected via USB cable to the PC in debug mode, but...
Read more >
Android Companion 2022.2: Local Push Notifications! - Blog
A persistent notification lets Android know the app has important ongoing tasks where we do not want the background process stopped.
Read more >
Connecting to Web Services with Android Wear
Set up the information so it can connect to the API, building the final URL. So as not to lock the UI, create...
Read more >
AXIS M3086-V Dome Camera - Axis documentation
Audio: No support for sending audio to the camera through the browser (i.e. through ... up analytics) requires that the browser can connect...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found