Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sio.save_session() raising KeyError: 'Session not found' #585

Closed
PATAPOsha opened this issue Dec 14, 2020 · 4 comments
Closed

sio.save_session() raising KeyError: 'Session not found' #585

PATAPOsha opened this issue Dec 14, 2020 · 4 comments
Assignees
Labels

Comments

@PATAPOsha
Copy link

PATAPOsha commented Dec 14, 2020

python-socketio==5.0.2
eventlet==0.30.0
Python 3.7.7 on linux (WSL2 Ubuntu 18.04.4 LTS)

I'm trying to run server from the docs using sessions.
My server code:

import socketio
import eventlet
from django.conf import settings

from mydjangoapp.wsgi import application

from chat_ws.logger import logger

# WS_CORS_ALLOWED_ORIGINS = ['http://127.0.0.1:8000', 'http://127.0.0.1:5000']
sio = socketio.Server(cors_allowed_origins=settings.WS_CORS_ALLOWED_ORIGINS, async_mode='eventlet')
app = socketio.WSGIApp(sio, application)

@sio.event(namespace='/chat')
def connect(sid, environ):
    print(f'connected: {sid}')
    sio.save_session(sid, {'username': 'test'}, namespace='/chat')

@sio.event(namespace='/chat')
def message(sid, data):
    print(f'message sid: {sid}')
    with sio.session(sid) as session:
        print(f"{session['username']} msg: {data}")
        sio.emit("message", "Zdarova!")

@sio.event(namespace='/chat')
def disconnect(sid):
    print('disconnect ', sid)

if __name__ == "__main__":
    eventlet.wsgi.server(eventlet.listen(("", 5000)), app)

My client code:

import time
import socketio

sio = socketio.Client()

@sio.on('connect', namespace='/chat')
def on_connect():
    print("I'm connected to the /chat namespace!")

@sio.on('join_event', namespace='/chat')
def on_message(data):
    print(f'[join_event]: {data}')

@sio.on('message', namespace='/chat')
def on_message(data):
    print(f'I received a message: {data}')

sio.connect('http://127.0.0.1:5000', namespaces=['/chat'])
time.sleep(5)
sio.emit('message', {'foo': 'bar'}, namespace='/chat')
print("sent msg.")

When client connects, server raises following error:

(25100) wsgi starting up on http://0.0.0.0:5000
(25100) accepted ('127.0.0.1', 60310)
127.0.0.1 - - [14/Dec/2020 13:07:17] "GET /socket.io/?EIO=4&transport=polling&t=NPXNBu7 HTTP/1.1" 200 329 0.001411
(25100) accepted ('127.0.0.1', 60312)
connected: jNJJ7jG2LFx5w5YRAAAB
message handler error
Traceback (most recent call last):
  File "/home/pata/.pyenv/versions/3.7.7/envs/env/lib/python3.7/site-packages/engineio/server.py", line 606, in _get_socket
    s = self.sockets[sid]
KeyError: 'Q2dEwhemB-8GAYSMAAAB'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/pata/.pyenv/versions/3.7.7/envs/env/lib/python3.7/site-packages/engineio/server.py", line 595, in _trigger_event
    return self.handlers[event](*args)
  File "/home/pata/.pyenv/versions/3.7.7/envs/env/lib/python3.7/site-packages/socketio/server.py", line 722, in _handle_eio_message
    self._handle_connect(eio_sid, pkt.namespace)
  File "/home/pata/.pyenv/versions/3.7.7/envs/env/lib/python3.7/site-packages/socketio/server.py", line 622, in _handle_connect
    self.environ[eio_sid])
  File "/home/pata/.pyenv/versions/3.7.7/envs/env/lib/python3.7/site-packages/socketio/server.py", line 694, in _trigger_event
    return self.handlers[namespace][event](*args)
  File "/mnt/c/Users/pata/PycharmProjects/ec-3.0-backend/source/chat_ws/ws.py", line 65, in connect
    sio.save_session(sid, {'username': 'dgdfg'}, namespace='/chat')
  File "/home/pata/.pyenv/versions/3.7.7/envs/env/lib/python3.7/site-packages/socketio/server.py", line 459, in save_session
    eio_session = self.eio.get_session(sid)
  File "/home/pata/.pyenv/versions/3.7.7/envs/env/lib/python3.7/site-packages/engineio/server.py", line 231, in get_session
    socket = self._get_socket(sid)
  File "/home/pata/.pyenv/versions/3.7.7/envs/env/lib/python3.7/site-packages/engineio/server.py", line 608, in _get_socket
    raise KeyError('Session not found')
KeyError: 'Session not found'

Output log contains printed sid that was received inside connect() method: connected: jNJJ7jG2LFx5w5YRAAAB
I tried to look inside engineio/server.py Server._get_socket() method using debugger. There is one record inside self.sockets but it's completely different from what I'm printing inside my connect() method.
image

@miguelgrinberg
Copy link
Owner

The different sid values is a change introduced in the latest version of the Socket.IO protocol, that is expected. But this may be a bug introduced when I implemented this change.

@miguelgrinberg
Copy link
Owner

Fixed in release 5.0.3, please upgrade and let me know if you still experience problems.

@pavel-vorobyev
Copy link

Still experiencing this error on v5.0.4

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Jan 7, 2021

@pavel-vorobyev the issue reported in this bug is fixed. If you are receiving the same error, please open an issue and provide code that I can use to reproduce the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants