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

Closed socket from time to time #51

Open
dorinand opened this issue Oct 27, 2019 · 0 comments
Open

Closed socket from time to time #51

dorinand opened this issue Oct 27, 2019 · 0 comments

Comments

@dorinand
Copy link

Issue:

Hi, I am using this library and from time to time, I have registered that my websocket stopped working. I tried to debug it, and find out, socket is missing but no error message is logged.

Now, lets show some example. I dockerized (the same problem was discovered even when the application was not dockerized) my application and find out via netstat command, I have opened socket with destination port 443:

docker exec mywebsocket /bin/netstat -tpn
tcp        0      0 172.23.0.13:56340      1.2.3.4:443       ESTABLISHED 1/python3

When I check it with my websocket app that did not receive any messages for a long time, I find out there is no opened socket, but also no errors in output (I checked docker logs and also my app logs):

docker exec mywebsocket /bin/netstat -tpn 

My fix:

I implemented function check_websocket_connection() and insert it inside while loop after sleep:

import os
import psutil
import logging

logger = logging.getLogger()

process = psutil.Process(os.getpid())

def check_websocket_connection():
    for connection in process.connections():
        if connection.raddr.port == 443 and connection.status == 'ESTABLISHED':
            logger.debug('Connections on port 433 is established.')
            return True
        
        return False
while True:
    # Do other things in the meantime here...
    time.sleep(1)
    if not check_websocket_connection():
        logger.error('Missing socket')
        break 

It check every second if there exists some connection with destination port 443 created by programs PID. I know, it can't be used if PID use more than one connection with port 443, but I need only this connection so it works.

This is just snippet, I created whole class with logging, I hope I did not make any mistake here.

It works for now, but is there a better way how to solve it?

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

No branches or pull requests

1 participant