Skip to content

Closed socket from time to time #51

@dorinand

Description

@dorinand

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions