What can be the reasons that cause a socket.io session to be crashed and server returns invalid session or session is disconnected ?
2 Answers
There is a specific situation that causes these problems with the session. When a client fails to send the pings at the expected interval the server declares the client gone and deletes the session. If a client that falls into this situation later tries to send a ping or another request using the now invalidated session id it will receive one of these errors.
Another possible problem with the same outcome is when the client does send the pings at the correct intervals, but the server is blocked or too busy to process these pings in time.
So to summarize, if you think your clients are well behaved, I would look at potential blocking tasks in your server.

- 65,299
- 14
- 133
- 152
-
Thank for you response, I added the value for ping_timeout but didn't solve the problem. Seems like two sessions are opened even though i have one client. – Mouna Ben Ayed May 27 '19 at 09:32
-
This isn't a problem with a timeout setting. As I said in my answer, either your client is misbehaving, or the server is too busy to process the messages from the client in a timely manner. – Miguel Grinberg May 27 '19 at 18:40
-
In fact, the problem occurs when a long message that takes a long time to get processed is sent from the server to the client. It appears that the session is closed just before the message get sent and automatically a new session gets opened. – Mouna Ben Ayed May 28 '19 at 12:10
-
How long is "a long time"? Who needs this processing time, the client or the server? – Miguel Grinberg May 28 '19 at 14:07
-
The client not the server, sorry for the mistake. "a long time" from 30s to 60s or even more. I can't give you and an exact value for the processing time. – Mouna Ben Ayed May 28 '19 at 14:54
-
This agrees with my diagnosis, right? Is the client sending the required ping packets? If you keep it blocked for up to a minute then it will miss some of these pings. – Miguel Grinberg May 28 '19 at 17:44
-
Yes while there is a ping that needs process other pings are sent from client. But the server is blocked waiting to end that process. What can I do to solve this issue, have you an idea ? – Mouna Ben Ayed May 28 '19 at 21:44
-
Why is the server blocked if the processing is done by the client? – Miguel Grinberg May 29 '19 at 14:03
-
I added a figure in the answer area to show the project's architecture. – Mouna Ben Ayed May 30 '19 at 09:55
-
Are you using eventlet or gevent for your Flask server? If yes, have you monkey patched the standard library? – Miguel Grinberg May 30 '19 at 17:15
-
It's already implemented by the rasa team https://github.com/RasaHQ/rasa/blob/master/rasa/server.py I didn't change their code – Mouna Ben Ayed May 31 '19 at 11:40
-
So this project doesn't use Flask, it uses Sanic and asyncio. I need to see the part of the application that you added. You said you send out requests to an API? How are you doing that? – Miguel Grinberg Jun 01 '19 at 15:39
-
I just call an API with a get request with an access token in the headers. When it returns the result I send it back as a message for the server part. And that when things get messed up. – Mouna Ben Ayed Jun 05 '19 at 18:35
-
@MounaBenAyed you need to tell me exactly how you are doing it, not a high-level description. My guess is that this is the problem, you are making a blocking call, and that breaks the async application. – Miguel Grinberg Jun 05 '19 at 22:46
Ok, I'll illustrate my problem in this figure project's architecture . In fact, I have a websocket between the react app and the rasa ( tool for creating chatbots) based on flask. bot response need to access to an external API to retrieve some data. Here where things go wrong. Sometimes, these requests take too long to return a response, and that's when websocket misbehave.

- 53
- 1
- 7