Skip to content

client.connect has inconsistent behavior #109

Description

@IonCaza

Hey there, I'm working on a little software KVM for my 3x C3 42". Here's my retry logic when trying to connect to my TVs:

def connectTVs(config):
    #TODO : Make sure TVs are on
    TVClients.clear()
    for tv in config['TVs']:
        client = WebOSClient(tv['ip'], secure=True)
        client.name = tv['name']
        keyString = {"client_key": tv['client_key']}
        
        retry_count = 0
        while retry_count < 3:
            try:
                client.connect()
                break
            except Exception as e:
                    retry_count += 1
                    logging.info(f"Connection attempt in connectTVs on count {retry_count} for {tv['name']} failed: {str(e)}")

        retry_count = 0
        while retry_count < 3:
            try:
                for status in client.register(keyString):
                    logging.info(f"{tv['name']} registered and logged in | status {status}")
                break
            except Exception as e:
                    retry_count += 1
                    logging.info(f"Connection attempt in connectTVs on count {retry_count} for {tv['name']} failed: {str(e)}")
  
        if retry_count == 3: sys.exit()

        TVClients.append(client)
    logging.info(TVClients)

This is my registration logic:

def registerTVs(config):
    tvCreds = {}

    for tv in config['TVs']:
        retry_count = 0 
                       
        if "client_key" not in tv or not tv['client_key']:
            while retry_count < 3:
                try:
                    client = WebOSClient(tv['ip'], secure=True)
                    client.connect()
                    for status in client.register(tvCreds):
                        if status == WebOSClient.PROMPTED:
                            logging.info("Please accept the connect on the TV!")
                        elif status == WebOSClient.REGISTERED:
                            logging.info("Registration successful!")
                    tv.update(tvCreds)
                    break
                except ConnectionResetError as e:
                    retry_count += 1
                    logging.info(f"Connection attempt in registerTVs on count {retry_count} failed: {str(e)}")
    return config

Every once in a while one of them fails to connect even though I try multiple times. Here's what that looks like:

INFO:root:Connection attempt in connectTVs on count 1 for RightMonitor failed: [Errno 54] Connection reset by peer
INFO:root:Connection attempt in connectTVs on count 2 for RightMonitor failed: [Errno 22] Invalid argument
INFO:root:Connection attempt in connectTVs on count 3 for RightMonitor failed: [Errno 9] Bad file descriptor
INFO:root:Connection attempt in connectTVs on count 1 for RightMonitor failed: [Errno 9] Bad file descriptor
INFO:root:Connection attempt in connectTVs on count 2 for RightMonitor failed: [Errno 9] Bad file descriptor
INFO:root:Connection attempt in connectTVs on count 3 for RightMonitor failed: [Errno 9] Bad file descriptor

Any ideas why this is happening? Most of the times, hitting that logic again works without issues.

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