flexxui/flexx

"Lost connection to server" on every little connection error/timeout

matkuki opened this issue ยท 6 comments

Hi @almarklein

I have an application that is running on a server that is accessed through a VPN, which works great.
But every little hiccup for even a split second in the connection gives the "Lost connection to server" error.

Is there a way to mitigate this, maybe with inserting a retry somewhere?

Thanks

Websockets are indeed more susceptible to connection problems. Technically it should be possible to restore the connection, but I fear that would involve substantial changes to the Flexx internals, because the session is very much tied to the websocket connection right now ...

No problem, was just wondering if there was a simple workaround.
Thanks for the information.

Hey @almarklein ,
I looked at some of the source and there are quite a few self.ws or self._ws references.
If I would update all of those references with a new WebSocket when on_ws_close (flexx/app/_clientcore.py line 347) is called, would that work?

I'm not sure, but it could be worth a try ...

Hey @almarklein

I tried a bit of fiddling around, but now I'm getting the error Asked for session id X, but could not find it when trying to reconnect the socket.
My main problem is that I do not have an idea of how the websocket initialization chain is processed. Could you give me a point-by-point description of the websocket initialization order of the methods needed to initialize and then communicate with the JS side?

Thanks

That error originates here:

flexx/flexx/app/_app.py

Lines 601 to 612 in 7461524

def connect_client(self, ws, name, session_id, cookies=None):
""" Connect a client to a session that was previously created.
"""
_, pending, connected = self._appinfo[name]
# Search for the session with the specific id
for session in pending:
if session.id == session_id:
pending.remove(session)
break
else:
raise RuntimeError('Asked for session id %r, but could not find it' %
session_id)

An incoming connection asks the manager to find the corresponding session. In this case it fails, because it looks in the pending sessions, and it's not there.

The session lifetime has a few stages:

STATUS = new_type('Enum', (), {'PENDING': 1, 'CONNECTED': 2, 'CLOSED': 0})

I think we'd need to add another stage, maybe LOST, from which we can reconnect. The manager will also have to keep track of lost sessions in its _appinfo data structure. Though we'd also need a mechanism to purge LOST sessions once they're lost for too long.

I hope this helps ...