Websocket connection on wear OS does not stop when application is placed in the background
See original GitHub issueHome 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:
- Created a year ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
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.I did see this in my limited research as well but my understanding here is not that great
I agree the first call may not even complete before the screen times out while the user is waiting