jellyfin-kodi icon indicating copy to clipboard operation
jellyfin-kodi copied to clipboard

Websocket does not reconnect after Jellyfin server restart

Open hagaygo opened this issue 1 year ago • 11 comments

Sounds similar to https://github.com/jellyfin/jellyfin-kodi/issues/627

I got 3 LibreElec on RPI4/5 (2 with LE 11.0.3 official and 1 with nightly for RPI5) All 3 working just fine , data is synced using the web sockets and with kodi sync after some down time.

When restarting Jellyfin server , no client is re-connecting using websocket , i can easily verify this by Jellyfin's web GUI (devices not shown as cast target).

At first i thought i need to wait few minutes , but it never reconnects , only restarting kodi or the addon makes the client re-connect the websocket.

To Reproduce

  1. Check the clients availability on Jellyfin's web GUI
  2. Restart Jellyfin server
  3. Check and confirm clients not shown on Jellyfin's web GUI and kodi's log file

Expected behavior Clients should re-connect after reasonable interval. (few minutes)

System (please complete the following information):

  • OS: LibreElec 11.0.3
  • Jellyfin Version: 10.8.13
  • Kodi Version: 20.2
  • Addon Version: 7.11.py3
  • Playback Mode: Native

hagaygo avatar Dec 13 '23 11:12 hagaygo

I am having the same problem, my jellyfin instance is running in a docker. When I update / reboot it, the websocket connection seems to not be reestablished.

I noticed it by two things:

  • "Play on" / cast doesn't show the kodi client anymore
  • The automatic syncing between jellyfin and kodi breaks.

System (please complete the following information):

  • OS: LibreElec 11.0.3
  • Jellyfin Version: 10.8.13
  • Kodi Version: 20.2
  • Addon Version: 7.11.py3
  • Playback Mode: Addon

Flobbes avatar Jan 21 '24 19:01 Flobbes

Noticing the same on my setup too having moved from PKC to Jellyfin. I don't keep my server powered on overnight and then notice that all my clients are disconnected until Kodi is restarted etc.

PKC did seem to react quite well with Plex been offline & then re-establishing the connectivity etc.

Hopefully its a minor change that will make a big improvement if this can be sorted.

Adam1986 avatar Jan 21 '24 20:01 Adam1986

I've created a PR which fixed this in my setup. After reconnecting the websocket a small delay is needed before the client capabilities are registered or the session ends up in a state where it is does not report as use controllable and doesn't appear in the play on dialog.

Tested through different reconnect scenarios, client reappears in Play On and play status sync resumes.

smcmnz avatar Jan 27 '24 01:01 smcmnz

I've created a PR which fixed this in my setup. After reconnecting the websocket a small delay is needed before the client capabilities are registered or the session ends up in a state where it is does not report as use controllable and doesn't appear in the play on dialog.

Tested through different reconnect scenarios, client reappears in Play On and play status sync resumes.

Thanks so much for taking a look at a resolution for this.

Is it sufficient to just edit the files manually that you changed or does other work need doing? Only ask as I tried editing the files manually on my setup & tried it on a client and it appeared to make no difference.

Adam1986 avatar Jan 30 '24 21:01 Adam1986

Is it sufficient to just edit the files manually that you changed or does other work need doing? Only ask as I tried editing the files manually on my setup & tried it on a client and it appeared to make no difference.

You'll need to restart Kodi to reconnect using the new client options. If it does not reconnect can you enable debug mode in the addon and submit the log of an interrupted connection.

smcmnz avatar Jan 30 '24 22:01 smcmnz

Is it sufficient to just edit the files manually that you changed or does other work need doing? Only ask as I tried editing the files manually on my setup & tried it on a client and it appeared to make no difference.

You'll need to restart Kodi to reconnect using the new client options. If it does not reconnect can you enable debug mode in the addon and submit the log of an interrupted connection.

Yeah I immediately restarted after making the changes to the files. I'll look at doing debug mode tomorrow and submitting that. Assuming debug just gets output to the main kodi.log file?

Cheers

Adam1986 avatar Jan 30 '24 23:01 Adam1986

OK so from looking at two of my clients, it seems that if the connection is disrupted from the server side (I.E service stopped) when that comes back up connectivity comes back fine:

2024-01-31 10:10:38.667 T:1277     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.ws_client -> ERROR::jellyfin_kodi/jellyfin/ws_client.py:70 [Errno 104] Connection reset by peer
2024-01-31 10:15:28.781 T:1277     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.ws_client -> INFO::jellyfin_kodi/jellyfin/ws_client.py:73 --->[ websocket opened ]
2024-01-31 10:15:29.793 T:1277     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.http -> DEBUG::jellyfin_kodi/jellyfin/http.py:84 --->[ http ] {
                                                       "params": null,
                                                       "json": {
                                                           "PlayableMediaTypes": "Audio,Video",
                                                           "SupportsMediaControl": true,
                                                           "SupportedCommands": "MoveUp,MoveDown,MoveLeft,MoveRight,Select,Back,ToggleContextMenu,ToggleFullscreen,ToggleOsdMenu,GoHome,PageUp,NextLetter,GoToSearch,GoToSettings,PageDown,PreviousLetter,TakeScreenshot,VolumeUp,VolumeDown,ToggleMute,SendString,DisplayMessage,SetAudioStreamIndex,SetSubtitleStreamIndex,SetRepeatMode,Mute,Unmute,SetVolume,Play,Playstate,PlayNext,PlayMediaSource"
                                                       },
                                                       "type": "POST",
                                                       "url": "http://{jellyfin-server}/Sessions/Capabilities/Full",
                                                       "headers": {
                                                           "Content-type": "application/json",
                                                           "Accept-Charset": "UTF-8,*",
                                                           "Accept-encoding": "gzip",
                                                           "User-Agent": "Jellyfin-Kodi/0.7.11+py3",
                                                           "x-emby-authorization": "MediaBrowser Client=Kodi, Device=KITCHEN-FD, DeviceId=37ffbbad-8ecc-4711-911c-53c64adcd0d3, Version=0.7.11+py3, UserId=652370786690407d9644918c90401e00",
                                                           "X-MediaBrowser-Token": "{jellyfin-token}"
                                                       },
                                                       "timeout": 30,
                                                       "verify": true
                                                   }
2024-01-31 10:15:29.929 T:1277     info <general>: JELLYFIN.jellyfin_kodi.helper.utils -> DEBUG::jellyfin_kodi/helper/utils.py:152 ---[ event: plugin.video.jellyfin/ForceKeepAlive ] "[60]"
2024-01-31 10:16:17.203 T:1277     info <general>: JELLYFIN.jellyfin_kodi.helper.utils -> DEBUG::jellyfin_kodi/helper/utils.py:152 ---[ event: plugin.video.jellyfin/ForceKeepAlive ] "[60]"

But if the connectivity was disrupted on the client side, I.E by me dropping the Wi-Fi connection, then something seems to go awry, I knocked the connection offline at about 10:24 but do not see a "connection reset by peer" like before, several minutes later Jellyfin does appear to make a connection again to the server, but no sync data comes through, and the client does not show back up in the "Play On" via my Android App:

2024-01-31 10:33:33.185 T:1277     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.ws_client -> INFO::jellyfin_kodi/jellyfin/ws_client.py:73 --->[ websocket opened ]
2024-01-31 10:33:34.198 T:1277     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.http -> DEBUG::jellyfin_kodi/jellyfin/http.py:84 --->[ http ] {
                                                       "params": null,
                                                       "json": {
                                                           "PlayableMediaTypes": "Audio,Video",
                                                           "SupportsMediaControl": true,
                                                           "SupportedCommands": "MoveUp,MoveDown,MoveLeft,MoveRight,Select,Back,ToggleContextMenu,ToggleFullscreen,ToggleOsdMenu,GoHome,PageUp,NextLetter,GoToSearch,GoToSettings,PageDown,PreviousLetter,TakeScreenshot,VolumeUp,VolumeDown,ToggleMute,SendString,DisplayMessage,SetAudioStreamIndex,SetSubtitleStreamIndex,SetRepeatMode,Mute,Unmute,SetVolume,Play,Playstate,PlayNext,PlayMediaSource"
                                                       },
                                                       "type": "POST",
                                                       "url": "http://{jellyfin-server}/Sessions/Capabilities/Full",
                                                       "headers": {
                                                           "Content-type": "application/json",
                                                           "Accept-Charset": "UTF-8,*",
                                                           "Accept-encoding": "gzip",
                                                           "User-Agent": "Jellyfin-Kodi/0.7.11+py3",
                                                           "x-emby-authorization": "MediaBrowser Client=Kodi, Device=KITCHEN-FD, DeviceId=37ffbbad-8ecc-4711-911c-53c64adcd0d3, Version=0.7.11+py3, UserId=652370786690407d9644918c90401e00",
                                                           "X-MediaBrowser-Token": "{jellyfin-token}"
                                                       },
                                                       "timeout": 30,
                                                       "verify": true
                                                   }
2024-01-31 10:34:05.260 T:1277     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.http -> ERROR::jellyfin_kodi/jellyfin/http.py:124 405 Client Error: Method Not Allowed for url: http://{jellyfin-server}/Sessions/Capabilities/Full
2024-01-31 10:34:05.265 T:1277     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.ws_client -> ERROR::jellyfin_kodi/jellyfin/ws_client.py:70 (405, HTTPError('405 Client Error: Method Not Allowed for url: http://{jellyfin-server}/Sessions/Capabilities/Full'))
2024-01-31 10:34:05.272 T:1277     info <general>: JELLYFIN.jellyfin_kodi.helper.utils -> DEBUG::jellyfin_kodi/helper/utils.py:152 ---[ event: plugin.video.jellyfin/ForceKeepAlive ] "[60]"
2024-01-31 10:34:21.239 T:1277     info <general>: JELLYFIN.jellyfin_kodi.helper.utils -> DEBUG::jellyfin_kodi/helper/utils.py:152 ---[ event: plugin.video.jellyfin/ForceKeepAlive ] "[60]"

Adam1986 avatar Jan 31 '24 10:01 Adam1986

Your log shows the websocket is reconnected but the API call to register the capabilities fails with 405 Client Error: Method Not Allowed this API call is what makes the player visible in the Play On dialog.

Can you try increasing the time.sleep() I added to see if it helps. I found without a sleep the API call will 405, 1 second worked in my setup but I'm not sure what Jellyfin is doing after the websocket connects

smcmnz avatar Jan 31 '24 21:01 smcmnz

OK so I increased this to 60 seconds and tested again, this has improved somewhat as I now see the device in my Play To section on the Android App.

However nothing works still from syncing the play progress perspective and this does not update/change.

