question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unclose session after disconnect

See original GitHub issue

server.py

sio = socketio.AsyncServer(
    async_mode='asgi', client_manager=mgr, cors_allowed_origins="*")


@sio.event
async def connect(sid, environ):
    claims = get_jwt_claims_from_environ(environ)
    ga = get_ga_from_environ(environ)
    async with sio.session(sid) as session:
        await Lock().wait()
        if claims:
            user_ID = claims.get('user_ID')
            session['user_ID'] = user_ID
            ret = await RedisClient.add_to_set(user_ID)
        if not ret:
            raise ConnectionRefusedError('Duplicate connect')


@sio.event
async def disconnect(sid):
    async with sio.session(sid) as session:
        user_ID = session.get('user_ID')
        if user_ID is not None:
            await RedisClient.remove_from_set(session.get('user_ID', ''))

version:

# python 3.6.9

aioredis        1.3.1  
async-timeout   3.0.1  
click           7.1.1  
h11             0.9.0  
hiredis         1.0.1  
httptools       0.1.1  
pip             19.3.1 
python-engineio 3.12.1 
python-socketio 4.5.0  
setuptools      41.6.0 
six             1.14.0 
uvicorn         0.11.3 
uvloop          0.14.0 
websockets      8.1    
wheel           0.33.6 

start:

uvicorn server:app --host 0.0.0.0 --workers 1 --log-level error --use-colors

nginx conf:

    location /socketio/ {
        proxy_pass http://localhost:8888/;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

When connect, I store user_ID in session and redis sets. If user_ID exists in redis sets, i will refuse connect. When disconnect, remove user_ID from redis sets. I find some user_ID not exists in redis sets but in session, I get session use follow code:

def get_online_user(sio):
    for s in sio.eio.sockets.values():
        session = s.session.get('/', {})
        if session.get('user_ID') is not None:
            online_user.append(session['user_ID'])
    return online_user

Sometimes, it has duplicate user_ID in session.

image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
victorvorobevcommented, Apr 17, 2020

Hello! I’ve noticed the same issue (I guess) with using SocketIO as a server to run. For some reason, the closed sockets are not being deleted from within the EngineIO, and they remain in memory, despite the fact that they are closed. I’ve been tracking the memory usage of my app, and it appears to be that when I refresh the browser page, I’m getting some memory leaks. I’ve logged out the sockets via simple script:

f.write("Opened sockets: {}".format(len(socketio.server.eio.sockets)))
f.write("\n")
for sid, socket in socketio.server.eio.sockets.iteritems():
    f.write("SID: {}\n".format(sid))
    f.write("State: connected {} closing {}, closed {}\n".format(socket.connected, socket.closing, socket.closed))
    f.write("Socket Queue: size {} \n\n".format(socket.queue.qsize()))

And the result looked quite sad, like so (don’t mind the queue size, I’m broadcasting quite a lot of data, so no surprises here):

Opened sockets: 30                                                                                   
SID: 68737ede0359448e8084c0e3c7b25b2e                                                                
State: connected True, upgrading False, upgraded False, closing True, closed True                    
Socket Queue: full False, empty False, size 169                                                      

SID: dc47bce25482424aac9a3a521fff30cf                                                                
State: connected True, upgrading False, upgraded False, closing True, closed True                    
Socket Queue: full False, empty False, size 135                                                      

SID: 3abd0a4f07dc491ea7f836d1dd4d65e1                                                                
State: connected True, upgrading False, upgraded False, closing True, closed True                    
Socket Queue: full False, empty False, size 137                                                      

SID: 24289a8c680449599600b9da6512b7e1                                                                
State: connected True, upgrading False, upgraded False, closing True, closed True                    
Socket Queue: full False, empty False, size 154                                                      

SID: 95c1437242cd485a9d9a922310a2a60d                                                                
State: connected True, upgrading False, upgraded False, closing True, closed True                    
Socket Queue: full False, empty False, size 139       

...

The worst part is that even when the selenium script is stopped and all the browsers closed, those sockets are not going anywhere.

I can, of course, clean them up manually via this code:

sessions = socketio.server.eio.sockets.keys()
for sid in sessions:
    try:
        socketio.server.eio._get_socket(sid)
    except KeyError:
        pass

But this just don’t seem to be right thing to do.

My setup is straight forward SocketIO(http_compression=True).run() with no Gunicorn neither Nginx to mess up with.

python-engineio == 3.12.1
python-socketio == 4.3.0
0reactions
Kazhuucommented, May 6, 2020

I’ve been looking into why my application code doesn’t close old connections. It seems this is the very same issue I’m facing. Any update on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Windows 10 RDP session closes after disconnecting. - TechNet
Now if I close the session, the user log off and stop what was doing, so I have to maintain RDP connection open...
Read more >
Session logged out after disconnection of remote connection
On the Windows 7 server, start gpedit.msc , browse to Computer Configuration --> Administrative Templates --> Windows Components --> Remote ...
Read more >
How to set time limit for disconnected sessions Windows ...
You can limit the amount of time that active, disconnected, and idle sessions remain on the server. Two methods are described below:
Read more >
Exiting RemoteApp session leaves disconnected RDP session
The problem is that when the users exit the RemoteApp, their session is showing on the server as 'disconnected'. Is there any way...
Read more >
Reconnecting to an existing session - http:\\hazards.fema.gov
The session begins when the user logs into the server (step 3) and ends when the ... Disconnected Session: the time period during...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found