2024-01-31 23:24:22.902 T:2733     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.ws_client -> ERROR::jellyfin_kodi/jellyfin/ws_client.py:70 [Errno 101] Network is unreachable
2024-01-31 23:24:33.037 T:2733     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.ws_client -> INFO::jellyfin_kodi/jellyfin/ws_client.py:73 --->[ websocket opened ]
2024-01-31 23:25:33.047 T:2733     info <general>: JELLYFIN.jellyfin_kodi.jellyfin.http -> DEBUG::jellyfin_kodi/jellyfin/http.py:84 --->[ http ] {
                                                       "params": null,
                                                       "json": {
                                                           "PlayableMediaTypes": "Audio,Video",
                                                           "SupportsMediaControl": true,
                                                           "SupportedCommands": "MoveUp,MoveDown,MoveLeft,MoveRight,Select,Back,ToggleContextMenu,ToggleFullscreen,ToggleOsdMenu,GoHome,PageUp,NextLetter,GoToSearch,GoToSettings,PageDown,PreviousLetter,TakeScreenshot,VolumeUp,VolumeDown,ToggleMute,SendString,DisplayMessage,SetAudioStreamIndex,SetSubtitleStreamIndex,SetRepeatMode,Mute,Unmute,SetVolume,Play,Playstate,PlayNext,PlayMediaSource"
                                                       },
                                                       "type": "POST",
                                                       "url": "http://{jellyfin-server}/Sessions/Capabilities/Full",
                                                       "headers": {
                                                           "Content-type": "application/json",
                                                           "Accept-Charset": "UTF-8,*",
                                                           "Accept-encoding": "gzip",
                                                           "User-Agent": "Jellyfin-Kodi/0.7.11+py3",
                                                           "x-emby-authorization": "MediaBrowser Client=Kodi, Device=KITCHEN-FD, DeviceId=37ffbbad-8ecc-4711-911c-53c64adcd0d3, Version=0.7.11+py3, UserId=652370786690407d9644918c90401e00",
                                                           "X-MediaBrowser-Token": "{jellyfin-token}"
                                                       },
                                                       "timeout": 30,
                                                       "verify": true
                                                   }
2024-01-31 23:25:33.081 T:2733     info <general>: JELLYFIN.jellyfin_kodi.helper.utils -> DEBUG::jellyfin_kodi/helper/utils.py:152 ---[ event: plugin.video.jellyfin/ForceKeepAlive ] "[60]"

Adam1986 avatar Jan 31 '24 23:01 Adam1986

From your comments are you making changes to watch/play state with the client offline and they are not syncing on reconnect? If after you reconnect you mark an item is watched does that come through? i.e. does the reconnect and subsequent sync work?

I tested blocking my client, updating a watch status on the server and unblocking 10 minutes later, the status event came through within 60 seconds of being unblocked. This looks to be handled by the websocket reconnect param. I didn't get a websocket opened message. I guess the change events were buffered by the server.

If I do the above and also tcpkill the socket I can reproduce the problem, the client is online and gets changes going forward but not anything that occurred while disconnected. Perhaps the client needs to request a full or partial (Via Kodi sync queue) library update on reconnect which is currently done on plugin startup.

I could look at triggering a sync for changes on reconnect. Doing a full sync feels too much and may have negative impact on clients that reconnect frequently for whatever reason.

smcmnz avatar Feb 04 '24 22:02 smcmnz

Offline sync of playback / re-sync after reconnect sound like separate pullrequest(s) to the one in question here

On 4 February 2024 23.22.50 CET, smcmnz @.***> wrote:

From your comments are you making changes to watch/play state with the client offline and they are not syncing on reconnect? If after you reconnect you mark an item is watched does that come through? i.e. does the reconnect and subsequent sync work?

I tested blocking my client, updating a watch status on the server and unblocking 10 minutes later, the status event came through within 60 seconds of being unblocked. This looks to be handled by the websocket reconnect param. I didn't get a websocket opened message. I guess the change events were buffered by the server.

If I do the above and also tcpkill the socket I can reproduce the problem, the client is online and gets changes going forward but not anything that occurred while disconnected. Perhaps the client needs to request a full or partial (Via Kodi sync queue) library update on reconnect which is currently done on plugin startup.

I could look at triggering a sync for changes on reconnect. Doing a full sync feels too much and may have negative impact on clients that reconnect frequently for whatever reason.

-- Reply to this email directly or view it on GitHub: https://github.com/jellyfin/jellyfin-kodi/issues/797#issuecomment-1925945358 You are receiving this because you are subscribed to this thread.

Message ID: @.***>

oddstr13 avatar Feb 05 '24 07:02 oddstr13